2025年贪吃蛇(C++)

贪吃蛇(C++)一 项目思路 该项目用于计算机软件课设 贪吃蛇要解决的问题有 蛇身的移动 事物的随机生成 蛇头与食物重合 蛇吃食物身体的变长 蛇身移动的处理可以改变蛇首元素 蛇身用数组存储 的坐标 剩下的依次前移即可 蛇身的变长只需尾部添上一个数组元素即可 二 源代码 include lt

大家好,我是讯享网,很高兴认识大家。

一、项目思路

该项目用于计算机软件课设。贪吃蛇要解决的问题有:蛇身的移动,事物的随机生成,蛇头与食物重合,蛇吃食物身体的变长。蛇身移动的处理可以改变蛇首元素(蛇身用数组存储)的坐标,剩下的依次前移即可。蛇身的变长只需尾部添上一个数组元素即可。

二、源代码


讯享网

#include<graphics.h> #include<Windows.h> #include<conio.h> #include<ctime> #include<vector> #include<cstdlib> using namespace std; class Snake { public: Snake(); bool check();//检测生成的食物是否和蛇重合了 void SnakeMove(); void startup(); void clean(); void show(); void UpdateWithoutInput(); void UpdateWithInput(); void gameover(); private: int High; int Width; int Background; int dir;//方向 int score; int speed;//画面更新速度,动态难度 int food_x, food_y; vector<int> snake_x, snake_y; //蛇的每一节的坐标 }; Snake::Snake() { High = 720; Width = 1280; Background = LIGHTGRAY;//easyx的颜色宏 dir = 5;//方向参考1235键位,类似wasd score = 0; speed = 300; food_x = rand() % (Width - 40) + 21; //限定食物出现在屏幕范围内 food_y = rand() % (High - 40) + 21; snake_x.resize(4), snake_y.resize(4);//蛇的初始长度为4 } bool Snake::check() { for (int i = 0; i < snake_x.size(); i++) { if ((food_x >= snake_x[i] - 40 && food_x <= snake_x[i] + 40) && (food_y >= snake_y[i] - 40 && food_y <= snake_y[i] + 40)) return false; } return true; } void Snake::SnakeMove() { srand((unsigned int)(time)(NULL)); //如果蛇头的坐标和食物的坐标有一定程度的重合,那么在蛇尾增加新的坐标 if (food_x >= snake_x[0] - 20 && food_x <= snake_x[0] + 20 && food_y >= snake_y[0] - 20 && food_y <= snake_y[0] + 20) { if (dir == 1) { snake_x.push_back(snake_x[snake_x.size() - 1] + 40); snake_y.push_back(snake_y[snake_y.size() - 1]); } else if (dir == 2) { snake_x.push_back(snake_x[snake_x.size() - 1]); snake_y.push_back(snake_y[snake_y.size() - 1] - 40); } else if (dir == 3) { snake_x.push_back(snake_x[snake_x.size() - 1] - 40); snake_y.push_back(snake_y[snake_y.size() - 1]); } else if (dir == 5) { snake_x.push_back(snake_x[snake_x.size() - 1]); snake_y.push_back(snake_y[snake_y.size() - 1] + 40); } score++;//加分 if (speed >= 150) //调整速度 { speed -= 5 * score; } //擦除旧食物,更新食物的坐标 setlinecolor(Background); setfillcolor(Background); fillcircle(food_x, food_y, 20); food_x = rand() % (Width - 40) + 21; food_y = rand() % (High - 40) + 21; //如果生成的食物坐标和蛇重合了,再更新食物的坐标,check是检测是否重合的函数。 while (!check()) { food_x = rand() % (Width - 40) + 21; food_y = rand() % (High - 40) + 21; } } //蛇移动,通过从后往前迭代更新坐标,形成移动 for (int i = snake_x.size() - 1; i > 0; i--) { snake_x[i] = snake_x[i - 1]; snake_y[i] = snake_y[i - 1]; } //头节点根据方向特殊处理 if (dir == 1) { snake_x[0] = snake_x[1] - 40; snake_y[0] = snake_y[1]; } else if (dir == 2) { snake_x[0] = snake_x[1]; snake_y[0] = snake_y[1] + 40; } else if (dir == 3) { snake_x[0] = snake_x[1] + 40; snake_y[0] = snake_y[1]; } else if (dir == 5) { snake_x[0] = snake_x[1]; snake_y[0] = snake_y[1] - 40; } //检测是否撞墙或者咬到自己了 for (unsigned i = 1; i < snake_x.size(); i++) { if ((snake_x[0] >= snake_x[i] - 20 && snake_x[0] <= snake_x[i] + 20 && snake_y[0] >= snake_y[i] - 20 && snake_y[0] <= snake_y[i] + 20) || ((snake_x[0] - 20 <= 0) || (snake_x[0] + 20 >= 1280) || (snake_y[0] - 20 <= 0) || (snake_y[0] + 20 >= 720))) {//停留三秒退出游戏 Sleep(3000); exit(0); } } } void Snake::startup() { srand((unsigned int)(time)(NULL)); initgraph(Width, High); setbkcolor(Background); cleardevice(); for (int i = 0; i < 4; i++) { snake_x[i] = Width / 2 - 40 * i; snake_y[i] = High / 2; } BeginBatchDraw(); } void Snake::clean() { setlinecolor(Background);//设置线的颜色 setfillcolor(Background);//设置填充色 for (unsigned i = 0; i < snake_x.size(); i++) { fillcircle(snake_x[i], snake_y[i], 20);//画圆 } } void Snake::show() { for (unsigned i = 0; i < snake_x.size(); i++) { if (i == 0) { setfillcolor(LIGHTMAGENTA); fillcircle(snake_x[i], snake_y[i], 20); } else { setfillcolor(LIGHTRED); fillcircle(snake_x[i], snake_y[i], 20); } } FlushBatchDraw();//与BeginBatchDraw配合使用 setfillcolor(YELLOW); //以下均为字体设置与字体显示,用于显示分数 TCHAR s[5]; fillcircle(food_x, food_y, 20); settextstyle(60, 0, _T("宋体")); settextcolor(RGB(25, 32, 94)); _stprintf_s(s, 5, _T("%d"), score); outtextxy(430, 620, _T("Score:")); outtextxy(640, 620, s); Sleep(speed);//用sleep控制速度,speed越小蛇动的越快 } void Snake::UpdateWithoutInput() { SnakeMove(); } void Snake::UpdateWithInput() { char input; if (_kbhit()) { input = _getch(); if ((input == 'a' || input == '1') && dir != 3) { dir = 1; SnakeMove(); } else if ((input == 's' || input == '2') && dir != 5) { dir = 2; SnakeMove(); } else if ((input == 'w' || input == '5') && dir != 2) { dir = 5; SnakeMove(); } else if ((input == 'd' || input == '3') && dir != 1) { dir = 3; SnakeMove(); } if (input == 27) { input = 'n'; while (1) { input = _getch(); if (input == 27) break; } } } } void Snake::gameover() { EndBatchDraw(); closegraph(); } int main() { Snake my_snake; my_snake.startup(); while (1) { my_snake.clean(); my_snake.UpdateWithoutInput(); my_snake.UpdateWithInput(); my_snake.show(); } my_snake.gameover(); return 0; } 

讯享网

三、项目总结

该项目较为简单,无复杂算法。

小讯
上一篇 2025-03-13 19:13
下一篇 2025-04-10 11:11

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/127730.html