您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么用C語言制作貪吃蛇小游戲的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
直接上代碼
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <conio.h> #include <time.h> #include <windows.h> //MAXWIDTH、MAXHEIGHT、INITLEN 以字符記 #define MAXWIDTH (30) #define MAXHEIGHT MAXWIDTH #define INITLEN (3) //貪吃蛇的初始長度 //程序中用到的各種字符,以及它們的顏色和類型(以數字表示) struct{ char *ch; int color; char type; } charBorder = {"□", 4, 1}, //邊框 charBg = {"■", 2, 2}, //背景 charSnake = {"★", 0xe, 3}, //貪吃蛇節點 charFood = {"●", 0xc, 4}; //食物 //用一個結構體數組保存地圖中的各個點 struct{ char type; int index; }globalMap[MAXWIDTH][MAXHEIGHT]; //貪吃蛇有效活動范圍地圖的索引 struct{ int x; int y; } snakeMap[ (MAXWIDTH-2)*(MAXHEIGHT-2) ], scoresPostion; int scores = 0; //得分 int snakeMapLen = (MAXWIDTH-2)*(MAXHEIGHT-2); int headerIndex, tailIndex; //蛇頭蛇尾對應的snakeMap中的索引(下標) HANDLE hStdin; //控制臺句柄 // 設置光標位置,x為行,y為列 void setPosition(int x, int y){ COORD coord; coord.X = 2*y; coord.Y = x; SetConsoleCursorPosition(hStdin, coord); } // 設置顏色 void setColor(int color){ SetConsoleTextAttribute(hStdin, color); } //創建食物 void createFood(){ int index, rang, x, y; //產生隨機數,確定 snakeMap 數組的索引 srand((unsigned)time(NULL)); if(tailIndex<headerIndex){ rang = headerIndex-tailIndex-1; index = rand()%rang + tailIndex + 1; }else{ rang = snakeMapLen - (tailIndex - headerIndex+1); index = rand()%rang; if(index>=headerIndex){ index += (tailIndex-headerIndex+1); } } x = snakeMap[index].x; y = snakeMap[index].y; setPosition(x, y); setColor(charFood.color); printf("%s", charFood.ch); globalMap[x][y].type=charFood.type; } //死掉 void die(){ int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1; int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1; setPosition(xCenter, yCenter-5); setColor(0xC); printf("You die! Game Over!"); getch(); exit(0); } // 蛇移動 void move(char direction){ int newHeaderX, newHeaderY; //新蛇頭的坐標 int newHeaderPreIndex; //新蛇頭坐標以前對應的索引 int newHeaderPreX, newHeaderPreY; //新蛇頭的索引以前對應的坐標 int newHeaderPreType; //新蛇頭以前的類型 int oldTailX, oldTailY; //老蛇尾坐標 // ----------------------------------------------- //新蛇頭的坐標 switch(direction){ case 'w': newHeaderX = snakeMap[headerIndex].x-1; newHeaderY = snakeMap[headerIndex].y; break; case 's': newHeaderX = snakeMap[headerIndex].x+1; newHeaderY = snakeMap[headerIndex].y; break; case 'a': newHeaderX = snakeMap[headerIndex].x; newHeaderY = snakeMap[headerIndex].y-1; break; case 'd': newHeaderX = snakeMap[headerIndex].x; newHeaderY = snakeMap[headerIndex].y+1; break; } //新蛇頭的索引 headerIndex = headerIndex==0 ? snakeMapLen-1 : headerIndex-1; // ----------------------------------------------- //新蛇頭坐標以前對應的索引 newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index; //新蛇頭的索引以前對應的坐標 newHeaderPreX = snakeMap[headerIndex].x; newHeaderPreY = snakeMap[headerIndex].y; //雙向綁定新蛇頭索引與坐標的對應關系 snakeMap[headerIndex].x = newHeaderX; snakeMap[headerIndex].y = newHeaderY; globalMap[newHeaderX][newHeaderY].index = headerIndex; //雙向綁定以前的索引與坐標的對應關系 snakeMap[newHeaderPreIndex].x = newHeaderPreX; snakeMap[newHeaderPreIndex].y = newHeaderPreY; globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex; //新蛇頭以前的類型 newHeaderPreType = globalMap[newHeaderX][newHeaderY].type; //設置新蛇頭類型 globalMap[newHeaderX][newHeaderY].type = charSnake.type; // 判斷是否出界或撞到自己 if(newHeaderPreType==charBorder.type || newHeaderPreType==charSnake.type ){ die(); } //輸出新蛇頭 setPosition(newHeaderX, newHeaderY); setColor(charSnake.color); printf("%s", charSnake.ch); //判斷是否吃到食物 if(newHeaderPreType==charFood.type){ //吃到食物 createFood(); //更改分數 setPosition(scoresPostion.x, scoresPostion.y); printf("%d", ++scores); }else{ //老蛇尾坐標 oldTailX = snakeMap[tailIndex].x; oldTailY = snakeMap[tailIndex].y; //刪除蛇尾 setPosition(oldTailX, oldTailY); setColor(charBg.color); printf("%s", charBg.ch); globalMap[oldTailX][oldTailY].type = charBg.type; tailIndex = (tailIndex==0) ? snakeMapLen-1 : tailIndex-1; } } //下次移動的方向 char nextDirection(char ch, char directionOld){ int sum = ch+directionOld; ch = tolower(ch); if( (ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum!=197 && sum!=234 ){ return ch; }else{ return directionOld; } } //暫停 char pause(){ return getch(); } // 初始化 void init(){ // 設置相關變量 int x, y, i, index; int xCenter = MAXHEIGHT%2==0 ? MAXHEIGHT/2 : MAXHEIGHT/2+1; int yCenter = MAXWIDTH%2==0 ? MAXWIDTH/2 : MAXWIDTH/2+1; CONSOLE_CURSOR_INFO cci; //控制臺光標信息 //判斷相關設置是否合理 if(MAXWIDTH<16){ printf("'MAXWIDTH' is too small!"); getch(); exit(0); } //設置窗口大小 system("mode con: cols=96 lines=32"); //隱藏光標 hStdin = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleCursorInfo(hStdin, &cci); cci.bVisible = 0; SetConsoleCursorInfo(hStdin, &cci); //打印背景 for(x=0; x<MAXHEIGHT; x++){ for(y=0; y<MAXWIDTH; y++){ if(y==0 || y==MAXWIDTH-1 || x==0 || x==MAXHEIGHT-1){ globalMap[x][y].type = charBorder.type; setColor(charBorder.color); printf("%s", charBorder.ch); }else{ index = (x-1)*(MAXWIDTH-2)+(y-1); snakeMap[index].x= x; snakeMap[index].y= y; globalMap[x][y].type = charBg.type; globalMap[x][y].index = index; setColor(charBg.color); printf("%s", charBg.ch); } } printf("\n"); } //初始化貪吃蛇 globalMap[xCenter][yCenter-1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter+1].type = charSnake.type; headerIndex = (xCenter-1)*(MAXWIDTH-2)+(yCenter-1) - 1; tailIndex = headerIndex+2; setPosition(xCenter, yCenter-1); setColor(charSnake.color); for(y = yCenter-1; y<=yCenter+1; y++){ printf("%s", charSnake.ch); } //生成食物 createFood(); //設置程序信息 setPosition(xCenter-1, MAXWIDTH+2); printf(" Apples : 0"); setPosition(xCenter, MAXWIDTH+2); printf(" Author : xiao p"); setPosition(xCenter+1, MAXWIDTH+2); printf("Copyright : c.biancheng.net"); scoresPostion.x = xCenter-1; scoresPostion.y = MAXWIDTH+8; } int main(){ char charInput, direction = 'a'; init(); charInput = tolower(getch()); direction = nextDirection(charInput, direction); while(1){ if(kbhit()){ charInput = tolower(getch()); if(charInput == ' '){ charInput = pause(); } direction = nextDirection(charInput, direction); } move(direction); Sleep(500); } getch(); return 0; }
【操作過程】
編譯運行后,先按enter鍵使游戲開始,然后w,s a d 分別控制上下左右移動
【運行展示】
以上就是“怎么用C語言制作貪吃蛇小游戲”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。