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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

贪吃蛇游戏代码

發布時間:2024/4/11 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 贪吃蛇游戏代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h>const int maxn = 100; const int n = 20;struct node {int x, y; };int map[maxn][maxn]; // 0表示空格,1表示蛇身,2表示食物,3表示撞死的位置, 4表示蛇頭. node food; node squence[maxn]; // 蛇的身子的坐標. int len; // 蛇的長度. bool eat; // 判斷當前事物是否被吃了. bool gameover; // 游戲是否失敗. int direction; // 判斷當前蛇頭是朝哪個方向.void Output(void); void Move(int direction); void Move_up(void); void Move_down(void); void Move_left(void); void Move_right(void);int main() {int i, j;double start;int gamespeed; // 游戲速度自己調整.int timeover;int game_times = 1; // 游戲的次數.char c = 'x';printf("請輸入游戲的級別(1到500,1最難,500最簡單):\n");scanf("%d", &gamespeed);gamespeed = gamespeed;// 對圖的初始化.for (i = 0; i <= n + 1; i++){for (j = 0; j <= n + 1; j++){map[i][j] = 0;}}// 對蛇的初始化.for (i = 1; i <= n; i++){squence[i].x = 0;squence[i].y = 0;}len = 1; // 蛇的長度為1.squence[len].x = 1;squence[len].y = 1; // 初始位置在點(1, 1).map[1][1] = 4;direction = 4; // 默認開始時蛇向右走.srand(time(0));while (game_times <= 20){eat = 0; // 食物還沒被吃.while (true){ // 隨機生出食物的坐標,注意不能與蛇身重合,否則就重新隨機產生.food.x = rand() % 20 + 1;food.y = rand() % 20 + 1;if (map[food.x][food.y] == 0){break;}}map[food.x][food.y] = 2; // 食物位置.system("cls");Output();// 以下這段半秒鐘不按鍵還取原方向繼續前行.while (!eat){start = clock();timeover=1;while(!kbhit()){ // 說明沒有按鍵.if (clock() - start <= gamespeed){ // 如果時間超過游戲時間.timeover = 1;}else{timeover = 0;break;}}if (timeover){ // 說明有按鍵.// 按一次鍵,可以連取兩個c = getch();c = getch();// 以下幾行告訴怎樣判斷用戶按的哪個方向鍵if(c==72) direction = 1; // printf("向上");if(c==80) direction = 2; // printf("向下");if(c==75) direction = 3; // printf("向左");if(c==77) direction = 4; // printf("向右");}Move(direction);system("cls");if (gameover){Output();printf("Game Over!!!\n");return 0;}Output();}game_times++; // 又成功吃到一次食物.}printf("You win!!!\n");return 0; }void Move(int direction) {switch (direction){case 1 : Move_up(); break;case 2 : Move_down(); break;case 3 : Move_left(); break;default : Move_right();} }void Output(void) {int i, j;for (j = 0; j <= n + 1; j++){printf("#");}printf("\n");for (i = 1; i <= n; i++){for (j = 0; j <= n + 1; j++){if (j == 0 || j == n + 1){if (map[i][j] == 3){printf("!");}else{printf("#");}}else{if (map[i][j] == 1){printf("*");}else if (map[i][j] == 2){printf("@");}else if (map[i][j] == 3){printf("!");}else if (map[i][j] == 4){switch (direction){case 1 : printf("%c", 30); break;case 2 : printf("%c", 31); break;case 3 : printf("%c", 17); break;default : printf("%c", 16);}}else{printf(" ");}}}printf("\n");}for (j = 0; j <= n + 1; j++){printf("#");}printf("\n"); }void Move_up(void) {int i;int x, y;if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x + 1){ // 不能移動,則按原來的移動.direction = 2; // 按原來的向下移動.Move(direction);return ;}// 開始移動.x = squence[len].x - 1;y = squence[len].y;if (x == 0 || map[x][y] == 1){ // 撞到邊界或者自己撞到自己.map[x][y] = 3;gameover = 1;}if (map[x][y] == 2){ // 說明已經吃到事物.map[squence[len].x][squence[len].y] = 1;len++;squence[len].x = x;squence[len].y = y;map[x][y] = 4;eat = 1;}else{map[squence[1].x][squence[1].y] = 0;for (i = 1; i <= len - 1; i++){squence[i].x = squence[i + 1].x;squence[i].y = squence[i + 1].y;map[squence[i + 1].x][squence[i + 1].y] = 1;}squence[len].x = squence[len].x - 1;if (gameover){map[squence[len].x][squence[len].y] = 3;}else{map[squence[len].x][squence[len].y] = 4;}} }void Move_down(void) {int i;int x, y;if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x - 1){ // 不能移動,則按原來的移動.direction = 1; // 按原來的向上移動.Move(direction);return ;}// 開始移動.x = squence[len].x + 1;y = squence[len].y;if (x == n + 1 || map[x][y] == 1){ // 撞到邊界或者自己撞到自己.map[x][y] = 3;gameover = 1;}if (map[x][y] == 2){ // 說明已經吃到事物.map[squence[len].x][squence[len].y] = 1;len++;squence[len].x = x;squence[len].y = y;map[x][y] = 4;eat = 1;}else{map[squence[1].x][squence[1].y] = 0;for (i = 1; i <= len - 1; i++){squence[i].x = squence[i + 1].x;squence[i].y = squence[i + 1].y;map[squence[i + 1].x][squence[i + 1].y] = 1;}squence[len].x = squence[len].x + 1;if (gameover){map[squence[len].x][squence[len].y] = 3;}else{map[squence[len].x][squence[len].y] = 4;}} }void Move_left(void) {int i;int x, y;if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y + 1){ // 不能移動,則按原來的移動.direction = 4; // 按原來的向右移動.Move(direction);return ;}// 開始移動.x = squence[len].x;y = squence[len].y - 1;if (y == 0 || map[x][y] == 1){ // 撞到邊界或者自己撞到自己.map[x][y] = 3;gameover = 1;}if (map[x][y] == 2){ // 說明已經吃到事物.map[squence[len].x][squence[len].y] = 1;len++;squence[len].x = x;squence[len].y = y;map[x][y] = 4;eat = 1;}else{map[squence[1].x][squence[1].y] = 0;for (i = 1; i <= len - 1; i++){squence[i].x = squence[i + 1].x;squence[i].y = squence[i + 1].y;map[squence[i + 1].x][squence[i + 1].y] = 1;}squence[len].y = squence[len].y - 1;if (gameover){map[squence[len].x][squence[len].y] = 3;}else{map[squence[len].x][squence[len].y] = 4;}} }void Move_right(void) {int i;int x, y;if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y - 1){ // 不能移動,則按原來的移動.direction = 3; // 按原來的向左移動.Move(direction);return ;}// 開始移動.x = squence[len].x;y = squence[len].y + 1;if (y == n + 1 || map[x][y] == 1){ // 撞到邊界或者自己撞到自己.map[x][y] = 3;gameover = 1;}if (map[x][y] == 2){ // 說明已經吃到事物.map[squence[len].x][squence[len].y] = 1;len++;squence[len].x = x;squence[len].y = y;map[x][y] = 4;eat = 1;}else{map[squence[1].x][squence[1].y] = 0;for (i = 1; i <= len - 1; i++){squence[i].x = squence[i + 1].x;squence[i].y = squence[i + 1].y;map[squence[i + 1].x][squence[i + 1].y] = 1;}squence[len].y = squence[len].y + 1;if (gameover){map[squence[len].x][squence[len].y] = 3;}else{map[squence[len].x][squence[len].y] = 4;}} }

總結

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

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