用c++写面对对象思想贪吃蛇
时间: 2023-12-24 07:05:08 浏览: 77
好的,我可以帮你回答这个问题。下面是一个简单的面向对象思想的贪吃蛇游戏的C++实现:
```c++
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20; // 地图宽度
const int height = 20; // 地图高度
int score = 0; // 得分
class Point {
private:
int x;
int y;
public:
Point(int _x, int _y) :x(_x), y(_y) {}
int getX() const { return x; }
int getY() const { return y; }
void setX(int _x) { x = _x; }
void setY(int _y) { y = _y; }
};
class Snake {
private:
vector<Point> body; // 蛇身
int direction; // 移动方向
public:
Snake(int x, int y) {
body.push_back(Point(x, y));
direction = 1;
}
void move() {
Point head = getHead();
switch (direction) {
case 1: // 上
body.insert(body.begin(), Point(head.getX(), head.getY() - 1));
break;
case 2: // 下
body.insert(body.begin(), Point(head.getX(), head.getY() + 1));
break;
case 3: // 左
body.insert(body.begin(), Point(head.getX() - 1, head.getY()));
break;
case 4: // 右
body.insert(body.begin(), Point(head.getX() + 1, head.getY()));
break;
}
body.pop_back();
}
void changeDirection(int d) {
if (abs(direction - d) != 2) {
direction = d;
}
}
bool checkCollision() const {
Point head = getHead();
if (head.getX() < 0 || head.getX() >= width || head.getY() < 0 || head.getY() >= height) {
return true;
}
for (int i = 1; i < body.size(); i++) {
if (head.getX() == body[i].getX() && head.getY() == body[i].getY()) {
return true;
}
}
return false;
}
void eat() {
Point head = getHead();
body.insert(body.begin(), Point(head.getX(), head.getY()));
score += 10;
}
Point getHead() const {
return body.front();
}
vector<Point> getBody() const {
return body;
}
};
class Food {
private:
Point position; // 食物位置
public:
Food() {
srand((unsigned)time(NULL));
int x = rand() % width;
int y = rand() % height;
position = Point(x, y);
}
Point getPosition() const {
return position;
}
void regenerate() {
srand((unsigned)time(NULL));
int x = rand() % width;
int y = rand() % height;
position.setX(x);
position.setY(y);
}
};
class Game {
private:
Snake snake;
Food food;
public:
Game(int x, int y) :snake(x, y) {}
void run() {
while (true) {
system("cls");
drawMap();
drawSnake();
drawFood();
moveSnake();
if (snake.checkCollision()) {
gameOver();
break;
}
if (eatFood()) {
snake.eat();
food.regenerate();
}
Sleep(150);
}
}
void drawMap() const {
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < height; i++) {
cout << "#";
for (int j = 0; j < width; j++) {
cout << " ";
}
cout << "#" << endl;
}
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
cout << "score: " << score << endl;
}
void drawSnake() const {
vector<Point> body = snake.getBody();
for (int i = 0; i < body.size(); i++) {
int x = body[i].getX();
int y = body[i].getY();
gotoxy(x + 1, y + 1);
if (i == 0) {
cout << "@";
}
else {
cout << "*";
}
}
}
void drawFood() const {
Point pos = food.getPosition();
gotoxy(pos.getX() + 1, pos.getY() + 1);
cout << "$";
}
void moveSnake() {
if (_kbhit()) {
int c = _getch();
switch (c) {
case 'w':
case 'W':
snake.changeDirection(1);
break;
case 's':
case 'S':
snake.changeDirection(2);
break;
case 'a':
case 'A':
snake.changeDirection(3);
break;
case 'd':
case 'D':
snake.changeDirection(4);
break;
}
}
snake.move();
}
bool eatFood() const {
Point head = snake.getHead();
Point foodPos = food.getPosition();
if (head.getX() == foodPos.getX() && head.getY() == foodPos.getY()) {
return true;
}
return false;
}
void gameOver() const {
system("cls");
cout << "Game Over!" << endl;
cout << "Your Score: " << score << endl;
system("pause");
}
void gotoxy(int x, int y) const {
COORD pos = { x, y };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(console, pos);
}
};
int main() {
Game game(width / 2, height / 2);
game.run();
return 0;
}
```
这个程序使用了三个类:`Point`、`Snake`和`Food`。其中`Point`类表示地图上的一个点,包含`x`和`y`两个坐标;`Snake`类表示蛇,包含蛇身、移动方向等属性和移动、改变方向、检查碰撞等方法;`Food`类表示食物,包含食物的位置和重新生成食物的方法。`Game`类是游戏主类,包含游戏的主循环、绘制地图、绘制蛇、绘制食物、移动蛇、检测碰撞、吃食物、游戏结束等方法。
在`main`函数中创建`Game`对象并调用`run`方法即可启动游戏。这个程序使用了Windows API的一些函数来实现控制台的光标移动和清屏等操作。
阅读全文