用类实现贪吃蛇游戏C++
时间: 2023-07-13 13:06:35 浏览: 108
以下一个简单的贪吃蛇游戏的C++代码实现,使用了面向对象编程的思想,利用类来实现游戏的各个功能:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
class Snake
{
public:
Snake(int x = 10, int y = 10) : length(1), direction(0)
{
body[0].x = x;
body[0].y = y;
}
void move()
{
for (int i = length; i > 0; i--)
{
body[i].x = body[i - 1].x;
body[i].y = body[i - 1].y;
}
switch (direction)
{
case 0: // up
body[0].y--;
break;
case 1: // down
body[0].y++;
break;
case 2: // left
body[0].x--;
break;
case 3: // right
body[0].x++;
break;
}
}
void changeDirection(char key)
{
switch (key)
{
case 'w':
if (direction != 1) // can't go down
direction = 0;
break;
case 's':
if (direction != 0) // can't go up
direction = 1;
break;
case 'a':
if (direction != 3) // can't go right
direction = 2;
break;
case 'd':
if (direction != 2) // can't go left
direction = 3;
break;
}
}
bool eat(Food &food)
{
if (body[0].x == food.x && body[0].y == food.y)
{
length++;
return true;
}
return false;
}
bool hitWall()
{
if (body[0].x < 0 || body[0].x >= WIDTH || body[0].y < 0 || body[0].y >= HEIGHT)
return true;
return false;
}
bool hitBody()
{
for (int i = 1; i < length; i++)
{
if (body[0].x == body[i].x && body[0].y == body[i].y)
{
return true;
}
}
return false;
}
public:
struct Point
{
int x;
int y;
};
static const int WIDTH = 20;
static const int HEIGHT = 20;
Point body[WIDTH * HEIGHT];
int length;
int direction;
};
class Food
{
public:
Food() : x(0), y(0) {}
void generate(Snake &snake)
{
srand(time(NULL));
do
{
x = rand() % Snake::WIDTH;
y = rand() % Snake::HEIGHT;
} while (isOnSnake(snake));
}
bool isOnSnake(Snake &snake)
{
for (int i = 0; i < snake.length; i++)
{
if (snake.body[i].x == x && snake.body[i].y == y)
{
return true;
}
}
return false;
}
public:
int x;
int y;
};
class Game
{
public:
Game() : score(0), isOver(false) {}
void start()
{
snake = Snake(Snake::WIDTH / 2, Snake::HEIGHT / 2);
food.generate(snake);
while (!isOver)
{
system("cls"); // clear console
draw();
processInput();
update();
Sleep(100); // wait for 100ms
}
cout << "Game over!" << endl;
}
void draw()
{
for (int i = 0; i <= Snake::HEIGHT; i++)
{
for (int j = 0; j <= Snake::WIDTH; j++)
{
if (i == 0 || i == Snake::HEIGHT || j == 0 || j == Snake::WIDTH)
{
cout << "#";
}
else if (i == food.y && j == food.x)
{
cout << "F";
}
else
{
bool isBody = false;
for (int k = 0; k < snake.length; k++)
{
if (snake.body[k].x == j && snake.body[k].y == i)
{
cout << "O";
isBody = true;
break;
}
}
if (!isBody)
{
cout << " ";
}
}
}
cout << endl;
}
cout << "Score: " << score << endl;
}
void processInput()
{
if (_kbhit())
{
char key = _getch();
snake.changeDirection(key);
}
}
void update()
{
snake.move();
if (snake.hitWall() || snake.hitBody())
{
isOver = true;
return;
}
if (snake.eat(food))
{
score += 10;
food.generate(snake);
}
}
public:
Snake snake;
Food food;
int score;
bool isOver;
};
int main()
{
Game game;
game.start();
return 0;
}
```
该代码实现了以下功能:
- Snake类:表示游戏中的蛇,包括蛇的身体、长度、方向等信息。实现了蛇的移动、改变方向、吃食物、撞墙、撞身体等功能。
- Food类:表示游戏中的食物,包括食物的坐标信息。实现了随机生成食物、判断食物是否在蛇身上等功能。
- Game类:表示整个游戏,包括蛇、食物、分数、游戏是否结束等信息。实现了游戏的开始、绘制、处理输入、更新等功能。
在main函数中,我们创建一个Game对象并调用其start方法,即可开始游戏。
该代码还有很多可以改进的地方,比如加入音效、增加难度等等,读者可以自行尝试。
阅读全文