C/C++游戏:Flappy Bird
效果图
代码:
//cocou
//2019 8/6
//.cpp
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
#define MAX 100
int g_MapHight, g_MapWidth; //场地大小
int g_BridX, g_BridY; //鸟的X,Y坐标
int g_ThroughX, g_ThroughY; //障碍物的 的坐标
int Map[MAX][MAX]={ 0 };
bool Book[MAX][MAX] = { false }; //true --触碰死了
bool g_Result = false; //true --游戏结束
int g_Score; //得分
//初始化数据
void StartUp();
//初始化地图
void StartMap();
//清除控制台光标
void HideCursor();
//显示游戏界面
void Show();
//刷新游戏界面
void UpDateOutPut();
//监测键盘输入
void UpDateIntPut();
int main()
{
system("color 0e");
char Judge;
bool i = false;
cout << "是否开始飞行之旅:【y/n】";
cin >> Judge;
if (Judge == 'y' || Judge == 'Y')
{
while (!i)
{
StartUp();
while (true)
{
HideCursor();
StartMap();
Show();
UpDateIntPut();
UpDateOutPut();
if (g_Result == true)
{
break;
}
system("cls");
}
cout << "醉鸡了 是否继续【y/n】";
char val;
cin >> val;
if (val == 'y' || val == 'Y')
{
g_Result = false;
}
else
{
i = true;
}
}
}
return 0;
}
void StartUp()
{
g_MapHight = 20;
g_MapWidth = 50;
g_Score = 0;
g_BridX = g_MapWidth / 4;
g_BridY = g_MapHight / 2;
g_ThroughX = g_MapWidth / 4 * 3;
g_ThroughY = g_MapHight / 2;
}
//0---' ' 1--'$ ' 2--'* ' 3--'- ' 4--'| '
void StartMap()
{
int i, j;
for(i = 0;i<=g_MapHight -1;i++)
{
for (j = 0; j <= g_MapWidth - 1;j++)
{
Map[i][j] = 0;
}
}
//
//初始化围墙
for (j = 0; j < g_MapWidth; j++)
{
Map[0][j] = Map[g_MapHight - 1][j] = 3;
}
//初始化左右的围墙
for (i = 0; i < g_MapHight; i++)
{
Map[i][0] = Map[i][g_MapWidth - 1] = 4;
}
//初始化小鸟位置
Map[g_BridY][g_BridX] = 1;
//初始化障碍物
for (i = 0; i < g_MapHight - 1; i++)
{
Map[i][g_ThroughX] = 2;
Book[i][g_ThroughX] = true;
}
//初始化一个口子
for (i = g_ThroughY - 3; i <= g_ThroughY + 3; i++)
{
Map[i][g_ThroughX] = 0;
Book[i][g_ThroughX] = false;
}
}
//清除控制台光标
void HideCursor()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
}
//显示游戏页面
void Show()
{
for (int i = 0; i < g_MapHight; i++)
{
for (int j = 0; j < g_MapWidth; j++)
{
switch (Map[i][j])
{
case 0:
cout << " ";
break;
case 1:
cout << "@";
break;
case 2:
cout << "*";
break;
case 3:
cout << "~";
break;
case 4:
cout << "|";
break;
default:
break;
}
}
cout << endl;
}
cout << "你的分数是:" << g_Score << endl;
cout << "按W 或者 空格 飞起来" << endl;
}
//刷新
void UpDateOutPut()
{
g_BridY++;
g_ThroughX--;
//鸟撞死
if (Book[g_BridY][g_BridX] == true || g_BridY <= 0 || g_BridY >= g_MapHight - 1)
{
g_Result = true;
}
if (g_BridX == g_ThroughX + 1)
{
g_Score += 1000;
}
if (g_ThroughX < 1)
{
srand((unsigned int) time(0));
g_ThroughY = rand() % g_MapHight;
while (g_ThroughY <= 4 || g_ThroughY >= g_MapHight - 5)
{
g_ThroughY = rand() % g_MapHight;
}
g_ThroughX = g_MapWidth / 4 * 3;
}
memset(Book, 0, sizeof(Book));
Sleep(100);
}
//监测键盘的输入
void UpDateIntPut()
{
char judge;
if (_kbhit())
{
judge = _getch();
if (judge = ' ' || judge == 'w' || judge == 'W')
{
g_BridY -= 2;
}
else {
cin.ignore(100, ' '); //忽略别的输入
}
}
}
清除光标
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
乱数
srand((unsigned int) time(0)); //触发器
g_ThroughY = rand() % g_MapHight;
输入控制
cin.ignore(100, ' '); // 忽略前100字符 或是' '前输入的字符
_kbhit() _getch() 包含在<conio.h>
kbhit(): 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。
kbhit()在执行时,检测是否有按键按下,有按下返回非0值,没有按下则返回0,是非阻塞函数;
getch():在执行时,检测按下什么键,如果不按键该函数不返回,也就不进行下一步操作,是阻塞函数。
可以搭配使用