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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 贪吃蛇 小游戏

發布時間:2024/2/28 c/c++ 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 贪吃蛇 小游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<time.h> #include<conio.h> #include <windows.h> #define M 28//方框大小 必須是方的 #define N 28 #define TIME 200//刷新時間 單位:毫秒 int flag = 0;//用于判斷是否刪除尾節點 typedef struct Body//蛇的身體 {int x;int y;struct Body *pnext; }body, *pbody;pbody phead = NULL; pbody ptail = NULL;void insert_head(pbody *pphead, pbody *pptail, int x_new, int y_new)//頭插法 {pbody pnew;pnew = (pbody)calloc(1, sizeof(body));pnew->x = x_new;pnew->y = y_new;if (NULL == *pptail)//如果鏈表為空{*pphead = pnew;*pptail = pnew;//不用擔心尾節點不是NULL,因為calloc的pnew中的pnext成員本身就是null}else{(*pptail)->pnext = pnew;//原有鏈表尾的pnext成員指向新節點*pptail = pnew;//新節點成為新的鏈表尾} } //刪除 void delete_tail(pbody *pphead, pbody *pptail) {pbody pbefore = *pphead;if (NULL == *pptail)//如果鏈表為空{printf("鏈表為空\n");system("pause");}*pphead = (*pphead)->pnext;free(pbefore);//?對嗎 }class Snake { public:std::string name;int length;int head_locate[M][N] = { 0 };//運行過程中,保證整個范圍內只有一個坐標為1int m, n;//用來記錄當前為1的位置char direction;//默認一開始向U走Snake(std::string name);//構造器~Snake();//析構器void snake_go(Snake snake); };class Apple { public:int apple_locate[M][M] = { 0 };Apple();void apple_change_location(); };class Printer { public:int p[M][N] = { 0 };void print_coder();void print_user();void synchronize(Snake *snake, Apple *apple); };//構造器 Snake::Snake(std::string name)//傳入player1 {this->name = name;this->length = 0;this->head_locate[M / 2][N / 2] = 1;this->m = M / 2;this->n = N / 2;this->direction = 'L'; }Apple::Apple() {apple_change_location(); }//析構器 Snake::~Snake() {// std::cout << "對象已被刪除" << std::endl; }//函數 int rand_number() {int num;num = rand() % (M - 2);return num + 1; }char keyboard() {char key;int ch1 = 0;int ch2 = 0;if (ch1 = _getch())//蜜汁下劃線{ch2 = _getch();//第一次調用getch(),返回值224switch (ch2)//第二次調用getch(){case 72: key = 'U'; break;case 80: key = 'D'; break;case 75: key = 'L'; break;case 77: key = 'R'; break;}}return key; }void Snake::snake_go(Snake snake) {if (snake.direction == 'U')//上{this->head_locate[this->m][this->n] = 0;m--;if (m < 0){std::cout << "\n\n你碰壁了!\n\n";system("pause");}this->head_locate[this->m][this->n] = 1;insert_head(&phead, &ptail, this->m, this->n);//頭插法}else if (snake.direction == 'D')//下{this->head_locate[this->m][this->n] = 0;m++;if (m >= M){std::cout << "\n\n你碰壁了!\n\n";system("pause");}this->head_locate[this->m][this->n] = 1;insert_head(&phead, &ptail, this->m, this->n);//頭插法}else if (snake.direction == 'L')//左{this->head_locate[this->m][this->n] = 0;n--;if (n < 0){std::cout << "\n\n你碰壁了!\n\n";system("pause");}this->head_locate[this->m][this->n] = 1;insert_head(&phead, &ptail, this->m, this->n);//頭插法}else if (snake.direction == 'R')//右{this->head_locate[this->m][this->n] = 0;n++;if (n >= N){std::cout << "\n\n你碰壁了!\n\n";system("pause");}this->head_locate[this->m][this->n] = 1;insert_head(&phead, &ptail, this->m, this->n);//頭插法}if (flag == 0 && phead->pnext != NULL)delete_tail(&phead, &ptail);//刪除一個尾節點system("cls");//放在主函數里的話看不到最后結果 }void Apple::apple_change_location() {//找到兩個坐標int x, y;x = rand_number();//產生隨機數 y = rand_number();//產生隨機數this->apple_locate[x][y] = 1; }void Printer::synchronize(Snake *snake, Apple *apple) {int m, n;//先清零for (m = 0; m < M; m++){for (n = 0; n < N; n++){this->p[m][n] = 0;}}//snake同步pbody pcur = phead;while (pcur != NULL){this->p[pcur->x][pcur->y] = 1;pcur = pcur->pnext;}//apple同步for (m = 0; m < M; m++){for (n = 0; n < N; n++){if (apple->apple_locate[m][n] == 1){this->p[m][n] = this->p[m][n] + 2;if (this->p[m][n] == 3){snake->length++;apple->apple_locate[m][n] = 0;apple->apple_change_location();flag = 1;}}}}//同步結果p[]:1蛇頭 2蘋果 3吃了 }void Printer::print_coder() {int m, n;for (m = 0; m < M; m++){for (n = 0; n < N; n++){std::cout << this->p[m][n] << ' ';}std::cout << std::endl;} }void Printer::print_user() {int m, n;for (m = 0; m < M; m++){for (n = 0; n < N; n++){if (this->p[m][n] != 0){std::cout << this->p[m][n];std::cout << " ";}else if (n == 0 || n == N - 1)//橫{std::cout << "*";std::cout << " ";}else if (m == 0 || m == M - 1)//豎{std::cout << "*";std::cout << " ";}else std::cout << " ";}std::cout << std::endl;} }int main() {srand((int)time(NULL));//產生隨機數的種子char exitflag = '\0';Snake snake("player1");Apple apple;Printer printer;//開始printer.synchronize(&snake, &apple);printer.print_user();//測試for (int i = 1; ; i++){if (_kbhit()){// 函數名: getch()// 功能及返回值: 從鍵盤上讀取到的字符exitflag = _getch();if (exitflag == 72 || exitflag == 80 || exitflag == 75 || exitflag == 77){switch (exitflag){case 72: snake.direction = 'U'; break;case 80: snake.direction = 'D'; break;case 75: snake.direction = 'L'; break;case 77: snake.direction = 'R'; break;default:std::cout << "輸入錯誤\n"; break;}}}snake.snake_go(snake);//走完看flag,如果是0就刪flag = 0;printer.synchronize(&snake, &apple);//同步完改flag,如果吃了,flag改成1printer.print_user();Sleep(TIME);}system("pause"); }

總結

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

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