C语言实现贪吃蛇游戏(命令行)
家电修理 2023-07-16 19:16www.caominkang.com电器维修
这是一个纯C语言写的贪吃蛇游戏,供大家参考,具体内容如下
#include#include #include #include #include #define SNAKE_LENGTH 100//定义蛇的最大长度 #define SCREEN_WIDETH 80 #define SCREEN_HEIGHT 30 //定义每一节蛇的坐标 struct coor{ int x; int y; }; //枚举方向 enum CH { right = VK_RIGHT, left = VK_LEFT, up = VK_UP, don = VK_DOWN }; //定义蛇的属性 struct snake{ int len;//当前蛇的长度 struct coor coord[SNAKE_LENGTH];//每一节蛇的坐标 enum CH CH;//定义蛇的方向 int SPEED; int flag;//定义蛇的状态 1表示存活 0表示死亡 }snake; //光标移动函数 void gotoxy(int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } //初始化游戏界面 void init_sence() { //初始化上下墙 for (int i = 0; i < SCREEN_WIDETH; i += 2) { gotoxy(i,0); printf("■"); gotoxy(i, SCREEN_HEIGHT); printf("■"); } //初始化左右墙 for (int i = 0; i <=SCREEN_HEIGHT; i++) { gotoxy(0, i); printf("■"); gotoxy(SCREEN_WIDETH,i); printf("■"); } //打印提示信息 gotoxy(SCREEN_WIDETH + 5, 2); printf("tt贪吃蛇"); gotoxy(SCREEN_WIDETH + 5, 6); printf("2018//12//1"); gotoxy(SCREEN_WIDETH + 5, 8); printf("作者小豆芽"); gotoxy(SCREEN_WIDETH + 5, 10); printf("F1:加速tF2减速"); gotoxy(SCREEN_WIDETH + 5, 12); printf("CTRL:继续t空格暂停"); gotoxy(SCREEN_WIDETH + 5, 14); printf("ESC:退出游戏"); gotoxy(SCREEN_WIDETH + 5, 28); printf("建议QQ2862841130:::"); } struct foodcoord { int x; int y; int flag;//定义食物的状态 }food; //这是c程序 #include"snake.h" //蛇的移动 void move_snake(); //画出蛇 void dra_snake(); //产生食物 void creatfood(); //判断蛇是否吃到食物 void eatfood(); //判断蛇是否死掉 void SnakeState(); int main() { //设置窗口大小 system("mode con cols=110 lines=31"); //设置标题 SetConsoleTitleA("贪吃蛇"); //初始化蛇 begin: snake.CH = VK_RIGHT;//初始化方向 snake.len = 5; //初始化长度 snake.SPEED = 300;//初始化蛇的移动速度 snake.coord[1].x = SCREEN_WIDETH / 2;//初始化蛇头的坐标 snake.coord[1].y = SCREEN_HEIGHT / 2; snake.coord[2].x = SCREEN_WIDETH / 2-2;//初始化蛇头的坐标 snake.coord[2].y = SCREEN_HEIGHT / 2; snake.coord[3].x = SCREEN_WIDETH / 2-4;//初始化蛇头的坐标 snake.coord[3].y = SCREEN_HEIGHT / 2; //初始化食物状态 food.flag = 1;//1表示吃到食物 0表示没有吃到食物 //初始化食物状态 snake.flag = 1;//1活 0死 init_sence();//初始化游戏界面 hile (1) { dra_snake();//画蛇 Sleep(snake.SPEED);//蛇的移动速度 move_snake();//移动蛇 if(food.flag) creatfood();//产生食物 eatfood();//判断是否吃到食物 SnakeState();//判断蛇是否死亡 if (!snake.flag)break; } system("cls"); gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4); printf(" GAME OVER!!!"); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2); printf("你的得分是ttt%d ",snake.len-1); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4); printf("我不服再来tttCTRL "); gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6); printf("算了垃圾游戏毁我青春:ttESC"); hile (1) { if (GetAsyncKeyState(VK_CONTROL)) { system("cls"); goto begin; } else if (GetAsyncKeyState(VK_ESCAPE)) return 0; } } //蛇的移动 void move_snake() { //判断是否有按键操作 if (GetAsyncKeyState(up)) { if(snake.CH!=don)snake.CH = up; } else if (GetAsyncKeyState(don)) { if (snake.CH != up)snake.CH = don; } else if (GetAsyncKeyState(right)) { if (snake.CH != left)snake.CH = right; } else if (GetAsyncKeyState(left)) { if (snake.CH != right)snake.CH = left; } else if (GetAsyncKeyState(VK_F1)) { if(snake.SPEED>=100)snake.SPEED -= 50; } else if (GetAsyncKeyState(VK_F2)) { if (snake.SPEED <= 3000)snake.SPEED += 100; } //根据检测到的方向改变蛇头的位置 sitch (snake.CH) { case right:snake.coord[1].x += 2; break; case left:snake.coord[1].x -= 2; break; case up:snake.coord[1].y -= 1; break; case don:snake.coord[1].y += 1; break; } } //画出蛇 void dra_snake() { //画出蛇头 gotoxy(snake.coord[1].x, snake.coord[1].y); printf("□"); //画出蛇身,直接一个for循环实现 for (int i = 2; i <= snake.len; i++) { gotoxy(snake.coord[i].x, snake.coord[i].y); printf("□"); } //擦掉尾巴 gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y); printf(" "); //遍历每一节蛇 for (int i = snake.len; i >1; i--) { snake.coord[i].x = snake.coord[i - 1].x; snake.coord[i].y = snake.coord[i - 1].y; } gotoxy(0, 0); printf("■"); gotoxy(85, 25); printf("得分:%d ", snake.len-1); } //产生食物 void creatfood() { //随机种子生成 srand((unsigned)time(NULL)); if(food.flag) hile (1) { food.x = rand() % 80; food.y = rand() % 30; if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30) { int flag = 0; //判断产生的食物可不可能在蛇的身体上 for (int i = 1; i <= snake.len; i++) { if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y) { flag = 1; break; } } if (flag)continue; //绘制食物 else { gotoxy(food.x, food.y); printf("⊙"); food.flag = 0; break; } } } food.flag = 0; } //判断蛇是否吃到食物 void eatfood() { //只需要判断蛇头是否与食物重合 if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y) { snake.len+=1; food.flag = 1; } } //判断蛇是否死掉 void SnakeState() { if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29) snake.flag = 0; for (int i = 2; i <= snake.len; i++) { if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y) snake.flag = 0; } }
更多有趣的经典小游戏实现专题,分享给大家
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
Javascript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
空调维修
- 温岭冰箱全国统一服务热线-全国统一人工【7X2
- 荆州速热热水器维修(荆州热水器维修)
- 昆山热水器故障码5ER-昆山热水器故障码26
- 温岭洗衣机24小时服务电话—(7X24小时)登记报
- 统帅热水器售后维修服务电话—— (7X24小时)登
- 阳江中央空调统一电话热线-阳江空调官方售后电
- 乌鲁木齐阳春燃气灶厂家服务热线
- 珠海许昌集成灶售后服务电话-全国统一人工【
- 乌鲁木齐中央空调维修服务专线-乌鲁木齐中央空
- 新沂热水器故障电话码维修-新沂热水器常见故障
- 诸城壁挂炉24小时服务热线电话
- 靖江空调24小时服务电话-——售后维修中心电话
- 空调室外滴水管维修(空调室外排水管维修)
- 九江壁挂炉400全国服务电话-(7X24小时)登记报修
- 热水器故障码f.22怎么解决-热水器f0故障解决方法
- 营口热水器售后维修服务电话—— 全国统一人工