c++用面向对象的思想和链表写一个贪吃蛇游戏
时间: 2024-03-15 20:47:12 浏览: 64
非常好的选择!下面是一个基于面向对象思想和链表的贪吃蛇游戏的示例代码:
```cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20; // 地图宽度
const int height = 20; // 地图高度
int score = 0; // 得分
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 方向
class SnakeNode {
public:
int x;
int y;
SnakeNode* next;
SnakeNode(int col, int row) {
x = col;
y = row;
next = NULL;
}
};
class Snake {
public:
SnakeNode* head;
SnakeNode* tail;
Snake() {
head = new SnakeNode(width / 2, height / 2);
tail = head;
}
void addNode() {
SnakeNode* node = new SnakeNode(tail->x, tail->y);
tail->next = node;
tail = node;
}
void move(int direction) {
int x = head->x;
int y = head->y;
switch (direction) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
SnakeNode* node = new SnakeNode(x, y);
node->next = head;
head = node;
SnakeNode* cur = head;
while (cur->next != tail) {
cur = cur->next;
}
delete tail;
tail = cur;
tail->next = NULL;
}
bool collision() {
SnakeNode* cur = head->next;
while (cur != NULL) {
if (head->x == cur->x && head->y == cur->y) {
return true;
}
cur = cur->next;
}
return false;
}
};
class Food {
public:
int x;
int y;
Food() {
srand(time(NULL));
x = rand() % (width - 2) + 1;
y = rand() % (height - 2) + 1;
}
void regenerate() {
srand(time(NULL));
x = rand() % (width - 2) + 1;
y = rand() % (height - 2) + 1;
}
};
class Game {
public:
Snake* snake;
Food* food;
Direction direction;
Game() {
snake = new Snake();
food = new Food();
direction = STOP;
}
void draw() {
system("cls");
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
SnakeNode* cur = snake->head;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0) {
cout << "#";
}
else if (j == width - 1) {
cout << "#";
}
else if (i == food->y && j == food->x) {
cout << "F";
}
else if (i == cur->y && j == cur->x) {
cout << "O";
cur = cur->next;
}
else {
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < width + 2; i++) {
cout << "#";
}
cout << endl;
cout << "Score: " << score << endl;
}
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
direction = LEFT;
break;
case 'd':
direction = RIGHT;
break;
case 'w':
direction = UP;
break;
case 's':
direction = DOWN;
break;
case 'x':
direction = STOP;
break;
}
}
}
void logic() {
switch (direction) {
case LEFT:
snake->move(LEFT);
break;
case RIGHT:
snake->move(RIGHT);
break;
case UP:
snake->move(UP);
break;
case DOWN:
snake->move(DOWN);
break;
default:
break;
}
if (snake->head->x == food->x && snake->head->y == food->y) {
snake->addNode();
food->regenerate();
score += 10;
}
if (snake->head->x == 0 || snake->head->x == width - 1 || snake->head->y == 0 || snake->head->y == height - 1) {
gameOver();
}
if (snake->collision()) {
gameOver();
}
}
void gameOver() {
cout << "Game Over!" << endl;
cout << "Your score: " << score << endl;
Sleep(2000);
exit(0);
}
};
int main() {
Game game;
while (true) {
game.draw();
game.input();
game.logic();
Sleep(100);
}
return 0;
}
```
这段代码使用了三个类:Snake、SnakeNode、Food和Game,它们分别表示蛇、蛇身节点、食物和游戏。Snake类中有一个指向蛇头和蛇尾的指针,它们都是SnakeNode类型的。Snake类中的move()函数使用链表的思想,每次将新的蛇头插入到链表的头部,然后删除链表的尾部。Food类负责生成和重新生成食物。Game类负责游戏的逻辑、绘制和输入。
阅读全文