貪吃蛇游戲C++敕令行版實例代碼。本站提示廣大學習愛好者:(貪吃蛇游戲C++敕令行版實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是貪吃蛇游戲C++敕令行版實例代碼正文
本文實例講述了貪吃蛇游戲C++敕令行版的完成代碼,長短常經典的游戲。分享給年夜家供年夜家參考。詳細完成辦法以下:
盡人皆知,貪吃蛇游戲是經典的盤算機游戲。
游戲描寫以下:
1. 貪吃蛇可以主動直線進步,或許玩家可以經由過程偏向鍵把持貪吃蛇高低閣下進步,每次進步一格。
2. 貪吃蛇在劃定的區域內運動,當:
①貪吃蛇觸碰著牆壁時;
②貪吃蛇的蛇頭觸碰著蛇身或許蛇尾時;
③玩家的鍵盤輸出不是偏向鍵時;
敕令行顯示“Game Over!”而且加入游戲。
3. 貪吃蛇運動的區域內每次隨機發生一顆“豆豆”,當貪吃蛇吃到“豆豆”後蛇身增加一格,主動進步時光縮 短100ms(默許是1000ms,且不克不及少於100ms)。貪吃蛇長度每為8的倍數Improve a Level。
C++代碼以下:
#include <bios.h> #include <conio.h> #include <dos.h> #include <graphics.h> #include <stdlib.h> #include <time.h> using namespace std; inline void display(char gsDomain[][22], int level, int moveSpeed) { system("cls"); //清屏 cout << endl << endl; for (int i = 0; i < 22; i++) { cout << "\t"; for (int j = 0; j < 22; j++) cout << gsDomain[i][j] << " "; if (i == 0) { cout << "\tLevel:" << level; } else if (i == 3) { cout << "\t主動進步時光"; } else if (i == 5) { cout << "\t距離:" << moveSpeed << " ms"; } cout << endl; } } int main() { char gsDomain[22][22]; //貪吃蛇運動區域(包含牆壁) //初始化貪吃蛇運動區域(不包含牆壁) for (int i = 1; i <= 21; i++) { for (int j = 1; j <= 21; j++) gsDomain[i][j] = ' '; } //初始化貪吃蛇運動區域的高低牆壁 for (int i = 0; i < 22; i++) gsDomain[0][i] = gsDomain[21][i] = '-'; //初始化貪吃蛇運動區域的閣下牆壁 for (int i = 1; i < 21; i++) gsDomain[i][0] = gsDomain[i][21] = '|'; //初始化蛇身 for (int i = 1; i <= 3; i++) gsDomain[1][i] = '*'; //初始化蛇頭 gsDomain[1][4] = '#'; int snake[2][100]; //記載貪吃蛇每次湧現的地位的坐標 for (int i = 0; i < 4; i++) { snake[0][i] = 1; //記載貪吃蛇地點地位的x坐標 snake[1][i] = i + 1; //記載貪吃蛇地點地位的y坐標 } int head = 3, tail = 0, length = 4; int beanX, beanY; //豆豆湧現的地位 srand(time(0)); do { beanX = rand() % 20 + 1; beanY = rand() % 20 + 1; } while (gsDomain[beanX][beanY] != ' '); gsDomain[beanX][beanY] = '*'; //豆豆 cout << "\n\n\t\t貪吃蛇游戲行將開端!\n"; long start; int level = 1, moveSpeed = 1000; for (int i = 3; i >= 0; i--) { start = clock(); while (clock() - start <= 1000){} system("cls"); if (i) { cout << "\n\n\t\t進入游戲倒計時:" << i << endl; } else display(gsDomain, level, moveSpeed); } char direction = 77; //貪吃蛇默許主動向右直線進步 while (true) { bool timeFlag = true; int x, y; start = clock(); //若時光跨越主動進步時光或許鍵盤上有鍵按下則終止輪回 while ((timeFlag = (clock() - start <= moveSpeed)) && !kbhit()){} if (timeFlag) { //鍵盤上有鍵按下時讀取鍵盤輸出 getch(); direction = getch(); } switch (direction) { //向上 case 72: x = snake[0][head] - 1, y = snake[1][head]; break; //向下 case 80: x = snake[0][head] + 1, y = snake[1][head]; break; //向左 case 75: x = snake[0][head], y = snake[1][head] - 1; break; //向右 case 77: x = snake[0][head], y = snake[1][head] + 1; break; default: cout << "\tGame Over!\n"; return 0; } if (x == 0 || x == 21 || y == 0 || y == 21) { //貪吃蛇觸碰著牆壁 cout << "\tGame Over!\n"; return 0; } if (gsDomain[x][y] != ' ' && !(x == beanX && y == beanY)) { //貪吃蛇的蛇頭觸碰著蛇身或許蛇尾 cout << "\tGame Over!\n"; return 0; } if (x == beanX && y == beanY) { //吃豆豆 length++; //長度加1 if (length >= 8) { //游戲進級處置 length -= 8; level++; if (moveSpeed > 100) moveSpeed -= 100; } gsDomain[snake[0][head]][snake[1][head]] = '*'; gsDomain[x][y] = '#'; head = (head + 1) % 100; snake[0][head] = x; snake[1][head] = y; do { beanX = rand() % 20 + 1; beanY = rand() % 20 + 1; } while (gsDomain[beanX][beanY] != ' '); gsDomain[beanX][beanY] = '*'; display(gsDomain, level, moveSpeed); //屏幕上顯示 } else { //不吃豆豆 gsDomain[snake[0][tail]][snake[1][tail]] = ' '; //蛇尾前移一格 tail = (tail + 1) % 100; gsDomain[snake[0][head]][snake[1][head]] = '*'; head = (head + 1) % 100; snake[0][head] = x; snake[1][head] = y; gsDomain[x][y] = '#'; //蛇頭前移一格 display(gsDomain, level, moveSpeed); //屏幕上顯示 } } return 0; }
願望本文所述實例對年夜家C法式設計的進修有所贊助。