本文執行個體講述了貪吃蛇遊戲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;}elsedisplay(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++; //長度加1if (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程式設計的學習有所協助。