1.定义蛇、食物的结构体
2.初始化蛇和食物
3.开始游戏
蛇和墙的碰撞
蛇和自身碰撞
蛇和食物碰撞
重新随机食物
蛇身体增长
分数增长
方向键控制
4.游戏结束
---1.定义蛇、食物的结构体
#define WIDTH 60 #define HEIGHT 20 struct BODY { int X; int Y; }; struct Snake { int size; struct BODY body[WIDTH * HEIGHT]; }snake; struct Food { int X; int Y; }food;
---2.初始化蛇、食物、墙
void InitFood() { food.X = rand() % WIDTH; food.Y = rand() % HEIGHT; } void InitSnake() { snake.size = 2; snake.body[0].X = WIDTH / 2; snake.body[0].Y = HEIGHT / 2; snake.body[1].X = WIDTH / 2 - 1; snake.body[1].Y = HEIGHT / 2; }
void InitWall() { for (int i = 0; i <= HEIGHT; i++) { for (int j = 0; j <= WIDTH; j++) { if (i == HEIGHT) { putchar('='); } else if (j == WIDTH) { putchar('='); } else { putchar(' '); } } putchar('\n'); } }
---3.开始游戏
int key = 'D'; // 是否和墙碰撞 while (snake.body[0].X >= 0 && snake.body[0].X < WIDTH && snake.body[0].Y >= 0 && snake.body[0].Y < HEIGHT) { // 显示界面 ShowUi(); // 方向控制 while (_kbhit()) { key = _getch(); } switch (key) { case'D':case'd':dx = 1; dy = 0; break; case'A':case'a':dx = -1; dy = 0; break; case'W':case'w':dx = 0; dy = -1; break; case'S':case's':dx = 0; dy = 1; break; } // 是否和自身碰撞 for (int i = 1; i < snake.size; i++) { if (snake.body[0].X == snake.body[i].X && snake.body[0].Y == snake.body[i].Y) { return; } } // 和食物碰撞 if (snake.body[0].X == food.X && snake.body[0].Y == food.Y) { // 随机新食物 InitFood(); // 身体增长 snake.size++; // 分数增长 score += 10; // 关卡 速度 障碍物 } }
// 将蛇尾巴经过显示的痕迹去掉 lx = snake.body[snake.size - 1].X; ly = snake.body[snake.size - 1].Y; // 蛇更新坐标 for (int i = snake.size - 1; i > 0; i--) { snake.body[i].X = snake.body[i - 1].X; snake.body[i].Y = snake.body[i - 1].Y; } // 更新蛇头 snake.body[0].X += dx; snake.body[0].Y += dy; // 控制蛇移动的速度 if (score > 30) { Sleep(300); } else if(score > 50) { Sleep(100); } else { Sleep(500); }
-------------------------------------------------------------------------------------------------------完整代码---------------------------------------------------------------------------------------------------------------------------------------
// Snakes.h #define WIDTH 60 #define HEIGHT 20 struct BODY { int X; int Y; }; struct Snake { int size; struct BODY body[WIDTH * HEIGHT]; }snake; struct Food { int X; int Y; }food; int score = 0; // 记录蛇末尾坐标 int lx = 0; int ly = 0; // 偏移坐标 int dx = 0; int dy = 0; void InitFood(); void InitSnake(); void ShowUi(); void PlayGame(); void InitWall();
// Snakes.cpp #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <Windows.h> #include "Snakes.h" void InitFood() { food.X = rand() % WIDTH; food.Y = rand() % HEIGHT; } void InitSnake() { snake.size = 2; snake.body[0].X = WIDTH / 2; snake.body[0].Y = HEIGHT / 2; snake.body[1].X = WIDTH / 2 - 1; snake.body[1].Y = HEIGHT / 2; } void ShowUi() { COORD coord; // 更新末尾空格 coord.X = lx; coord.Y = ly; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); putchar(' '); // 显示蛇的位置 for (int i = 0; i < snake.size; i++) { coord.X = snake.body[i].X; coord.Y = snake.body[i].Y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); if (i == 0) { putchar('@'); } else { putchar('*'); } } // 显示食物的位置 coord.X = food.X; coord.Y = food.Y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); printf("#"); } void PlayGame() { int key = 'D'; // 是否和墙碰撞 while (snake.body[0].X >= 0 && snake.body[0].X < WIDTH && snake.body[0].Y >= 0 && snake.body[0].Y < HEIGHT) { // 显示界面 ShowUi(); // 方向控制 while (_kbhit()) { key = _getch(); } switch (key) { case'D':case'd':dx = 1; dy = 0; break; case'A':case'a':dx = -1; dy = 0; break; case'W':case'w':dx = 0; dy = -1; break; case'S':case's':dx = 0; dy = 1; break; } // 是否和自身碰撞 for (int i = 1; i < snake.size; i++) { if (snake.body[0].X == snake.body[i].X && snake.body[0].Y == snake.body[i].Y) { return; } } // 和食物碰撞 if (snake.body[0].X == food.X && snake.body[0].Y == food.Y) { // 随机新食物 InitFood(); // 身体增长 snake.size++; // 分数增长 score += 10; // 关卡 速度 障碍物 } // 蛇更新坐标 lx = snake.body[snake.size - 1].X; ly = snake.body[snake.size - 1].Y; for (int i = snake.size - 1; i > 0; i--) { snake.body[i].X = snake.body[i - 1].X; snake.body[i].Y = snake.body[i - 1].Y; } // 更新蛇头 snake.body[0].X += dx; snake.body[0].Y += dy; if (score > 30) { Sleep(300); } else if(score > 50) { Sleep(100); } else { Sleep(500); } //system("cls"); } } void InitWall() { for (int i = 0; i <= HEIGHT; i++) { for (int j = 0; j <= WIDTH; j++) { if (i == HEIGHT) { putchar('='); } else if (j == WIDTH) { putchar('='); } else { putchar(' '); } } putchar('\n'); } } int main() { /* 1.定义蛇的结构体 2.初始化蛇和食物 3.开始游戏 蛇和墙的碰撞 蛇和自身碰撞 蛇和食物碰撞 重新随机食物 蛇身体增长 分数增长 方向键控制 4.游戏结束 */ srand((size_t)time(NULL)); // 去掉控制台的光标 CONSOLE_CURSOR_INFO cci; cci.dwSize = sizeof(cci); cci.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci); InitWall(); InitFood(); InitSnake(); PlayGame(); //getchar(); return EXIT_SUCCESS; }
标签:body,case,int,void,C语言,贪吃蛇,-----,snake,size From: https://www.cnblogs.com/gnos1s/p/18099506