用easyx写一个贪吃蛇游戏
时间: 2024-06-08 11:05:27 浏览: 76
好的,以下是使用EasyX库实现贪吃蛇游戏的示例代码:
```c++
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_LENGTH 100 // 贪吃蛇最大长度
#define BLOCK_SIZE 20 // 每个方块的大小
#define WIDTH 800 // 窗口宽度
#define HEIGHT 600 // 窗口高度
// 方向枚举
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
// 食物结构体
struct Food {
int x; // x轴坐标
int y; // y轴坐标
};
// 贪吃蛇结构体
struct Snake {
int length; // 长度
int x[MAX_LENGTH]; // x轴坐标数组
int y[MAX_LENGTH]; // y轴坐标数组
Direction direction; // 当前方向
};
// 初始化贪吃蛇
void initSnake(Snake &snake) {
snake.length = 3;
snake.x[0] = BLOCK_SIZE * 5;
snake.y[0] = BLOCK_SIZE * 5;
snake.x[1] = BLOCK_SIZE * 4;
snake.y[1] = BLOCK_SIZE * 5;
snake.x[2] = BLOCK_SIZE * 3;
snake.y[2] = BLOCK_SIZE * 5;
snake.direction = RIGHT;
}
// 初始化食物
void initFood(Food &food) {
srand((unsigned)time(NULL));
food.x = (rand() % (WIDTH / BLOCK_SIZE)) * BLOCK_SIZE;
food.y = (rand() % (HEIGHT / BLOCK_SIZE)) * BLOCK_SIZE;
}
// 绘制方块
void drawBlock(int x, int y, COLORREF color) {
setfillcolor(color);
setlinecolor(RGB(255, 255, 255));
fillrectangle(x, y, x + BLOCK_SIZE, y + BLOCK_SIZE);
}
// 绘制贪吃蛇
void drawSnake(Snake &snake) {
for (int i = 0; i < snake.length; i++) {
drawBlock(snake.x[i], snake.y[i], RGB(0, 255, 0));
}
}
// 绘制食物
void drawFood(Food &food) {
drawBlock(food.x, food.y, RGB(255, 0, 0));
}
// 贪吃蛇移动
void moveSnake(Snake &snake) {
for (int i = snake.length - 1; i > 0; i--) {
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
switch (snake.direction) {
case UP:
snake.y[0] -= BLOCK_SIZE;
break;
case DOWN:
snake.y[0] += BLOCK_SIZE;
break;
case LEFT:
snake.x[0] -= BLOCK_SIZE;
break;
case RIGHT:
snake.x[0] += BLOCK_SIZE;
break;
}
}
// 判断贪吃蛇是否吃到食物
bool isEatFood(Snake &snake, Food &food) {
return snake.x[0] == food.x && snake.y[0] == food.y;
}
// 判断贪吃蛇是否撞墙或自撞
bool isGameOver(Snake &snake) {
if (snake.x[0] < 0 || snake.x[0] >= WIDTH || snake.y[0] < 0 || snake.y[0] >= HEIGHT) {
return true;
}
for (int i = 1; i < snake.length; i++) {
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {
return true;
}
}
return false;
}
int main() {
// 初始化窗口
initgraph(WIDTH, HEIGHT);
setbkcolor(RGB(255, 255, 255));
// 初始化贪吃蛇和食物
Snake snake;
Food food;
initSnake(snake);
initFood(food);
// 游戏循环
while (true) {
// 绘制贪吃蛇和食物
cleardevice();
drawSnake(snake);
drawFood(food);
// 贪吃蛇移动
moveSnake(snake);
// 判断是否吃到食物
if (isEatFood(snake, food)) {
snake.length++;
initFood(food);
}
// 判断游戏是否结束
if (isGameOver(snake)) {
MessageBox(NULL, _T("游戏结束!"), _T("提示"), MB_OK);
break;
}
// 监听键盘方向键
if (_kbhit()) {
switch (_getch()) {
case 'w':
case 'W':
case VK_UP:
if (snake.direction != DOWN) {
snake.direction = UP;
}
break;
case 's':
case 'S':
case VK_DOWN:
if (snake.direction != UP) {
snake.direction = DOWN;
}
break;
case 'a':
case 'A':
case VK_LEFT:
if (snake.direction != RIGHT) {
snake.direction = LEFT;
}
break;
case 'd':
case 'D':
case VK_RIGHT:
if (snake.direction != LEFT) {
snake.direction = RIGHT;
}
break;
}
}
// 刷新画面
Sleep(100);
}
// 关闭窗口
closegraph();
return 0;
}
```
运行示意图如下:
![贪吃蛇游戏示意图](https://img-blog.csdn.net/20180530140837963?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3VzZXJfNjExMjIzNjg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85/blog_pic.jpg)
阅读全文