c++编写手机小游戏代码_C++代码实现贪吃蛇小游戏
本文實例為大家分享了C++實現貪吃蛇小游戲的具體代碼,供大家參考,具體內容如下
1.游戲描述
貪吃蛇可謂是從小玩到大的經典趣味小游戲,蛇每吃到一次食物,身體就會長一節,如果撞到墻或者撞到自身,游戲結束。
2.代碼實現
1.首先需要思考的問題是如何指定位置輸出字符?這時候就有一個非常強大的函數叫 gotoxy() ,現在庫函數里邊已經沒有了,只能我們自己實現,代碼中注釋很完整,自行閱讀即可。
2.實現了指哪畫哪的目標之后,就可以開始游戲內容制作了。首先便是圈地,即畫地圖,一個簡簡單單的循環就能安排的明明白白。
3.偉大的圈地運動就結束了,接下來我們就實現畫一條蛇,我們選擇使用deque雙端隊列,這個操作更為方便,畫好蛇之后就給畫一個食物出來,食物的位置坐標使用隨機數來實現,簡單吧~
4.讓蛇動起來。我們默認讓蛇往上走,即‘w'方向,之后便是按鍵響應,這個只要懂點語法,小白都能實現,就不多說了。
5.貪吃蛇的大體框架就這樣搭好了,是不是soeasy~
3.裝飾環節
只是會跑當然不能滿足我們日益增長的精神需求,那就加點料唄,下面的代碼中只加入了計分、等級,其他的都沒有加,為的是新手能快速上手,你也可以再往里邊加吃到食物時“滴~”響一聲等元素,這都不是問題。
話不多說,上代碼!
#include
#include
#include
#include
#include
#pragma warning(disable:4996)
using namespace std;
HANDLE hOut;
COORD pos;
//1.實現gotoxy函數
void gotoxy(short x, short y)
{
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //獲取句柄
pos = { x, y };
SetConsoleCursorPosition(hOut, pos); //移動光標到指定位置
}
void HideCursor() //隱藏光標
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//獲取控制臺光標信息
CursorInfo.bVisible = false; //隱藏控制臺光標
SetConsoleCursorInfo(handle, &CursorInfo);//設置控制臺光標狀態
}
//2.蛇的結構體
struct Snake
{
char body;
short position_x, position_y; //蛇的坐標
};
//3.游戲運行類
class Game
{
private:
char image;
enum mapSize { width = 60, height = 30 }; //游戲地圖
deque snake; //定義一個隊列,裝蛇的身體
int score = 0; //游戲分數
char hit = 'w'; //按鍵輸入
bool eat_Food = false; //是否吃到食物
short food_x, food_y; //食物坐標
int speed = 400; //蛇的速度
bool snake_state = true; //蛇的狀態
int level = 1; //游戲關卡
public:
Game();
void draw_Frame() //畫邊框
{
for (int i = 0; i < height; i++)
{
gotoxy(0, i);
cout << "■";
gotoxy(width, i);
cout << "■";
}
for (int i = 0; i <= width; i += 2) //■ 這個符號占兩個字符位置,所以是+2
{
gotoxy(i, 0);
cout << "■";
gotoxy(i, height);
cout << "■";
}
}
void init_snake() //初始化蛇
{
snake.push_back({ '#', width / 2, static_cast(height / 2) }); //添加蛇頭
for (int i = 0; i < 3; i++) //蛇的初始身體節數,可自定
snake.push_back({ char('o'), width / 2, static_cast((height / 2) + 1 + i) });
snake.push_back({ ' ', width / 2, static_cast((height / 2) + 4) }); //添加蛇尾,先放空,以便于后面添加節數時操作
}
void draw_Snake() //畫蛇
{
for (int k = 0; k < snake.size(); k++)
{
gotoxy(snake[k].position_x, snake[k].position_y);
cout << snake[k].body;
}
}
void clear_Tail() //清除蛇尾,不建議使用清屏函數,避免屏閃
{
int k = snake.size() - 1;
gotoxy(snake[k].position_x, snake[k].position_y);
cout << " "; //蛇每移動一次(即一格),就把上一次畫出來的蛇尾擦掉
}
void draw_Food() //畫食物
{
while (1)
{
food_x = rand() % (width - 4) + 2; //食物要在地圖中,不能再地圖邊框上,地圖的符號在x方向占兩個字符位置所以+2,而-4則是減去邊框
food_y = rand() % (height - 2) + 1; //與上同理
if (wrong_Location() && food_x % 2 == 0)
break;
}
gotoxy(food_x, food_y);
cout << "O";
}
bool wrong_Location() //判斷食物的坐標是否合理
{
for (auto i : snake) //c++11的基于范圍的循環
{
if (food_x == i.position_x && food_y == i.position_y) //食物不能出現在蛇的身體上
return 0;
}
return 1;
}
void judge_eatFood() //判斷是否吃到食物
{
if (food_x == snake[0].position_x && food_y == snake[0].position_y)
eat_Food = true;
}
void judge_state() //判斷蛇是否撞墻或撞身體
{
if (snake.size() >= 2)
{
deque::iterator iter = snake.begin() + 1; //實際就是把snake容器里第一個(即蛇頭)存在iter里
int x = (iter - 1)->position_x, y = (iter - 1)->position_y;
for (; iter != snake.end(); ++iter)
{
if (iter->position_x == x && iter->position_y == y) //蛇頭不能撞自身
snake_state = false;
}
}
if(snake[0].position_x == 1 ||
snake[0].position_x == 59 ||
snake[0].position_y == 0 ||
snake[0].position_y == 30) //蛇頭不能撞邊框
snake_state = false;
}
void key_Down() //按鍵響應
{
char key = hit;
if (_kbhit()) //接受按鍵
hit = _getch();
for (int i = snake.size() - 1; i > 0; i--) //蛇的移動方法,每移動一次,后面一節的身體到了它的前一節身體上
{
snake[i].position_x = snake[i - 1].position_x;
snake[i].position_y = snake[i - 1].position_y;
}
if ((hit == 'a' || hit == 'A') && hit != 'd')
{
hit = 'a'; snake[0].position_x--;
}
else if ((hit == 'd' || hit == 'D') && hit != 'a')
{
hit = 'd'; snake[0].position_x++;
}
else if ((hit == 'w' || hit == 'W') && hit != 's')
{
hit = 'w'; snake[0].position_y--;
}
else if ((hit == 's' || hit == 'S') && hit != 'w')
{
hit = 's'; snake[0].position_y++;
}
}
void show()
{
gotoxy(65, 0);
cout << "你的得分是:";
gotoxy(71, 1);
cout << score;
gotoxy(69, 2);
cout << "關卡:" << level;
}
};
Game::Game()
{
HideCursor();
srand(static_cast(time(NULL))); //隨機數種子
init_snake();
draw_Food();
Snake tail; //蛇尾
while (1)
{
draw_Frame();
tail = snake.back();
if (eat_Food)
{
snake.back().body = 'o'; //把初始化蛇的空尾顯示化為o,看到的效果就是加了一節
snake.push_back(tail); //再添加一節空尾,便于下次操作
gotoxy(food_x, food_y);
cout << " "; //食物被吃后要在原來的位置畫空,否則光標會退位,導致邊框錯位
draw_Food();
score++;
if (score % 5 == 0)
{
speed *= 0.8;
level++;
}
eat_Food = false;
}
if (level == 10)
break;
key_Down();
draw_Snake();
judge_state();
if (!snake_state)
break;
judge_eatFood();
Sleep(speed);
clear_Tail();
show();
}
}
int main()
{
system("mode con cols=100 lines=40"); //設置打開窗口大小
system("color 7C"); //設置背景色和前景色
system("title 貪吃蛇 v1.0"); 設置窗口標題
Game game;
gotoxy(0, 32);
cout << "Game over!" << endl;
}
本期教程到這里就結束了。
更多有趣的經典小游戲實現專題,分享給大家:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的c++编写手机小游戏代码_C++代码实现贪吃蛇小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机rom,那些你不知道的事
- 下一篇: php判断是否是节假日,C#编程之C#判