用easyx写一个贪吃蛇游戏
时间: 2023-07-01 17:09:31 浏览: 113
基于easyx的贪吃蛇代码
好的,以下是用EasyX写的贪吃蛇游戏代码,希望能对您有帮助:
```c++
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
// 贪吃蛇的节点
struct Node {
int x;
int y;
Node *next;
};
const int width = 40; // 游戏区宽度
const int height = 30; // 游戏区高度
const int nodeSize = 20; // 节点大小
const int delay = 100; // 每个节点移动的延迟时间(毫秒)
const int levelUpScore = 10; // 等级提升的分数
const int initLength = 3; // 初始贪吃蛇长度
const int initSpeed = 5; // 初始速度等级
const int speedUpInterval = 5; // 每隔多少分数提升一级速度
const int maxSpeedLevel = 10; // 最大速度等级
int score = 0; // 得分
int speedLevel = initSpeed; // 速度等级
bool gameOver = false; // 游戏是否结束
Node *head = NULL; // 贪吃蛇头指针
Node *tail = NULL; // 贪吃蛇尾指针
Node *food = NULL; // 食物指针
// 生成随机食物
void generateFood() {
int x = rand() % width;
int y = rand() % height;
food = new Node({ x, y, NULL });
}
// 初始化游戏
void initGame() {
srand((unsigned int)time(NULL));
head = new Node({ width / 2, height / 2, NULL });
tail = head;
for (int i = 1; i < initLength; i++) {
Node *node = new Node({ head->x - i, head->y, NULL });
tail->next = node;
tail = node;
}
generateFood();
}
// 绘制游戏区
void drawGameArea() {
setfillcolor(RGB(255, 255, 255));
bar(0, 0, width * nodeSize, height * nodeSize);
}
// 绘制节点
void drawNode(Node *node, COLORREF color) {
setfillcolor(color);
setlinecolor(color);
int x = node->x * nodeSize;
int y = node->y * nodeSize;
fillrectangle(x, y, x + nodeSize, y + nodeSize);
}
// 绘制贪吃蛇
void drawSnake() {
Node *node = head;
while (node != NULL) {
drawNode(node, RGB(0, 0, 255));
node = node->next;
}
}
// 绘制食物
void drawFood() {
drawNode(food, RGB(255, 0, 0));
}
// 移动贪吃蛇
void moveSnake() {
// 添加新的头节点
Node *node = new Node({ head->x, head->y, head });
head = node;
// 删除尾节点
if (tail->next == NULL) {
Node *nodeToDelete = tail;
tail = tail->next;
delete nodeToDelete;
}
// 判断是否吃到食物
if (head->x == food->x && head->y == food->y) {
score++;
if (score % speedUpInterval == 0 && speedLevel < maxSpeedLevel) {
speedLevel++;
}
generateFood();
}
else {
// 删除头节点
Node *nodeToDelete = tail;
tail = tail->next;
delete nodeToDelete;
}
// 判断是否撞墙或撞到自己
node = head->next;
while (node != NULL) {
if (node->x == head->x && node->y == head->y) {
gameOver = true;
}
node = node->next;
}
if (head->x < 0 || head->x >= width || head->y < 0 || head->y >= height) {
gameOver = true;
}
}
// 更新游戏状态
void updateGame() {
moveSnake();
}
// 游戏结束
void endGame() {
setbkmode(TRANSPARENT);
settextcolor(RGB(255, 0, 0));
settextstyle(30, 0, _T("微软雅黑"));
outtextxy(width * nodeSize / 2 - 80, height * nodeSize / 2 - 15, _T("游戏结束"));
}
int main() {
initgraph(width * nodeSize, height * nodeSize);
initGame();
while (true) {
drawGameArea();
drawSnake();
drawFood();
updateGame();
if (gameOver) {
endGame();
break;
}
Sleep(delay / speedLevel);
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w':
if (head->next == NULL || head->y != head->next->y) {
head->y--;
}
break;
case 's':
if (head->next == NULL || head->y != head->next->y) {
head->y++;
}
break;
case 'a':
if (head->next == NULL || head->x != head->next->x) {
head->x--;
}
break;
case 'd':
if (head->next == NULL || head->x != head->next->x) {
head->x++;
}
break;
}
}
}
_getch();
closegraph();
return 0;
}
```
这是一个基本的贪吃蛇游戏,您可以根据需要进行修改和扩展。
阅读全文