C/C++小游戏,贪吃蛇
游戏效果图
源代码
#include <stdio.h> #include <graphics.h> #include <easyx.h> #include <conio.h> /* 贪吃蛇,小游戏 */ #define HEIGHT 480 #define WIDTH 640 #define SNAKE_SIZE 500 //蛇的最大节数 #define DEFAULT_LOW_SPEED 5 // enum Direction { LEFT, RIGHT, UP, DOWN }; //方向枚举 Direction snake_direction = RIGHT;//默认蛇向右移动 //定义蛇的结构 struct Snake { int size; //初始节数 int speed; //蛇的速度 Direction direction; //蛇的方向 POINT coor[SNAKE_SIZE]; //蛇的坐标 } snake; //食物结构属性 struct Food { int x; int y; int radius; //食物半径 bool flag; //标记是否被吃 DWORD color; //食物颜色 }food; void game_init() { snake.size = 3; snake.speed = 10; snake.direction = snake_direction; for (int i = 0; i < snake.size; i++) { snake.coor[i].x = 40 - i * snake.speed; snake.coor[i].y = 10; } //初始化窗口 initgraph(WIDTH, HEIGHT); //设置随机数种子 srand(GetTickCount()); //初始化食物 food.x = rand() % WIDTH; food.y = rand() % HEIGHT; food.color = RGB(rand() % 256, rand() % 256, rand() % 256); food.radius = rand() % 10 + 5; food.flag = true; } void snake_move() { //记录移动前的坐标 for (int i = snake.size - 1; i > 0; i--) { snake.coor[i] = snake.coor[i - 1]; } switch (snake.direction) { case UP: snake.coor[0].y -= snake.speed; if (snake.coor[0].y + 10 <= 0) { snake.coor[0].y = HEIGHT; } break; case DOWN: snake.coor[0].y += snake.speed; if (snake.coor[0].y - 10 >= HEIGHT) { snake.coor[0].y = 0; } break; case RIGHT: snake.coor[0].x += snake.speed; if (snake.coor[0].x + 10 >= WIDTH) { snake.coor[0].x = 0; } break; case LEFT: snake.coor[0].x -= snake.speed; if (snake.coor[0].x - 10 <= 0) { snake.coor[0].x = WIDTH; } break; } } void key_control() { //_kbhit() ,非阻塞监听键盘按键,有按下返回真 if (_kbhit()) { //72 80 75 77 上下左右方向键码 int keycode = _getch(); switch (keycode) { case 'w': case 'W': case 72: if (snake.direction != DOWN) snake.direction = UP; break; case 's': case 'S': case 80: if (snake.direction != UP) snake.direction = DOWN; break; case 'a': case 'A': case 75: if (snake.direction != RIGHT) snake.direction = LEFT; break; case 'd': case 'D': case 77: if (snake.direction != LEFT) snake.direction = RIGHT; break; //小键盘+- 调节蛇的速度 + = 107, - = 109 case 45: snake.speed++; break; case 43: if (snake.speed > DEFAULT_LOW_SPEED) { snake.speed++; } break; //空格键 32 暂停控制 case 32: while (true) { keycode = _getch(); if (keycode == 32) { break; } } break; } } } void game_draw() { BeginBatchDraw(); //双缓冲绘图 setbkcolor(RGB(192, 192, 192)); //设置背景颜色 cleardevice(); //清空设备上下文 setfillcolor(RGB(128, 42, 42)); //绘制蛇 for (int i = 0; i < snake.size; i++) { solidcircle(snake.coor[i].x, snake.coor[i].y, 5); } //绘制食物 if (food.flag) { solidcircle(food.x, food.y, food.radius); } EndBatchDraw(); } void eat_food() { int moveX = abs(snake.coor[0].x - food.x); int moveY = abs(snake.coor[0].y - food.y); if (food.flag && moveX < food.radius && moveY < food.radius) { food.flag = false; snake.size++; } if (!food.flag) { food.x = rand() % WIDTH; food.y = rand() % HEIGHT; food.color = RGB(rand() % 256, rand() % 256, rand() % 256); food.radius = rand() % 10 + 5; food.flag = true; } } int main() { game_init(); while (1) { game_draw(); snake_move(); key_control(); eat_food(); Sleep(100); } return 0; }