日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言编写双人贪吃蛇游戏

發布時間:2023/12/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言编写双人贪吃蛇游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
雙人貪吃蛇游戲

運行環境 visual stdio 2017


/* 貪吃蛇游戲 //雙人版本 玩家2使用 W 上 S下 A左 D右邊玩家1使用 箭頭上 箭頭下 箭頭左 箭頭右 */ /* 作者:李金虎 */ /* 時間:2018-04-06 */ /* QQ:1910084514 */#include<stdio.h> #include<windows.h> #include<conio.h> #include<time.h> int a = 1; int a2 = 1; int snack_life=1;//1代表游戲繼續 0代表游戲結束 int snack_life2 = 1;//1代表游戲繼續 0代表游戲結束 玩家2 int count = 0;//得分 int count2 = 0;//得分 玩家2 int speed=0;//貪吃蛇的速度 int who_flag=0;//判斷是誰吃了標志位 0為玩家1吃了 1為玩家2吃了 void gotoxy(int x, int y)//光標移動函數,光標想在哪里就在哪里 {COORD coord = { x , y };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }struct food //食物結構體 {int x;int y;int life; }Food;struct snack //蛇結構體 {int x;int y;struct snack *next; } *snack_body, *snack_body2;void color(short x) //自定義函根據參數改變顏色 {if (x >= 0 && x <= 15)//參數在0-15的范圍顏色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一個參數,改變字體顏色 else//默認的顏色白色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); }void product_food() //產生食物坐標函數 {remake:struct snack *p;//定義一個臨時指針變量 指向snack_bodystruct snack *p2;//定義一個臨時指針變量 指向snack_bodyp = snack_body;p2 = snack_body2;srand(time(NULL));Food.x = (rand() % 16 + 4)*2;Food.y = rand() % 17 + 2;Food.life = 1;while (p != NULL) //防止食物生成在蛇所處的位置,如果生成在蛇身體的位置,則重新生成食物{if (Food.x == p->x&&p->y == Food.y)goto remake;//跳轉回去,重新產生一個食物p = p->next;}//while (p2 != NULL) //防止食物生成在蛇所處的位置,如果生成在蛇身體的位置,則重新生成食物//{// if (Food.x == p2->x&&p2->y == Food.y)// goto remake;//跳轉回去,重新產生一個食物// p2 = p2->next;//}gotoxy(Food.x, Food.y);printf("G"); }void introduce() {system("cls");gotoxy(10, 2);printf("歡迎來到貪吃蛇的世界 ,這次帶來的是一個雙人貪吃蛇游戲\n");gotoxy(10, 4);printf("我是李金虎,此代碼的作者\n");gotoxy(10, 6);printf("完成時間: 2018-04-09 , 歷時7小時\n");gotoxy(10, 8);printf("初次編寫貪吃蛇,代碼風格以及邏輯有很多的不足\n");gotoxy(10, 10);printf("如果有些代碼不懂的小伙伴可以加我QQ1910084514私信我,樂意為您效勞\n");gotoxy(10, 12);printf("希望您游戲愉快\n");while (speed<=0||speed>=21){gotoxy(10, 14);printf("請輸入游戲的級別 最快的為20級,最慢為1級,請輸入一個整數:");scanf_s("%d", &speed);}system("cls");gotoxy(10, 16);printf("按任意鍵盤,確認開始游戲");system("pause");system("cls");}void map() //打印地圖函數 {for (int i = 0; i < 20; i++){gotoxy(i*2, 0);printf("〇");gotoxy(i * 2, 19);printf("〇");gotoxy(0, i);printf("〇");gotoxy(40, i);printf("〇");}gotoxy(50, 7);printf("玩家1得分:%d", count);gotoxy(50, 8);printf("玩家2得分:%d", count);gotoxy(50, 9);printf("游戲難度:%d 級",speed);gotoxy(50, 10);printf("-----貪吃蛇小游戲-----");gotoxy(50, 11);printf("--作者--李金虎"); }void init_snack()//蛇初始化函數 {struct snack *p;struct snack *p2;color(3);p2 = (struct snack *)(malloc(sizeof(struct snack)));p2->x = 30;p2->y = 10;snack_body2 = p2;p2->next = (struct snack *)(malloc(sizeof(struct snack)));p2 = p2->next;p2->x = 30;p2->y = 9;p2->next = (struct snack *)(malloc(sizeof(struct snack)));p2 = p2->next;p2->x = 30;p2->y = 8;p2->next = (struct snack *)(malloc(sizeof(struct snack)));p2 = p2->next;p2->x = 30;p2->y = 7;p2->next = NULL;p2 = snack_body2;while (p2 != NULL){gotoxy(p2->x, p2->y);printf("G");p2 = p2->next;}color(16);color(4);p= (struct snack *)(malloc(sizeof(struct snack)));p->x = 10;p->y = 10;snack_body = p;p->next = (struct snack *)(malloc(sizeof(struct snack)));p = p->next;p->x = 10;p->y = 9;p->next = (struct snack *)(malloc(sizeof(struct snack)));p = p->next;p->x = 10;p->y = 8;p->next = (struct snack *)(malloc(sizeof(struct snack)));p = p->next;p->x = 10;p->y = 7;p->next = NULL;p= snack_body;while (p != NULL){gotoxy(p->x, p->y);printf("G");p = p->next;}color(16); }int control(void)//蛇的控制函數 {int down;int left;//左int right;//右int up;down = GetKeyState(VK_DOWN);//獲取上箭頭鍵狀態 if (down < 0&& a!=2 ) //如果上箭頭鍵被按下 a!=2 的作用在于復制頭朝下的時候蛇還會往上走{a = 1;}up = GetKeyState(VK_UP);//獲取下箭頭鍵狀態 if (up < 0 && a != 1) //如果下箭頭鍵被按下 {a = 2;}left = GetKeyState(VK_LEFT);//獲取左箭頭鍵狀態 if (left < 0 && a != 4) //如果左箭頭鍵被按下 {a = 3;}right = GetKeyState(VK_RIGHT);//獲取右箭頭鍵狀態 if (right < 0 && a != 3) //如果右箭頭鍵被按下 {a = 4;}return a; } int control2(void)//蛇的控制函數 玩家2 {int down;int left;//左int right;//右int up;down = GetKeyState('S');//獲取箭下頭鍵狀態 if (down < 0 && a2 != 2) //如果上箭頭鍵被按下 a!=2 的作用在于復制頭朝下的時候蛇還會往上走{a2 = 1;}up = GetKeyState('W');//獲取上箭頭鍵狀態 if (up < 0 && a2 != 1) //如果下箭頭鍵被按下 {a2 = 2;}left = GetKeyState('A');//獲取左箭頭鍵狀態 if (left < 0 && a2 != 4) //如果左箭頭鍵被按下 {a2 = 3;}right = GetKeyState('D');//獲取右箭頭鍵狀態 if (right < 0 && a2 != 3) //如果右箭頭鍵被按下 {a2 = 4;}return a2; }int is_eat_itself(int x, int y) {struct snack *p;p = snack_body;p = p->next;//跳過蛇頭while (p != NULL){if (x == p->x && y == p->y)//如果蛇咬到自己{return 1;}p = p->next;}return 0; } int is_eat_itself2(int x, int y) {struct snack *p;p = snack_body2;p = p->next;//跳過蛇頭while (p != NULL){if (x == p->x && y == p->y)//如果蛇咬到自己{return 1;}p = p->next;}return 0; }void snack_move(int direction )//蛇移動函數 direction為方向 direction的值是1向上 2向下 3向左 4向右 {int y=0;int x=0;struct snack *p4=NULL;struct snack *p1=NULL;struct snack *p2 = NULL;p4 = snack_body;x = p4->x;//臨時儲存當前的蛇頭坐標y = p4->y;color(4);if (1 == direction){y=y + (1);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body;snack_body = p1;p2 = snack_body;if ( !((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself(x,y))//判斷是否撞墻 滿足條件就是沒撞墻{gotoxy(snack_body->x, snack_body->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 1;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life = 0;}}else if (2 == direction){y=y -(1);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body;snack_body = p1;p2 = snack_body;if ( !((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself(x, y))//判斷是否撞墻{gotoxy(snack_body->x, snack_body->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 1;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life = 0;}}else if (3 == direction){x=x - (1 * 2);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body;snack_body = p1;p2 = snack_body;if ( !( (y < 1||y > 18) || (x < 2||x >38)) && !is_eat_itself(x, y))//判斷是否撞墻 {gotoxy(snack_body->x, snack_body->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 1;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else {snack_life = 0;}}if (4 == direction){x = x + (1 * 2);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body;snack_body = p1;p2 = snack_body;if (! ((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself(x, y))//判斷是否撞墻{gotoxy(snack_body->x, snack_body->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 1;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life = 0;}}color(16); }void snack_move2(int direction)//蛇移動函數 direction為方向 direction的值是1向上 2向下 3向左 4向右 玩家2 {int y = 0;int x = 0;struct snack *p4 = NULL;struct snack *p1 = NULL;struct snack *p2 = NULL;p4 = snack_body2;x = p4->x;//臨時儲存當前的蛇頭坐標y = p4->y;color(3);if (1 == direction){y = y + (1);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body2;snack_body2 = p1;p2 = snack_body2;if (!((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself2(x, y))//判斷是否撞墻 滿足條件就是沒撞墻{gotoxy(snack_body2->x, snack_body2->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 0;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life2 = 0;}}else if (2 == direction){y = y - (1);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body2;snack_body2 = p1;p2 = snack_body2;if (!((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself2(x, y))//判斷是否撞墻{gotoxy(snack_body2->x, snack_body2->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 0;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life2 = 0;}}else if (3 == direction){x = x - (1 * 2);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body2;snack_body2 = p1;p2 = snack_body2;if (!((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself2(x, y))//判斷是否撞墻 {gotoxy(snack_body2->x, snack_body2->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 0;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life2 = 0;}}if (4 == direction){x = x + (1 * 2);p1 = (struct snack *)malloc(sizeof(struct snack));//創建了一個結構體然后它的下一及指向舊的蛇頭,然后讓指向舊蛇頭的指針又指向新蛇頭p1->x = x;p1->y = y;p1->next = snack_body2;snack_body2 = p1;p2 = snack_body2;if (!((y < 1 || y > 18) || (x < 2 || x >38)) && !is_eat_itself2(x, y))//判斷是否撞墻{gotoxy(snack_body2->x, snack_body2->y);printf("G");//打印新蛇頭if (x == Food.x&&y == Food.y){Food.life = 0;//不清除蛇尾也不釋放蛇尾,標志食物被吃who_flag = 0;//表示被此玩家吃掉}else{while (((p2->next)->next) != NULL)//倒數第二個身體{p2 = p2->next;}gotoxy(p2->next->x, p2->next->y);printf(" ");//清除蛇尾free(p2->next);//釋放蛇尾p2->next = NULL;p2 = NULL;}}else{snack_life2 = 0;}}color(16); }void add_grade()//加分函數 {if (0 == Food.life)//被吃掉就在此產生食物{if (0 == who_flag){count = count + speed;gotoxy(60, 8);printf("%d", count);}if (1 == who_flag){count2 = count2 + speed;gotoxy(60, 7);printf("%d", count2);}product_food();} }void is_who_winner() {} int main() { char ch; restart:count = 0;//玩家1分數清零count2 = 0;//玩家2分數清零a = 1;//向下走 玩家1初始向下走a2 = 1;//向下走 玩家2初始向下走Food.life = 0;//使重新產生食物snack_life = 1;//蛇活著 玩家1活著snack_life2 = 1;//蛇活著 玩家2活著speed = 0;introduce();//自我介紹map();product_food();init_snack();while (1){add_grade();snack_move(control());snack_move2(control2());if (snack_life == 0|| snack_life2 == 0||count>100||count2>100){break; //游戲結束}Sleep(2100-speed*100);}if (snack_life == 0 || snack_life2 == 0)//誰死誰輸{gotoxy(20, 20);printf("有玩家撞死了,游戲結束!!!\n");if (snack_life == 0&& snack_life2 == 1){printf("玩家2勝利\n");}if(snack_life == 1 && snack_life2 == 0){printf("玩家1勝利\n");}if (snack_life == 0 && snack_life2 == 0){printf("平局\n");}}else//都沒死{gotoxy(20, 20);printf("\n");if (count > count2){printf("玩家1勝利\n");}else if (count < count2){printf("玩家2勝利\n");}}while (1){gotoxy(20, 21);printf("是否繼續:Y/N:");scanf_s("%c", &ch);if ('Y' == ch){free(snack_body);//釋放上一局游戲內存snack_body = NULL;free(snack_body2);//釋放上一局游戲內存snack_body = NULL;goto restart;}else if('N'==ch){break;//退出游戲}}return 0; }

總結

以上是生活随笔為你收集整理的C语言编写双人贪吃蛇游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。