贪吃蛇游戏 c++代码 ↑,↓,→,←分别控制蛇的方向 增加蛇长 随机生成食物 吃食物 吃撞墙 撞到自己
生活随笔
收集整理的這篇文章主要介紹了
贪吃蛇游戏 c++代码 ↑,↓,→,←分别控制蛇的方向 增加蛇长 随机生成食物 吃食物 吃撞墙 撞到自己
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
貪吃蛇游戲 c++代碼
↑,↓,→,←分別控制蛇的方向
增加蛇長(zhǎng)
隨機(jī)生成食物
吃食物
吃撞墻 撞到自己
Snake.h文件
#pragma once #include <stdio.h> #include <stdlib.h> #include <windows.h> //windows API函數(shù) #include<conio.h> #include<iostream> //#include <graphics.h> //#define Speed 100//定義一個(gè)蛇的節(jié)點(diǎn) typedef struct _Snake //定義一個(gè)結(jié)構(gòu)體 {int x;int y;struct _Snake *next; //定義一個(gè)指針 }Snake;//定義蛇頭 Snake * g_pHead = NULL; typedef struct _Food {int x;int y;bool flag; }Food;Food * food = NULL;//開始游戲 void StartGame(); //歡迎界面 void Welcome(); //打印地圖 void PrintMap(); //設(shè)置光標(biāo)位置 void SetPos(int x, int y); //初始化蛇 void InitSnake(); //運(yùn)行游戲 void RunGame(); //移動(dòng)蛇 //void MoveSnake(); void UpMoveSnake(); void DownMoveSnake(); void LeftMoveSnake(); void RightMoveSnake(); //判斷蛇的生命狀態(tài) bool StateSnake(); //隨機(jī)生成食物(5個(gè)) void InitFood(); //吃食物 void EatFood();Snake.cpp文件
/***********************************************@Copyright Meda 文件名稱:Snake.cpp程序名稱:貪吃蛇編譯環(huán)境:VS2017作者相關(guān):Meda最后修改時(shí)間:2019/4/28************************************************/#include "Snake.h" using namespace std; extern int a = -1; int main() {//int a = 0;StartGame();RunGame(); return 0;}void StartGame() {Welcome();PrintMap();InitSnake();InitFood(); } void Welcome() {printf("\n\n\n\n\n\n\n\n");printf("\t\t\t\t\t歡迎來(lái)到貪吃蛇游戲\n");system("pause");//暫停system("cls");//清屏printf("\n\n\n\n\n\n\n\n");printf("\t\t\t\t\t同↑,↓,→,←分別控制蛇的方向\n\n");//printf("\t\t\t\t\t加速能得到更高的分?jǐn)?shù)\n\n");system("pause");system("cls");} void PrintMap() {for (int i = 0; i< 60; i+=2){SetPos(i, 0);printf("■");SetPos(i,29);printf("■");}for (int i = 0; i < 30; i++){SetPos(0, i);printf("■");SetPos(58, i);printf("■");}} //設(shè)置光標(biāo)位置 void SetPos(int x, int y) {HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE );//控制臺(tái)輸出句柄COORD pos = { x , y }; //坐標(biāo)結(jié)構(gòu)體SetConsoleCursorPosition( hOut,pos );//設(shè)置控制臺(tái)光標(biāo)位置} //初始化蛇 void InitSnake() {Snake* pTail = (Snake*)malloc(sizeof(Snake));pTail->x = 40;pTail->y = 15;pTail->next = NULL;//頭插法for (int i = 1; i < 4; i++){g_pHead = (Snake*)malloc(sizeof(Snake));g_pHead->x = 40+ 2 * i;g_pHead->y = 15;g_pHead->next = pTail;pTail = g_pHead;}//顯示蛇Snake* p = g_pHead;while (p!= NULL){SetPos(p->x, p->y);printf("●");p = p->next;}//SetPos(41, 16);}void RunGame() {//程序不停的運(yùn)行,不能推出本界面while (1){//判斷蛇的生命狀態(tài)//bool state = StateSnake();//if (!state)//{// system("cls");//清屏// printf("\n\n\n\n\n\n\n\n");// // printf("\t\t\t\t\tGame Over\n");// break;//}//控制方向char ch;if (_kbhit()) //判斷是否按鍵盤,有個(gè)頭文件<conio.h>{ch = _getch();switch (ch){//按 "↑":case 72: //EatFood();UpMoveSnake();/*while (!food->flag){InitFood();}*/break;//按 "↓":case 80://EatFood();DownMoveSnake();/*while (!food->flag){InitFood();}*/break;//按 "←":case 75:// EatFood();LeftMoveSnake(); /* while (!food->flag){InitFood();}*/break;//按 "→":case 77: // EatFood();RightMoveSnake();/*while (!food->flag){InitFood();}*/break;}}Sleep(100);}return; } /******************************** ***************移動(dòng)蛇************* **********************************///往上移動(dòng) void UpMoveSnake() {while (1 && !_kbhit()){ if(!StateSnake())break;EatFood();//增頭刪尾Snake* p = (Snake *)malloc(sizeof(Snake));//往上移動(dòng)p->x = g_pHead->x;p->y = g_pHead->y - 1;p->next = g_pHead;g_pHead = p;Snake* temp = g_pHead;while (temp->next->next != NULL){//再打印一遍除去尾巴的蛇SetPos(temp->x, temp->y);printf("●");temp = temp->next;}SetPos(temp->next->x, temp->next->y);if (food->flag){printf(" ");free(temp->next);temp->next = NULL;}Sleep(500);while (!food->flag){InitFood();}} }//向右移動(dòng) void RightMoveSnake() {while (1 && !_kbhit()){if (!StateSnake())break;EatFood();//增頭刪尾Snake* p = (Snake *)malloc(sizeof(Snake));//往右移動(dòng)p->x = g_pHead->x + 2;p->y = g_pHead->y;p->next = g_pHead;g_pHead = p;Snake* temp = g_pHead;while (temp->next->next != NULL){//再打印一遍除去尾巴的蛇SetPos(temp->x, temp->y);printf("●");temp = temp->next;}SetPos(temp->next->x, temp->next->y);if (food->flag){printf(" ");free(temp->next);temp->next = NULL;}Sleep(500);while (!food->flag){InitFood();}}}//往下移動(dòng) void DownMoveSnake() {while (1 && !_kbhit()){if (!StateSnake())break;EatFood();//增頭刪尾Snake* p = (Snake *)malloc(sizeof(Snake));p->x = g_pHead->x;p->y = g_pHead->y + 1;p->next = g_pHead;g_pHead = p;Snake* temp = g_pHead;while (temp->next->next != NULL){//再打印一遍除去尾巴的蛇SetPos(temp->x, temp->y);printf("●");temp = temp->next;}SetPos(temp->next->x, temp->next->y);if (food->flag){printf(" ");free(temp->next);temp->next = NULL;}Sleep(500);while (!food->flag){InitFood();}} } //往左移動(dòng) void LeftMoveSnake() {while (1 && !_kbhit()){if (!StateSnake())break;EatFood();//增頭刪尾Snake* p = (Snake *)malloc(sizeof(Snake));//往左移動(dòng)p->x = g_pHead->x - 2;p->y = g_pHead->y;p->next = g_pHead;g_pHead = p;Snake* temp = g_pHead;while (temp->next->next != NULL){//再打印一遍除去尾巴的蛇SetPos(temp->x, temp->y);printf("●");temp = temp->next;}SetPos(temp->next->x, temp->next->y);if (food->flag){printf(" ");free(temp->next);temp->next = NULL;}Sleep(500);while (!food->flag){InitFood();}} }//判斷蛇的生命狀態(tài) bool StateSnake() {int x , y;bool state = true;//Snake *p = (Snake*)malloc(sizeof(Snake));x = g_pHead->x ;y = g_pHead->y;//碰到墻壁if (x == 0 || x == 58||y == 0 || y == 27)state = false;//吃到自己Snake* tem = g_pHead->next;while(tem->next != NULL){if (x == tem->x && y == tem->y){state = false;break;}tem = tem->next;} // bool state = StateSnake(); //游戲結(jié)束if (!state){system("cls");//清屏printf("\n\n\n\n\n\n");printf("\t\t\t\t\t恭喜你得分: " );cout << a;printf("\n\n\t\t\t\t\t\tGame Over\n");}return state ; }//隨機(jī)生成食物 void InitFood() {a = a + 1;int food_x = (rand() % 29) * 2+2;int food_y = rand() % 30;SetPos(food_x, food_y);printf("▲");food = (Food*)malloc(sizeof(Food));food->x = food_x;food->y = food_y;food->flag = true;//打印得分SetPos(70, 10);printf("目前游戲得分: ");cout <<a;return; } //吃到食物 void EatFood() {if (g_pHead->x == food->x && g_pHead->y == food->y){ food->flag = false;}}總結(jié)
以上是生活随笔為你收集整理的贪吃蛇游戏 c++代码 ↑,↓,→,←分别控制蛇的方向 增加蛇长 随机生成食物 吃食物 吃撞墙 撞到自己的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @RunWith的使用
- 下一篇: 高德地图开发(一)显示地图与定位