利用ege不使用EasyX做一个贪吃蛇,包含用图片食物随机生成,蛇身与蛇头的碰撞检测,蛇头与边距的碰撞检测,障碍物的生成,难度的选择,
时间: 2024-04-20 07:26:33 浏览: 238
好的,以下是使用ege库编写的一个贪吃蛇游戏的示例代码,包含了用图片作为食物、碰撞检测、边界检测、障碍物生成和难度选择功能:
```cpp
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <string>
const int SIZE = 20; // 蛇身和食物的大小
const int WIDTH = 800; // 窗口宽度
const int HEIGHT = 600; // 窗口高度
struct Point {
int x;
int y;
};
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
void drawSnake(Point snake[], int length, IMAGE& imgSnake) {
for (int i = 0; i < length; i++) {
putimage(snake[i].x * SIZE, snake[i].y * SIZE, &imgSnake);
}
}
void generateFood(Point& food, IMAGE& imgFood) {
food.x = rand() % (WIDTH / SIZE);
food.y = rand() % (HEIGHT / SIZE);
putimage(food.x * SIZE, food.y * SIZE, &imgFood);
}
bool checkCollision(Point snake[], int length, Point food) {
if (snake[0].x == food.x && snake[0].y == food.y) {
return true;
}
for (int i = 1; i < length; i++) {
if (snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
return true;
}
}
return false;
}
bool checkBoundary(Point snake[], int length) {
if (snake[0].x < 0 || snake[0].x >= WIDTH / SIZE || snake[0].y < 0 || snake[0].y >= HEIGHT / SIZE) {
return true;
}
return false;
}
bool checkObstacle(Point snake[], int length, Point obstacles[], int obstacleNum) {
for (int i = 0; i < obstacleNum; i++) {
if (snake[0].x == obstacles[i].x && snake[0].y == obstacles[i].y) {
return true;
}
}
return false;
}
int main() {
initgraph(WIDTH, HEIGHT);
srand(static_cast<unsigned int>(time(nullptr)));
Point snake[100];
snake[0] = {WIDTH / SIZE / 2, HEIGHT / SIZE / 2}; // 初始蛇头位置
int length = 1;
Direction dir = RIGHT; // 初始移动方向
IMAGE imgSnake;
loadimage(&imgSnake, _T("snake.png")); // 加载蛇的图片
IMAGE imgFood;
loadimage(&imgFood, _T("food.png")); // 加载食物的图片
Point food;
generateFood(food, imgFood);
Point obstacles[10];
int obstacleNum = 0;
int difficulty = 1; // 难度,初始为1
while (true) {
cleardevice();
if (kbhit()) {
switch (getch()) {
case 'W':
case 'w':
if (dir != DOWN) {
dir = UP;
}
break;
case 'S':
case 's':
if (dir != UP) {
dir = DOWN;
}
break;
case 'A':
case 'a':
if (dir != RIGHT) {
dir = LEFT;
}
break;
case 'D':
case 'd':
if (dir != LEFT) {
dir = RIGHT;
}
break;
case '1':
difficulty = 1;
break;
case '2':
difficulty = 2;
break;
case '3':
difficulty = 3;
break;
}
}
switch (dir) {
case UP:
snake[0].y--;
break;
case DOWN:
snake[0].y++;
break;
case LEFT:
snake[0].x--;
break;
case RIGHT:
snake[0].x++;
break;
}
if (checkCollision(snake, length, food)) {
length++;
generateFood(food, imgFood);
}
if (checkBoundary(snake, length) || checkObstacle(snake, length, obstacles, obstacleNum)) {
break;
}
drawSnake(snake, length, imgSnake);
setfillcolor(YELLOW);
fillrectangle(food.x * SIZE, food.y * SIZE, (food.x + 1) * SIZE, (food.y + 1) * SIZE);
for (int i = 0; i < obstacleNum; i++) {
setfillcolor(BLUE);
fillrectangle(obstacles[i].x * SIZE, obstacles[i].y * SIZE, (obstacles[i].x + 1) * SIZE, (obstacles[i].y + 1) * SIZE);
}
Sleep(200 / difficulty); // 根据难度控制移动速度
if (length % 5 == 0) { // 每吃5个食物增加一个障碍物
obstacles[obstacleNum] = {rand() % (WIDTH / SIZE), rand() % (HEIGHT / SIZE)};
obstacleNum++;
}
}
closegraph();
return 0;
}
```
在这个示例代码中,我们使用了ege图形库,并加载了蛇和食物的图片。你可以将`snake.png`和`food.png`这两个图片文件放在同一目录下,并将其替换为你自己的图片。
难度选择通过按下数字键1、2、3来进行选择,分别代表简单、中等和困难。难度越高,蛇移动的速度越快。
障碍物的生成是根据蛇吃到一定数量的食物来增加的,每吃到5个食物增加一个障碍物。
你可以根据需要进一步修改和优化代码,添加其他功能和特性。
阅读全文