您好,登錄后才能下訂單哦!
這篇文章主要介紹了C語言怎么實現貪吃蛇小游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
(1)游戲進入界面(動態讀取以及溫馨提示)
void loading()//動態加載圖 { int j; gotoxy(43, 11); printf("Loading..."); gotoxy(39, 13); printf("□□□□□□□□"); gotoxy(39, 13); for (j = 8; j >= 0; j--) { HideCursor(); Sleep(200); printf("\033[40;35m■\033[0m"); } gotoxy(43, 11); printf(" "); gotoxy(39, 13); printf(" "); gotoxy(39, 9); printf("歡迎來到貪吃蛇世界!"); gotoxy(22, 14); printf("抵制不良游戲 拒絕盜版游戲 注意自我保護 謹防受騙上當"); gotoxy(22, 16); printf("適度游戲益腦 沉迷游戲傷身 合理安排時間 享受健康生活"); Sleep(1500); }
(2)菜單界面
void menu(int a) { HideCursor(); gotoxy(11, 3); printf("\033[40;37m _________________________ || \033[0m"); gotoxy(11, 4); printf("\033[40;33m //===================== || // \033[0m"); gotoxy(11, 5); printf("\033[40;37m // || // \033[0m"); gotoxy(11, 6); printf("\033[40;37m \\\\ __ || // \033[0m"); gotoxy(11, 7); printf("\033[40;33m \\\\ //\\\\ ||// \033[0m"); gotoxy(11, 8); printf("\033[40;33m // // \\\\ ||\\\\ ===== \033[0m"); gotoxy(11, 9); printf("\033[40;37m // == // \\\\ || \\\\ // \\\\ \033[0m"); gotoxy(11, 10); printf("\033[40;37m _ _ _ ____ // // \\\\ //======\\\\ || \\\\ ||========= \033[0m"); gotoxy(11, 11); printf("\033[40;33m- - - -----\\\\_______// // \\\\ // \\\\ || \\\\ || \033[0m"); gotoxy(11, 12); printf("\033[40;35m \\-------/ // \\\\ // \\\\ || \\\\ \\\\________________\033[0m"); gotoxy(39, 6); printf("\033[40;36m②○②○\033[0m"); gotoxy(2, 15); printf("\033[40;33m—————————————————————————————————————————————————\033[1m"); gotoxy(2, 18); printf(" 1.開始游戲 2.游戲設置"); gotoxy(2, 20); printf(" 3.游戲說明 4.退出游戲"); switch (a) { case 1: gotoxy(22, 18); break; case 2: gotoxy(62, 18); break; case 3: gotoxy(22, 20); break; case 4: gotoxy(62, 20); break; } printf("\033[40;36m◇\033[0m"); return; }
(3)菜單選擇
void chose()//菜單選擇 { ii = 1; fflush(stdin); frame(2); menu(ii); while (ii != 5) { (void)_getch(); if (GetAsyncKeyState(VK_DOWN) & 0x8000)//下 { if (ii == 1) ii = 3; else if (ii == 2) ii = 4; } if (GetAsyncKeyState(VK_UP) & 0x8000)//上 { if (ii == 3) ii = 1; else if (ii == 4) ii = 2; } if (GetAsyncKeyState(VK_LEFT) & 0x8000)//左 { if (ii == 2) ii = 1; else if (ii == 4) ii = 3; } if (GetAsyncKeyState(VK_RIGHT) & 0x8000)//右 { if (ii == 1) ii = 2; else if (ii == 3) ii = 4; } menu(ii); Sleep(150);//緩沖 if (GetAsyncKeyState(VK_RETURN) & 0x0D)//確認 { fflush(stdin); return; } } }
(4)障礙物與地圖
void obstacle() //障礙物 { //IOT地圖 int sup_mapx1[68] = { 16,18,20,22,24,20,20,20,20,20,20,16,18,20,22,24,36,38,40,42,44,36,44,36,44,36,44,36,44,36,44,36,44,36,38,40,42,44,56,58,60,62,64,60,60,60,60,60,60,60 }; int sup_mapy1[68] = { 8, 8, 8, 8, 8, 9,10,11,12,13,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9,10,11,12,13,14,15 }; //ZHKU地圖 int sup_mapx2[77] = { 10,12,14,16,18,20,20,18,16,14,12,10,10,12,14,16,18,20,28,28,28,28,28,28,28,28,30,32,34,36,36,36,36,36,36,36,36,44,44,44,44,44,44,44,44,46,48,48,50,52,50,52,60,60,60,60,60,60,60,60,62,64,66,68,70,70,70,70,70,70,70,70,18,12,46,46,48 }; int sup_mapy2[77] = { 9, 9, 9, 9, 9, 9,10,11,12,13,14,15,16,16,16,16,16,16, 9,10,11,12,13,14,15,16,12,12,12, 9,10,11,12,13,14,15,16, 9,10,11,12,13,14,15,16,12,11,13,10, 9,14,15, 9,10,11,12,13,14,15,16,16,16,16,16, 9,10,11,12,13,14,15,16,10,15,11,13,12 }; if (map == 2)//困難 j = 5; if (map == 3)//地獄 j = 15; if (map == 4)//傳說 j = 30; if(map>1) for (i = 0; i < j; i++)//生成障礙物 { obs.x[i] = rand() % 70 + 6; obs.y[i] = rand() % 20 + 3; if (obs.x[i] % 2 != 0)//優化出現的位置 obs.x[i]--; if (obs.x[i] == food.x && obs.y[i] == food.y)//障礙物不能出現在初始食物上 continue; if (obs.y[i] == 20)//初始方向上不出現障礙物 i--; } else if (map == 0) { j = 68; for (i = 0; i < j; i++)//特別版地圖1繪制 { obs.x[i] = sup_mapx1[i]; obs.y[i] = sup_mapy1[i]; } } else if (map == -1) { j = 77; for (i = 0; i < j; i++)//特別版地圖2繪制 { obs.x[i] = sup_mapx2[i]; obs.y[i] = sup_mapy2[i]; } } if (map >1) for (i = 0; i < j; i++)//打印障礙物 { gotoxy(obs.x[i], obs.y[i]); printf("\033[40;31m▲\033[0m"); } else if(map==0) for (i = 0; i < 50; i++)//打印特別地圖1 { gotoxy(obs.x[i], obs.y[i]); if(i<16) printf("\033[40;31m■\033[0m"); else if(i>37) printf("\033[40;34m■\033[0m"); else printf("\033[40;32m■\033[0m"); } else if(map==-1) for (i = 0; i < 72; i++)//打印特別地圖2 { gotoxy(obs.x[i], obs.y[i]); if(i<18)printf("\033[40;32m■\033[0m"); if (i < 37 && i >= 18) printf("\033[40;33m■\033[0m"); if(i>=37&&i<52) printf("\033[40;36m■\033[0m"); if (i >=52)printf("\033[40;35m■\033[0m"); } }
(5)蛇的控制
void control() { fflush(stdin); if (foodFlag == 0)//在蛇尾處輸出空格即擦去蛇尾 { gotoxy(snake.x[snake.length - 1], snake.y[snake.length - 1]); switch (tail) //尾巴痕跡選擇功能 { case 1:printf(" "); break; case 2:printf("~ "); break; case 3:printf("·"); break; } } for (i = snake.length - 1; i > 0; i--)//將蛇的每一節依次向前移動一節(蛇頭除外) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } while (1) { if ((GetAsyncKeyState(VK_DOWN)) && vkflag != 2)//下 { vkflag = 4; break; } if ((GetAsyncKeyState(VK_UP)) && vkflag != 4)//上 { vkflag = 2; break; } if ((GetAsyncKeyState(VK_LEFT)) && vkflag != 3)//左 { vkflag = 1; break; } if ((GetAsyncKeyState(VK_RIGHT)) && vkflag != 1)//右 { vkflag = 3; break; } if (GetAsyncKeyState(VK_SPACE) && 0x20)//暫停功能 while (1) { gotoxy(84, 16); printf("\033[40;36m游戲已經暫停\033[0m"); Sleep(200); gotoxy(84, 16); printf(" "); //閃爍 Sleep(150); if (GetAsyncKeyState(VK_SPACE) && 0x20) break; } break; } switch (vkflag)//方向 { case 1: snake.x[0] -= 2; break;//左 case 2: snake.y[0]--; break;//上 case 3: snake.x[0] += 2; break;//右 case 4: snake.y[0]++; break;//下 } gotoxy(snake.x[0], snake.y[0]);//蛇頭移動 printf("◆"); gotoxy(snake.x[1], snake.y[1]);//蛇身移動 printf("■"); foodFlag = 0; fflush(stdin); return; }
(6)食物生成
void foodcreate() { n++;//加速標記 flag = 1; sorce += 10;//每個食物得10分 if (sorce>=his_max )//最高分 { his_max = sorce; gotoxy(82, 2); printf(" "); gotoxy(82, 2); printf("★歷史最高:%d", his_max); } gotoxy(82, 3);//本局得分 printf(" "); gotoxy(82, 3); printf("☆當前得分:%d", sorce); while (flag==1)//生成食物 { food.x = rand() % 74 + 4;//隨機選取食物的范圍 food.y = rand() % 24 + 1; if (food.x % 2 != 0 && flag == 1)//優化出現位置 { food.x = food.x - 1; flag = 0; } if(map!=1)//非普通地圖項 for (i = 0; i < snake.length; i++) { for(m=0;m<j;m++) if (obs.x[m] == food.x && obs.y[m] == food.y) { flag = 1; break; } } for (i = 0; i < snake.length; i++)//不能在蛇的身體上生成食物 { if (snake.x[i] == food.x && snake.y[i] == food.y) { flag = 1; break; } } } gotoxy(food.x, food.y); //生成食物 printf("●"); snake.length++;//吃到食物長度加1 foodFlag = 1;//吃到食物尾部不清除 return; }
(7)死亡判斷
int dead() { if (snake.y[0] == 0 || snake.y[0] == map_y-1)//上下邊界死亡 return 0; if (snake.x[0] == 0 || snake.x[0] == map_x-2)//左右邊界死亡 return 0; if(map!=1)//撞到障礙物死亡 for (m = 0; m < j; m++) if (obs.x[m] == snake.x[0] && obs.y[m] == snake.y[0]) { return 0; } for (i = 1; i < snake.length; i++)//咬到自身死亡 { if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) return 0; } if (GetAsyncKeyState(VK_ESCAPE)&0x1B)//ESC健返回死亡 { ii = 5; return 0; } return 1; }
(8)初始化游戲
void game()//初始化游戲 { int i ; n = 0;//重置狀態 vkflag = 1; sorce = 0; foodFlag = 0; system("cls");//初始清屏 frame(1);//打印游戲框架(動態) gotoxy(82, 2); printf("★歷史最高:%d", his_max); gotoxy(82, 3); printf("☆當前得分:%d", sorce); gotoxy(84, 8); printf("通過下面按鍵"); gotoxy(85, 9); printf("\033[40;33m ↑ \033[0m"); gotoxy(85, 10); printf("\033[40;33m ← → \033[0m"); gotoxy(85, 11); printf("\033[40;33m ↓ \033[0m"); gotoxy(83, 12); printf("來控制蛇的方向"); gotoxy(82, 18); printf("按空格鍵暫停/繼續"); gotoxy(83, 20); printf("按Shift鍵 加速"); gotoxy(82, 22); printf("按ESC鍵返回菜單"); snake.length = org_len;//初始化蛇(固定位置) snake.x[0] = 50 ; snake.y[0] = 20; gotoxy(snake.x[0], snake.y[0]); printf("◆"); for (i = 1; i < snake.length; i++)//打印蛇 { Sleep(25); snake.x[i] = snake.x[i - 1] + 2; snake.y[i] = snake.y[0]; gotoxy(snake.x[i], snake.y[i]); printf("■"); } if (map >=1) { food.x = rand() % 44 + 4;//初始化食物(左方出現) food.y = rand() % 24 + 1; if (food.x % 2 != 0) food.x = food.x - 1; } else { food.x = 14;//初始化食物(左方出現) food.y = 3; } gotoxy(food.x, food.y);//初始的食物閃爍 printf("\033[40;37m●\033[0m"); Sleep(250); gotoxy(food.x, food.y); printf(" "); Sleep(250); gotoxy(food.x, food.y); printf("\033[40;37m●\033[0m"); Sleep(250); obstacle(); while(ii==1) //運行 { if (GetAsyncKeyState(VK_SHIFT))//加速功能 { gotoxy(93, 20); printf("\033[40;31m加速\033[0m");//特效 } else { Sleep(100); gotoxy(93, 20); printf("加速"); } Sleep(speed - (changespeed * n));//變速模式 control();//控制 if (snake.x[0] == food.x && snake.y[0] == food.y)//吃掉食物 foodcreate(); if (dead() != 1) //判斷是否結束游戲 { gotoxy(36,12); printf("Game over");//結束顯示 gotoxy(36, 14); printf(" T _ T "); Sleep(500); while (1) { gotoxy(32, 16); printf("請按Enter鍵返回主菜單"); Sleep(150); if (GetAsyncKeyState(VK_RETURN) & 0x0D)//確認返回 { fflush(stdin); ii = 5; return; } } } } }
(9)游戲設置
void setup2()//設置顯示(分析各種情況) { gotoxy(34, 8); switch (org_len)//初始長度 { case 2: printf("初始長度 ◆■ >"); break; case 3: printf("初始長度 < ◆■■ >"); break; case 4: printf("初始長度 < ◆■■■ >"); break; case 5: printf("初始長度 < ◆■■■■ >"); break; case 6: printf("初始長度 < ◆■■■■■ >"); break; case 7: printf("初始長度 < ◆■■■■■■ "); break; } gotoxy(34, 10); switch (speed)//初始速度 { case 50:printf("游戲初速度 < 較快 "); break; case 100:printf("游戲初速度 < 中等 >"); break; case 150:printf("游戲初速度 較慢 >"); break; } gotoxy(34, 12); switch (changespeed)//是否變速 { case 0:printf("吃到食物加速 否 >"); break; case 5:printf("吃到食物加速 < 是 "); break; } gotoxy(34, 14); switch (tail)//尾巴選擇 { case 1:printf("當前尾巴痕跡 無 >"); break; case 2:printf("當前尾巴痕跡 < ~ >"); break; case 3:printf("當前尾巴痕跡 < · "); break; } gotoxy(34, 16); switch (map)//地圖選擇 { case -1:printf("當前地圖難度 特別版2 >"); break; case 0:printf("當前地圖難度 < 特別版1 >"); break; case 1:printf("當前地圖難度 < 普通 >"); break; case 2:printf("當前地圖難度 < 困難 >"); break; case 3:printf("當前地圖難度 < 地獄 >"); break; case 4:printf("當前地圖難度 < 傳說 "); break; } gotoxy(70, 22); printf("確定并返回菜單"); for (i = 0; i < 7; i++) { gotoxy(31, 8 + 2 * i); printf(" "); } gotoxy(67, 22); printf(" "); switch (set_flag) { case 1:gotoxy(31, 8); break; case 2:gotoxy(31, 10); break; case 3:gotoxy(31, 12); break; case 4:gotoxy(31, 14); break; case 5:gotoxy(31, 16); break; case 6:gotoxy(67, 22); break; } printf("★"); return; } void setup()//設置操作(改變全局變量) { frame(2); set_flag=1; gotoxy(30, 3); printf(" ■■◆ 游戲設置 ◆■■"); gotoxy(26, 4); printf("■■■■■■ ■■■■■■"); setup2(); fflush(stdin); while (1) { if (GetAsyncKeyState(VK_DOWN))//下 { if (set_flag == 6) set_flag = 1; else set_flag++; } if (GetAsyncKeyState(VK_UP))//上 { if (set_flag == 1) set_flag = 6; else set_flag--; } if (GetAsyncKeyState(VK_LEFT))//左 { switch (set_flag) { case 1: if (org_len > 2) org_len--; break; case 2: if (speed < 150) speed += 50; break; case 3: if (changespeed == 5) changespeed = 0;break; case 4: if (tail > 1) tail--;break; case 5: if (map > -1) map--;break; case 6: break; } } if (GetAsyncKeyState(VK_RIGHT))//右 { switch (set_flag) { case 1: if (org_len < 7) org_len++;break; case 2: if (speed > 50) speed -= 50;break; case 3: if (changespeed == 0) changespeed = 5;break; case 4: if (tail < 3) tail++;break; case 5: if (map < 4) map++;break; case 6:break; } } setup2(); if ((GetAsyncKeyState(VK_RETURN)&0x0D)&&set_flag==6) { ii = 5; return; } Sleep(175); } return; }
(10)游戲說明
void instructions()//游戲說明 { system("cls"); frame(2); gotoxy(10, 3); printf("游戲說明:"); gotoxy(18, 5); printf("一條蛇在封閉空間里,空間內隨機出現一個食物,通過按鍵盤四個光標"); gotoxy(18, 7); printf("鍵↑←↓→控制蛇向上下左右四個方向移動來獲取食物食物,當食物被"); gotoxy(18, 9); printf("吃掉,蛇身體長一節,同時記10分,接著又出現新的食物,如果蛇撞到"); gotoxy(18, 11); printf("墻或蛇頭撞到自己身體或撞到障礙物,游戲結束。"); gotoxy(18, 15); printf("溫馨提示:游戲過程中,可按Shift鍵加速,但不能同時按方向鍵,也"); gotoxy(18, 17); printf(" 可以按空格鍵來暫停游戲。 "); gotoxy(60, 20); printf("按確認鍵返回主菜單"); gotoxy(3, 23); printf("當前游戲版本 V1.0"); while (1)//按確認鍵返回主菜單 { Sleep(100); (void)_getch(); if (GetAsyncKeyState(VK_RETURN))//確認 { ii = 5; break; } } } void end()//游戲退出 { frame(2); gotoxy(44, 12); printf("游戲已退出"); gotoxy(0, 26); }
(11)直接貼完整代碼
#include<stdio.h> #include <stdlib.h> #include<Windows.h> #include <time.h> #include<conio.h> #define SNAKE 300//蛇的最大節數 #define KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)//vk鍵會返回兩次值,在這里篩選按下操作 #define KEYUP(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?0:1) int ii = 1, i, m, flag = 1, n = 0, set_flag = 1;//其他的全局變量(ii為菜單,n變速,set_flag設置,flag是否產生新食物標記) int speed = 100, changespeed = 5, tail = 1, org_len = 5, map = 1;//速度、速度變化、尾巴痕跡、初始長度、地圖難度 int vkflag = 1;//記錄鍵盤 int sorce = 0, his_max = 0;//記錄分數和歷史最高分 int map_x = 80, map_y = 26;//邊界 int foodFlag = 0;//吃到食物標記 int j;//障礙物數量 struct//定義蛇 { int length;//蛇的長度 int x[SNAKE];//組成蛇身的每一個小方塊中x的坐標 int y[SNAKE];//組成蛇身的每一個小方塊中y的坐標 }snake; struct //定義食物節點 { int x, y; } food; struct //定義障礙物 { int x[77]; int y[77]; }obs; void gotoxy();//移動光標位置 void HideCurson();//隱藏光標 void frame(int a);//框架1/2 void loading();//(假)讀取 void menu(int a);//菜單 void chose();//菜單選擇 void obstacle();//障礙物 void control();//控制方向 void foodcreate();//創建食物 int dead();//判斷死亡 void game();//開始游戲 void setup2();//設置的更新 void setup();//設置參數的改變 void instructions();//游戲說明 void end();//游戲結束 void gotoxy(int x, int y) //調整光標 { COORD pos = { x,y }; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 獲取標準輸出設備句柄 SetConsoleCursorPosition(hOut, pos);//兩個參數分別是指定哪個窗體,具體位置 } void HideCursor()//隱藏光標 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void frame(int a)//初始化框 { int i, k; system("cls"); gotoxy(78, 0);//橫1 for (k = 0; k <= 11; k++) { if (a == 1) Sleep(4); printf("\033[40;37m■\033[0m"); } for (i = 0; i <= 25; i++)//右豎 { if (a == 1) Sleep(4); gotoxy(100, i); printf("\033[40;37m■\033[0m"); } for (k = 0; k <= 50; k++)//下橫往左 { if (a == 1) Sleep(4); gotoxy(100 - 2 * k, 25); printf("\033[40;37m■\033[0m"); } for (i = 25; i >= 0; i--)//左豎向上 { if (a == 1) Sleep(4); gotoxy(0, i); printf("\033[40;37m■\033[0m"); } gotoxy(0, 0); for (k = 0; k <= 38; k++)//橫2 { if (a == 1) Sleep(4); printf("\033[40;37m■\033[0m"); } if (a == 1) for (i = 0; i <= 25; i++)//分割豎 { Sleep(6); gotoxy(78, i); printf("\033[40;37m■\033[0m"); } } void loading()//動態加載圖 { int j; gotoxy(43, 11); printf("Loading..."); gotoxy(39, 13); printf("□□□□□□□□"); gotoxy(39, 13); for (j = 8; j >= 0; j--) { HideCursor(); Sleep(200); printf("\033[40;35m■\033[0m"); } gotoxy(43, 11); printf(" "); gotoxy(39, 13); printf(" "); gotoxy(39, 9); printf("歡迎來到貪吃蛇世界!"); gotoxy(22, 14); printf("抵制不良游戲 拒絕盜版游戲 注意自我保護 謹防受騙上當"); gotoxy(22, 16); printf("適度游戲益腦 沉迷游戲傷身 合理安排時間 享受健康生活"); Sleep(1500); } void menu(int a) { HideCursor(); gotoxy(11, 3); printf("\033[40;37m _________________________ || \033[0m"); gotoxy(11, 4); printf("\033[40;33m //===================== || // \033[0m"); gotoxy(11, 5); printf("\033[40;37m // || // \033[0m"); gotoxy(11, 6); printf("\033[40;37m \\\\ __ || // \033[0m"); gotoxy(11, 7); printf("\033[40;33m \\\\ //\\\\ ||// \033[0m"); gotoxy(11, 8); printf("\033[40;33m // // \\\\ ||\\\\ ===== \033[0m"); gotoxy(11, 9); printf("\033[40;37m // == // \\\\ || \\\\ // \\\\ \033[0m"); gotoxy(11, 10); printf("\033[40;37m _ _ _ ____ // // \\\\ //======\\\\ || \\\\ ||========= \033[0m"); gotoxy(11, 11); printf("\033[40;33m- - - -----\\\\_______// // \\\\ // \\\\ || \\\\ || \033[0m"); gotoxy(11, 12); printf("\033[40;35m \\-------/ // \\\\ // \\\\ || \\\\ \\\\________________\033[0m"); gotoxy(39, 6); printf("\033[40;36m②○②○\033[0m"); gotoxy(2, 15); printf("\033[40;33m—————————————————————————————————————————————————\033[1m"); gotoxy(2, 18); printf(" 1.開始游戲 2.游戲設置"); gotoxy(2, 20); printf(" 3.游戲說明 4.退出游戲"); switch (a) { case 1: gotoxy(22, 18); break; case 2: gotoxy(62, 18); break; case 3: gotoxy(22, 20); break; case 4: gotoxy(62, 20); break; } printf("\033[40;36m◇\033[0m"); return; } void chose()//菜單選擇 { ii = 1; fflush(stdin); frame(2); menu(ii); while (ii != 5) { (void)_getch(); if (GetAsyncKeyState(VK_DOWN) & 0x8000)//下 { if (ii == 1) ii = 3; else if (ii == 2) ii = 4; } if (GetAsyncKeyState(VK_UP) & 0x8000)//上 { if (ii == 3) ii = 1; else if (ii == 4) ii = 2; } if (GetAsyncKeyState(VK_LEFT) & 0x8000)//左 { if (ii == 2) ii = 1; else if (ii == 4) ii = 3; } if (GetAsyncKeyState(VK_RIGHT) & 0x8000)//右 { if (ii == 1) ii = 2; else if (ii == 3) ii = 4; } menu(ii); Sleep(150);//緩沖 if (GetAsyncKeyState(VK_RETURN) & 0x0D)//確認 { fflush(stdin); return; } } } void obstacle() //障礙物 { //IOT地圖 int sup_mapx1[68] = { 16,18,20,22,24,20,20,20,20,20,20,16,18,20,22,24,36,38,40,42,44,36,44,36,44,36,44,36,44,36,44,36,44,36,38,40,42,44,56,58,60,62,64,60,60,60,60,60,60,60 }; int sup_mapy1[68] = { 8, 8, 8, 8, 8, 9,10,11,12,13,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,15,15,15, 8, 8, 8, 8, 8, 9,10,11,12,13,14,15 }; //ZHKU地圖 int sup_mapx2[77] = { 10,12,14,16,18,20,20,18,16,14,12,10,10,12,14,16,18,20,28,28,28,28,28,28,28,28,30,32,34,36,36,36,36,36,36,36,36,44,44,44,44,44,44,44,44,46,48,48,50,52,50,52,60,60,60,60,60,60,60,60,62,64,66,68,70,70,70,70,70,70,70,70,18,12,46,46,48 }; int sup_mapy2[77] = { 9, 9, 9, 9, 9, 9,10,11,12,13,14,15,16,16,16,16,16,16, 9,10,11,12,13,14,15,16,12,12,12, 9,10,11,12,13,14,15,16, 9,10,11,12,13,14,15,16,12,11,13,10, 9,14,15, 9,10,11,12,13,14,15,16,16,16,16,16, 9,10,11,12,13,14,15,16,10,15,11,13,12 }; if (map == 2)//困難 j = 5; if (map == 3)//地獄 j = 15; if (map == 4)//傳說 j = 30; if(map>1) for (i = 0; i < j; i++)//生成障礙物 { obs.x[i] = rand() % 70 + 6; obs.y[i] = rand() % 20 + 3; if (obs.x[i] % 2 != 0)//優化出現的位置 obs.x[i]--; if (obs.x[i] == food.x && obs.y[i] == food.y)//障礙物不能出現在初始食物上 continue; if (obs.y[i] == 20)//初始方向上不出現障礙物 i--; } else if (map == 0) { j = 68; for (i = 0; i < j; i++)//特別版地圖1繪制 { obs.x[i] = sup_mapx1[i]; obs.y[i] = sup_mapy1[i]; } } else if (map == -1) { j = 77; for (i = 0; i < j; i++)//特別版地圖2繪制 { obs.x[i] = sup_mapx2[i]; obs.y[i] = sup_mapy2[i]; } } if (map >1) for (i = 0; i < j; i++)//打印障礙物 { gotoxy(obs.x[i], obs.y[i]); printf("\033[40;31m▲\033[0m"); } else if(map==0) for (i = 0; i < 50; i++)//打印特別地圖1 { gotoxy(obs.x[i], obs.y[i]); if(i<16) printf("\033[40;31m■\033[0m"); else if(i>37) printf("\033[40;34m■\033[0m"); else printf("\033[40;32m■\033[0m"); } else if(map==-1) for (i = 0; i < 72; i++)//打印特別地圖2 { gotoxy(obs.x[i], obs.y[i]); if(i<18)printf("\033[40;32m■\033[0m"); if (i < 37 && i >= 18) printf("\033[40;33m■\033[0m"); if(i>=37&&i<52) printf("\033[40;36m■\033[0m"); if (i >=52)printf("\033[40;35m■\033[0m"); } } void control() { fflush(stdin); if (foodFlag == 0)//在蛇尾處輸出空格即擦去蛇尾 { gotoxy(snake.x[snake.length - 1], snake.y[snake.length - 1]); switch (tail) //尾巴痕跡選擇功能 { case 1:printf(" "); break; case 2:printf("~ "); break; case 3:printf("·"); break; } } for (i = snake.length - 1; i > 0; i--)//將蛇的每一節依次向前移動一節(蛇頭除外) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } while (1) { if ((GetAsyncKeyState(VK_DOWN)) && vkflag != 2)//下 { vkflag = 4; break; } if ((GetAsyncKeyState(VK_UP)) && vkflag != 4)//上 { vkflag = 2; break; } if ((GetAsyncKeyState(VK_LEFT)) && vkflag != 3)//左 { vkflag = 1; break; } if ((GetAsyncKeyState(VK_RIGHT)) && vkflag != 1)//右 { vkflag = 3; break; } if (GetAsyncKeyState(VK_SPACE) && 0x20)//暫停功能 while (1) { gotoxy(84, 16); printf("\033[40;36m游戲已經暫停\033[0m"); Sleep(200); gotoxy(84, 16); printf(" "); //閃爍 Sleep(150); if (GetAsyncKeyState(VK_SPACE) && 0x20) break; } break; } switch (vkflag)//方向 { case 1: snake.x[0] -= 2; break;//左 case 2: snake.y[0]--; break;//上 case 3: snake.x[0] += 2; break;//右 case 4: snake.y[0]++; break;//下 } gotoxy(snake.x[0], snake.y[0]);//蛇頭移動 printf("◆"); gotoxy(snake.x[1], snake.y[1]);//蛇身移動 printf("■"); foodFlag = 0; fflush(stdin); return; } void foodcreate() { n++;//加速標記 flag = 1; sorce += 10;//每個食物得10分 if (sorce>=his_max )//最高分 { his_max = sorce; gotoxy(82, 2); printf(" "); gotoxy(82, 2); printf("★歷史最高:%d", his_max); } gotoxy(82, 3);//本局得分 printf(" "); gotoxy(82, 3); printf("☆當前得分:%d", sorce); while (flag==1)//生成食物 { food.x = rand() % 74 + 4;//隨機選取食物的范圍 food.y = rand() % 24 + 1; if (food.x % 2 != 0 && flag == 1)//優化出現位置 { food.x = food.x - 1; flag = 0; } if(map!=1)//非普通地圖項 for (i = 0; i < snake.length; i++) { for(m=0;m<j;m++) if (obs.x[m] == food.x && obs.y[m] == food.y) { flag = 1; break; } } for (i = 0; i < snake.length; i++)//不能在蛇的身體上生成食物 { if (snake.x[i] == food.x && snake.y[i] == food.y) { flag = 1; break; } } } gotoxy(food.x, food.y); //生成食物 printf("●"); snake.length++;//吃到食物長度加1 foodFlag = 1;//吃到食物尾部不清除 return; } int dead() { if (snake.y[0] == 0 || snake.y[0] == map_y-1)//上下邊界死亡 return 0; if (snake.x[0] == 0 || snake.x[0] == map_x-2)//左右邊界死亡 return 0; if(map!=1)//撞到障礙物死亡 for (m = 0; m < j; m++) if (obs.x[m] == snake.x[0] && obs.y[m] == snake.y[0]) { return 0; } for (i = 1; i < snake.length; i++)//咬到自身死亡 { if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) return 0; } if (GetAsyncKeyState(VK_ESCAPE)&0x1B)//ESC健返回死亡 { ii = 5; return 0; } return 1; } void game()//初始化游戲 { int i ; n = 0;//重置狀態 vkflag = 1; sorce = 0; foodFlag = 0; system("cls");//初始清屏 frame(1);//打印游戲框架(動態) gotoxy(82, 2); printf("★歷史最高:%d", his_max); gotoxy(82, 3); printf("☆當前得分:%d", sorce); gotoxy(84, 8); printf("通過下面按鍵"); gotoxy(85, 9); printf("\033[40;33m ↑ \033[0m"); gotoxy(85, 10); printf("\033[40;33m ← → \033[0m"); gotoxy(85, 11); printf("\033[40;33m ↓ \033[0m"); gotoxy(83, 12); printf("來控制蛇的方向"); gotoxy(82, 18); printf("按空格鍵暫停/繼續"); gotoxy(83, 20); printf("按Shift鍵 加速"); gotoxy(82, 22); printf("按ESC鍵返回菜單"); snake.length = org_len;//初始化蛇(固定位置) snake.x[0] = 50 ; snake.y[0] = 20; gotoxy(snake.x[0], snake.y[0]); printf("◆"); for (i = 1; i < snake.length; i++)//打印蛇 { Sleep(25); snake.x[i] = snake.x[i - 1] + 2; snake.y[i] = snake.y[0]; gotoxy(snake.x[i], snake.y[i]); printf("■"); } if (map >=1) { food.x = rand() % 44 + 4;//初始化食物(左方出現) food.y = rand() % 24 + 1; if (food.x % 2 != 0) food.x = food.x - 1; } else { food.x = 14;//初始化食物(左方出現) food.y = 3; } gotoxy(food.x, food.y);//初始的食物閃爍 printf("\033[40;37m●\033[0m"); Sleep(250); gotoxy(food.x, food.y); printf(" "); Sleep(250); gotoxy(food.x, food.y); printf("\033[40;37m●\033[0m"); Sleep(250); obstacle(); while(ii==1) //運行 { if (GetAsyncKeyState(VK_SHIFT))//加速功能 { gotoxy(93, 20); printf("\033[40;31m加速\033[0m");//特效 } else { Sleep(100); gotoxy(93, 20); printf("加速"); } Sleep(speed - (changespeed * n));//變速模式 control();//控制 if (snake.x[0] == food.x && snake.y[0] == food.y)//吃掉食物 foodcreate(); if (dead() != 1) //判斷是否結束游戲 { gotoxy(36,12); printf("Game over");//結束顯示 gotoxy(36, 14); printf(" T _ T "); Sleep(500); while (1) { gotoxy(32, 16); printf("請按Enter鍵返回主菜單"); Sleep(150); if (GetAsyncKeyState(VK_RETURN) & 0x0D)//確認返回 { fflush(stdin); ii = 5; return; } } } } } void setup2()//設置顯示(分析各種情況) { gotoxy(34, 8); switch (org_len)//初始長度 { case 2: printf("初始長度 ◆■ >"); break; case 3: printf("初始長度 < ◆■■ >"); break; case 4: printf("初始長度 < ◆■■■ >"); break; case 5: printf("初始長度 < ◆■■■■ >"); break; case 6: printf("初始長度 < ◆■■■■■ >"); break; case 7: printf("初始長度 < ◆■■■■■■ "); break; } gotoxy(34, 10); switch (speed)//初始速度 { case 50:printf("游戲初速度 < 較快 "); break; case 100:printf("游戲初速度 < 中等 >"); break; case 150:printf("游戲初速度 較慢 >"); break; } gotoxy(34, 12); switch (changespeed)//是否變速 { case 0:printf("吃到食物加速 否 >"); break; case 5:printf("吃到食物加速 < 是 "); break; } gotoxy(34, 14); switch (tail)//尾巴選擇 { case 1:printf("當前尾巴痕跡 無 >"); break; case 2:printf("當前尾巴痕跡 < ~ >"); break; case 3:printf("當前尾巴痕跡 < · "); break; } gotoxy(34, 16); switch (map)//地圖選擇 { case -1:printf("當前地圖難度 特別版2 >"); break; case 0:printf("當前地圖難度 < 特別版1 >"); break; case 1:printf("當前地圖難度 < 普通 >"); break; case 2:printf("當前地圖難度 < 困難 >"); break; case 3:printf("當前地圖難度 < 地獄 >"); break; case 4:printf("當前地圖難度 < 傳說 "); break; } gotoxy(70, 22); printf("確定并返回菜單"); for (i = 0; i < 7; i++) { gotoxy(31, 8 + 2 * i); printf(" "); } gotoxy(67, 22); printf(" "); switch (set_flag) { case 1:gotoxy(31, 8); break; case 2:gotoxy(31, 10); break; case 3:gotoxy(31, 12); break; case 4:gotoxy(31, 14); break; case 5:gotoxy(31, 16); break; case 6:gotoxy(67, 22); break; } printf("★"); return; } void setup()//設置操作(改變全局變量) { frame(2); set_flag=1; gotoxy(30, 3); printf(" ■■◆ 游戲設置 ◆■■"); gotoxy(26, 4); printf("■■■■■■ ■■■■■■"); setup2(); fflush(stdin); while (1) { if (GetAsyncKeyState(VK_DOWN))//下 { if (set_flag == 6) set_flag = 1; else set_flag++; } if (GetAsyncKeyState(VK_UP))//上 { if (set_flag == 1) set_flag = 6; else set_flag--; } if (GetAsyncKeyState(VK_LEFT))//左 { switch (set_flag) { case 1: if (org_len > 2) org_len--; break; case 2: if (speed < 150) speed += 50; break; case 3: if (changespeed == 5) changespeed = 0;break; case 4: if (tail > 1) tail--;break; case 5: if (map > -1) map--;break; case 6: break; } } if (GetAsyncKeyState(VK_RIGHT))//右 { switch (set_flag) { case 1: if (org_len < 7) org_len++;break; case 2: if (speed > 50) speed -= 50;break; case 3: if (changespeed == 0) changespeed = 5;break; case 4: if (tail < 3) tail++;break; case 5: if (map < 4) map++;break; case 6:break; } } setup2(); if ((GetAsyncKeyState(VK_RETURN)&0x0D)&&set_flag==6) { ii = 5; return; } Sleep(175); } return; } void instructions()//游戲說明 { system("cls"); frame(2); gotoxy(10, 3); printf("游戲說明:"); gotoxy(18, 5); printf("一條蛇在封閉空間里,空間內隨機出現一個食物,通過按鍵盤四個光標"); gotoxy(18, 7); printf("鍵↑←↓→控制蛇向上下左右四個方向移動來獲取食物食物,當食物被"); gotoxy(18, 9); printf("吃掉,蛇身體長一節,同時記10分,接著又出現新的食物,如果蛇撞到"); gotoxy(18, 11); printf("墻或蛇頭撞到自己身體或撞到障礙物,游戲結束。"); gotoxy(18, 15); printf("溫馨提示:游戲過程中,可按Shift鍵加速,但不能同時按方向鍵,也"); gotoxy(18, 17); printf(" 可以按空格鍵來暫停游戲。 "); gotoxy(60, 20); printf("按確認鍵返回主菜單"); gotoxy(3, 23); printf("當前游戲版本 V1.0"); while (1)//按確認鍵返回主菜單 { Sleep(100); (void)_getch(); if (GetAsyncKeyState(VK_RETURN))//確認 { ii = 5; break; } } } void end()//游戲退出 { frame(2); gotoxy(44, 12); printf("游戲已退出"); gotoxy(0, 26); } main()//游戲框架 { frame(2); loading(); while (ii == 1 || ii == 2 || ii == 3||ii==5) { system("cls"); Sleep(200); menu(ii); chose(); switch (ii) { case 1:game(); break; case 2:setup(); break; case 3:instructions(); break; case 4:break; case 5:ii = 1; continue; } } end(); }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C語言怎么實現貪吃蛇小游戲”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。