贪吃蛇双人小游戏,每局游戏两分钟,死亡则直接失败,若时间结束,则分高者获胜。
#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <time.h>#include<stdbool.h>#include <conio.h>#define snakesize 100#define mapwidth 118#define mapheight 29struct { //保存食物坐标int x;int y;}food;struct {int len;int x[snakesize];int y[snakesize];}snake;struct {int len;int x[snakesize];int y[snakesize];}snake1;char key = '8';//初始方向向上char key1 = 'w';int changeflag = 0 , changeflag1 = 0;int speed=150 , sorce = 0 , sorce1 = 0 , c=0 , min=2;void gotoxy(int x, int y)//移动光标到指定位置{coord coord;coord.x = x;coord.y = y;tconsolecursorposition(getstdhandle(std_output_handle), coord);}void map()//打印边框和两条蛇的起始位置{for (int i = 0; i <= mapwidth; i += 2)//打印最上面和最下面两横边框{gotoxy(i, 0);printf("■");gotoxy(i, mapheight);printf("■");}for (int i = 1; i < mapheight; i++)//打印最左面和最右面{gotoxy(0, i);printf("■");gotoxy(mapwidth, i);printf("■");}while (1)//打印一个食物{srand((unsigned int)time(null));food.x = rand() % (mapwidth - 4) + 2;food.y = rand() % (mapheight - 2) + 1;if (food.x % 2 == 0)break;}gotoxy(food.x, food.y);printf("");snake.len = snake1.len = 4;//给两条蛇的长度赋初值snake.x[0] = mapwidth / 2 + 31;//然后分别打印两条蛇身部分snake.y[0] = mapheight / 2;snake1.x[0] = mapwidth / 2 -31;snake1.y[0] = mapheight / 2;gotoxy(snake.x[0], snake.y[0]);printf("■");gotoxy(snake1.x[0], snake1.y[0]);printf("●");for (int i = 1; i < snake.len; i++){snake.x[i] = snake.x[i - 1];snake.y[i] = snake.y[i - 1]+1;gotoxy(snake.x[i], snake.y[i]);printf("■");snake1.x[i] = snake1.x[i - 1];snake1.y[i] = snake1.y[i - 1]+1;gotoxy(snake1.x[i], snake1.y[i]);printf("●");}gotoxy(mapwidth , 0);//把光标移走return;}void operation()//操作函数{char pre_key = key , pre_key1 = key1 , s;//保存两条蛇上一次的方向if (_kbhit()){s = getch();if(s=='w'||s=='s'||s=='a'||s=='d'||s=='w'||s=='s'||s=='a'||s=='d')key1=s;el if(s=='8'||s=='5'||s=='4'||s=='6')key=s;}if (changeflag == 0)//没吃到食物{gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);printf(" ");//在蛇尾处输出空格即擦去蛇尾}if (changeflag1 == 0){gotoxy(snake1.x[snake1.len - 1], snake1.y[snake1.len - 1]);printf(" ");//在蛇尾处输出空格即擦去蛇尾}//将蛇的每一节依次向前移动一节(蛇头除外)for (int i = snake.len - 1; i > 0; i--){snake.x[i] = snake.x[i - 1];snake.y[i] = snake.y[i - 1];}for (int i = snake1.len - 1; i > 0; i--){snake1.x[i] = snake1.x[i - 1];snake1.y[i] = snake1.y[i - 1];}//蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走//如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向if (pre_key == '8' && key == '5')key = '8';if (pre_key == '5' && key == '8')key = '5';if (pre_key == '4' && key == '6')key = '4';if (pre_key == '6' && key == '4')key = '6';if (pre_key1 == 'w' && key1 == 's')key1 = 'w';if (pre_key1 == 's' && key1 == 'w')key1 = 's';if (pre_key1 == 'a' && key1 == 'd')key1 = 'a';if (pre_key1 == 'd' && key1 == 'a')key1 = 'd';//判断蛇头应该往哪个方向移动switch (key){ca '4':snake.x[0] -= 2;//往左break;ca '6':snake.x[0] += 2;//往右break;ca '8':snake.y[0]--;//往上break;ca '5':snake.y[0]++;//往下break;}gotoxy(snake.x[0], snake.y[0]);printf("■");changeflag = 0;switch (key1){ca 'a':ca 'a':snake1.x[0] -= 2;//往左break;ca 'd':ca 'd':snake1.x[0] += 2;//往右break;ca 'w':ca 'w':snake1.y[0]--;//往上break;ca 's':ca 's':snake1.y[0]++;//往下break;}gotoxy(snake1.x[0], snake1.y[0]);printf("●");changeflag1 = 0;gotoxy(mapwidth, 0);return;}void createfood(){if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物{//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物while (1){int a = 1 , b=1;srand((unsigned int)time(null));food.x = rand() % (mapwidth - 4) + 2;food.y = rand() % (mapheight - 2) + 1;//随机生成的食物不能在蛇的身体上for (int i = 0; i < snake.len; i++){if (snake.x[i] == food.x && snake.y[i] == food.y){a = 0;break;}}for (int i = 0; i < snake1.len; i++){if (snake1.x[i] == food.x && snake1.y[i] == food.y){b = 0;break;}}//随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成if (a==1&&兄弟难当歌词b==1 && food.x % 2 == 0)break;}//绘制食物gotoxy(food.x, food.y);printf("");snake.len++;//吃到食物,蛇身长度加1sorce += 10;speed -= 5;//随着吃的食物越来越多,速度会越来越快changeflag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果}return;}void createfood1(){if (snake1.x[0] == food.x && snake1.y[0] == food.y)//蛇头碰到食物{//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物while (1){int a = 1 , b=1;srand((unsigned int)time(null));food.x = rand() % (mapwidth - 4) + 2;food.y = rand() % (mapheight - 2) + 1;//随机今年国庆生成的食物不能在蛇的身体上for (int i = 0; i < snake.len; i++){if (snake.x[i] == food.x && snake.y[i] == food.y){a = 0;break;}}for (int i = 0; i < snake1.len; i++){if (snake1.x[i] == food.x && snake1.y[i] == food.y){b = 0;break;}}//随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成if (a==1&&b==1&& food.x % 2 == 0)break;}//绘制食物gotoxy(food.x, food.y);printf("");snake1.len++;//吃到食物,蛇身长度加1sorce1 += 10;speed -= 5;//随着吃的食物越来越多,速度会越来越快changeflag1 = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果}return;}bool check(){//蛇头碰到上下边界,游戏结束if (snake.y[0] == 0 || snake.y[0] == mapheight)return true;//蛇头碰到左右边界,游戏结束if (snake.x[0] == 0 || s两会报告nake.x[0] == mapwidth)return true;//蛇头碰到蛇身,游戏结束for (int i = 1; i < snake.len; i++){if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])return true;}for (int i = 0; i < snake1.len; i++){if(snake1.x[i] == snake.x[0]&&snake1.y[i] == snake.y[0])return true;}return fal;}bool check1(){//蛇头碰到上下边界,游戏结束if (snake1.y[0] == 0 || snake1.y[0] == mapheight)return true;//蛇头碰到左右边界,游戏结束if (snake1.x[0] == 0 || snake1.x[0] == mapwidth)return true;//蛇头碰到蛇身,游戏结束for (int i = 1; i < snake1.len; i++){if (snake1.x[i] == snake1.x[0] && snake1.y[i] == snake1.y[0])return true;}for (int i = 0; i < snake.len; i++){if (snake.x[i] == snake1.x[0] && snake.y[i] == snake1.y[0])return true;}return fal;}void menu ()//打印菜单界面{printf("\n\n\n\n\t\t\t\t ╔═══════════════════════════════════════╗\n");printf("\t\t\t\t ║ ║\n");printf("\t\t\t\t ║ 欢迎来到贪吃蛇 ║\n");printf("\t\t\t\t ║ ║\n");printf("\t\t\t\t ║ ║\n");printf("\t\t\t\t ║ ┏━━┓ ┏━━┓ ┏━━┓ ║\n");printf("\t\t\t\t ║ 开始:┃ 1┃ 规则:┃ 2┃ 退出:┃ 3┃ ║\n");printf("\t\t\t\t ║ ┗━━┛ ┗━━┛ ┗━━┛ ║\n");printf("\t\t\t\t ║ ║\n");printf("\t\t\t\t ║ ║\n");printf("\t\t\t\t ╚═══════════════════════════════════════╝\n");switch(getch()){ca '1':system("cls");start();break;ca '2':system("cls");rule();menu();break;ca '3':exit(0);break;default:system("cls");printf("error");menu();}}void rule (){system("cls");//清屏printf("\t╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n");printf("\t║本游戏玩家一(左侧)通过按键w、s、a、d(不区分大小写)四个键分别控制snake1上移、下移、左移和右移。║\n");printf("\t║玩家二(右侧)过按键8、5、4、6 四个键分别控制snake2上移、下移、左移和右移。 ║\n");printf("\t║每局游戏两分钟,死亡则直接失败,若时间结束,则分高者获胜。 ║\n");printf("\t╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n");system("pau");//暂停system("cls");//清屏}void start(){time_t time_c = 0;time_t old_c = 0;map();system("pau");time(&time_c); //获取时间old_c = time_c; //保存时间while (!check()&&!check1()){operation();createfood();createfood1();sleep(speed);time(&time_c); //获取秒数保存到time_t变量if(time_c != old_c) //如果秒数改变(计时达到1秒){old_c = time_c; //更新旧的秒数if(c > 0)c--; //计时秒数减1el{c = 59; //如果原秒数为0,则变为59min--; //计时分钟减1}}if(min==0&&c==0)break;if(c>=8)printf("\a");gotoxy(4,1);printf("倒计时——%02d:%02d\r",min, c);}system("cls");printf("\n\n\t\t\tgame over!\n");system("pau");}void over (){system("cls");printf("\n\n\n\n\t\t\t\t╔═══════════════════════════════════════════╗\n");printf("\t\t\t\t║ ║\n");printf("\t\t\t\t║ 游戏结束 ║\n");printf("\t\t\t\t║ ║\n");printf("\t\t\t\t║ ║\n");if(check()&&!check1())printf("\t\t\t\t║ 玩家一(左侧)获胜,玩家二(右侧)失败。 ║\n");el if(!check()&&check1())printf("\t\t\t\t║ 玩家一(左侧)失败,玩家二(右侧)获胜。 ║\n");el{printf("\t\t\t\t║ 玩家一本次游戏得分为:%4d ║\n", sorce1);printf("\t\t\t\t║ 玩家二本次游戏得分为:%4d ║\n", sorce);if(sorce>sorce1)printf("\t\t\t\t║ 玩家一(左侧)失败,玩家二(右侧)获胜。 ║\n");el if(sorce1>sorce)printf("\t\t\t\t║ 玩家一(左侧最新潮流网名)获胜,玩家二(右侧)失败。 ║\n");elprintf("\t\t\t\t║ 恭喜你们平局了! ║\n");}printf("\t\t\t\t║ ║\n");printf("\t\t\t\t║ ║\n");printf("\t\t\t\t╚═══════════════════════════════════════════╝\n");}int main (){system("title 双人贪吃蛇");system("color f0");menu();over();return 0;}[点击并拖拽以移动]
平津战役
到此这篇关于c语言实现双人贪吃蛇游戏实例代码的文章就介绍到这了,更多相关c语言贪吃蛇游戏内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 08:12:25,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/dac047ca5662c3be7112fae247d26c35.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C语言实现双人贪吃蛇游戏实例代码.doc
本文 PDF 下载地址:C语言实现双人贪吃蛇游戏实例代码.pdf
留言与评论(共有 0 条评论) |