C++控制檯實現貪吃蛇遊戲
阿新 • • 發佈:2020-04-28
本文例項為大家分享了C++實現貪吃蛇遊戲的具體程式碼,供大家參考,具體內容如下
剛學完了C語言,便嘗試的寫了貪吃蛇的程式碼,但是效果不佳,很多的bug,所以,這個學了C++,便重新的寫了這個小遊戲,用類來封裝!
先是標頭檔案:
struct Snake { int x,y; }; class snake { public: snake() //建構函式 { length = 3; s[2].x = 10; s[2].y = 10; s[1].x = 9; s[1].y = 10; s[0].x = 8; s[0].y = 10; up = right = left = down = 0; } ~snake(){} void display(); //顯示蛇身函式 void Rightmove(); //右移函式 void Leftmove(); //左移函式 void Upmove(); //上移函式 void Downmove(); //下移函式 int cheak(); //檢查是否撞牆或撞到自身 void creat_food(); //產生食物 int eat_food(); //吃食物 private: struct Snake s[100]; //先定義蛇身最長100 int length; //當前蛇長度 int x3,y3; //食物座標 int up,down,right,left; //蛇的狀態,是上移還是下移或... }; void make_frame(); //列印框架的函式 void show(); //遊戲開始倒計時函式 void gameover(); //遊戲結束函式
下面是各個函式的實現的cpp檔案:
# include <iostream> # include <Windows.h> # include <time.h> # include "snake.h" # define MaxLen 20 # define MaxWen 30 using namespace std; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //獲取控制代碼 void gotoxy(HANDLE hOut,int x,int y) //輸出位置的函式 { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut,pos); } void snake::display() //列印蛇身 { for (int i = length - 1; i >= 0; i--) { if (i == length - 1) //列印蛇頭 { gotoxy(hOut,s[i].x,s[i].y); cout << char(15); } else //列印蛇身 { gotoxy(hOut,s[i].y); cout << '*'; } } gotoxy(hOut,22); } void snake::Rightmove() //右移 { right = 1; up = down = left = 0; int x1,x2,y1,y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].x++; //蛇頭x座標自增 for (int i = length - 2; i >= 0; i--) //除了蛇頭,其他的結點都等於它的上一個結點的座標 { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut,y2); //消除蛇移動遺留的 ‘*' cout << ' '; } void snake::Leftmove() //左移 { left = 1; right = up = down = 0; int x1,y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].x--; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut,y2); //同上 cout << ' '; } void snake::Downmove() //下移 { down = 1; right = up = left = 0; int x1,y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].y++; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut,y2); //同上 cout << ' '; } void snake::Upmove() //上移 { up = 1; down = right = left = 0; int x1,y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].y--; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut,y2); //同上 cout << ' '; } int snake::cheak() { int flag = 0; for (int i = length - 2; i >= 0; i--) //是否撞到自身 { if (s[i].x == s[length - 1].x && s[i].y == s[length - 1].y) { flag = 1; //是,識別符號為1 break; } } if (flag == 1 || (s[length - 1].x >= 30 + 1 || s[length - 1].x < 4) || (s[length - 1].y <= 1 || s[length - 1].y >= 20)) { return 0; //檢測是否撞自身,或者撞牆 } else { return 1; } } void snake::creat_food() //產生食物座標 { xy: x3 = (rand() % (25)) + 3; y3 = (rand() % (17)) + 2; for (int i = length - 1; i >= 0; i--) //檢查食物是否在蛇身上 { if (s[i].x == x3 && s[i].y == y3) //是就重新產生食物座標 goto xy; } gotoxy(hOut,x3,y3); //顯示食物 cout << '*'; } int snake::eat_food() { if (s[length - 1].x == x3 && s[length - 1].y == y3) //蛇頭碰到食物 { if (up == 1) //如果蛇是在上移,增加一個結點,為蛇頭的上一個結點 { s[length].x = x3; s[length].y = y3 - 1; } else if (down == 1) //同上 { s[length].x = x3; s[length].y = y3 + 1; } else if (right == 1) //同上 { s[length].x = x3 + 1; s[length].y = y3; } else if (left == 1) //同上 { s[length].x = x3 - 1; s[length].y = y3; } length++; //蛇長加1 return 1; } else return 0; } void make_frame() //列印框架函式 { cout << " 貪吃蛇遊戲" << endl; gotoxy(hOut,2,1); cout << "╔"; for (int i = 4; i < 2 + MaxWen; i++) { gotoxy(hOut,i,1); printf("="); } for (int i = 2; i < MaxLen; i++) { gotoxy(hOut,i); printf("║"); } gotoxy(hOut,2 + MaxWen,1); printf("╗"); for (int i = 2; i < MaxLen; i++) { gotoxy(hOut,MaxLen); printf("╚"); gotoxy(hOut,MaxLen); printf("╝"); for (int i = 4; i < 2 + MaxWen; i++) { gotoxy(hOut,MaxLen); printf("="); } } void show() //顯示操作方法和遊戲開始倒計時 { gotoxy(hOut,35,5); cout << "↑:" << 'w'; gotoxy(hOut,6); cout << "←:" << 'a'; gotoxy(hOut,7); cout << "↓:" << 's'; gotoxy(hOut,8); cout << "→:" << 'd'; gotoxy(hOut,16,5); cout << '3'; Sleep(1000); gotoxy(hOut,5); cout << '2'; Sleep(1000); gotoxy(hOut,5); cout << '1'; Sleep(1000); gotoxy(hOut,5); cout << ' '; } void gameover() //遊戲結束函式 { system("cls"); system("color 3B"); gotoxy(hOut,14,5); cout << " GAME OVER!"; gotoxy(hOut,6); cout << "PLAY AGAIN ? Y(yes) \ N(no)"; }
主函式的cpp檔案:
# include <iostream> # include <Windows.h> # include <conio.h> # include "snake.h" using namespace std; char ch; int main() { while (1) { snake sn; //宣告物件 system("cls"); //清屏 system("color 3B"); //背景和字型顏色調整 make_frame(); //列印框架 sn.display(); //顯示蛇 show(); //遊戲開始 sn.creat_food(); //產生食物 while (sn.cheak()) //檢查是否死亡 { sn.Rightmove(); //右移 sn.display(); //顯示蛇身 if (sn.eat_food()) //檢查是否吃到食物 { sn.creat_food(); //重新產生食物 sn.display(); } Sleep(500); //等待500Ms p: if (_kbhit()) //是否有按鍵 { ch = _getch(); if (ch == 97 || ch == 100) goto p; if (ch == 115 || ch == 119) break; } } pp: switch (ch) //有按鍵 { case 119: //上移的情況 { while (sn.cheak()) { sn.Upmove(); sn.display(); if (sn.eat_food()) { sn.creat_food(); sn.display(); Sleep(300); } Sleep(500); pw: if (_kbhit()) { ch = _getch(); if (ch == 119 || ch == 115) goto pw; if (ch == 97 || ch == 100) goto pp; } } }break; case 97: //左移的情況 { while (sn.cheak()) { sn.Leftmove(); sn.display(); if (sn.eat_food()) { sn.creat_food(); sn.display(); } Sleep(500); pa: if (_kbhit()) { ch = _getch(); if (ch == 97 || ch == 100) goto pa; if (ch == 115 || ch == 119) goto pp; } } }break; case 115: //下移的情況 { while (sn.cheak()) { sn.Downmove(); sn.display(); if (sn.eat_food()) { sn.creat_food(); sn.display(); Sleep(300); } Sleep(500); ps: if (_kbhit()) { ch = _getch(); if (ch == 115 || ch == 119) goto ps; if (ch == 97 || ch == 100) goto pp; } } }break; case 100: //右移的情況 { while (sn.cheak()) { sn.Rightmove(); sn.display(); if (sn.eat_food()) { sn.creat_food(); sn.display(); } Sleep(500); pd: if (_kbhit()) { ch = _getch(); if (ch == 100 || ch == 97) goto pd; if (ch == 119 || ch == 115) goto pp; } } }break; default: break; } gameover(); //顯示遊戲結束,是否重玩 py: ch = _getch(); if (ch == 110) //否 { system("cls"); break; } else if (ch == 121) //是 continue; else goto py; } return 0; }
下面是遊戲的截圖:
控制檯的實現,不是很美觀,主要是由於上下和左右的間隙不一樣大,所以看起來不是很好看,但總體還是實現了貪吃蛇!
關於C++小遊戲的更多精彩內容請點選專題: 《C++經典小遊戲》 學習瞭解
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。