1、先看下制作效果:
(1)开始界面
(2)分数显示及死亡判断(撞墙后死亡以及落地后死亡)
2、接下来展示代码:
#include"iostream"
#include"time.h"
#include"conio.h"
#include"windows.h"
#include"vector"
using namespace std;
const int Long = 80; // x轴长度
const int High = 40; // y轴高度
char GameMap[High][Long];
enum DIR
{
UP,
DOWN,
};
int keyboard = DOWN;//判断上下
int sum = 0;
class Column { // 柱子类
int X;
int Up;
int Down;
public:
Column(int x) : X(x){
Up = rand() % 10 + 10;
Down = Up + 7;
}
int getX() { return X; }
int getUp() { return Up; }
int getDown() { return Down; }
void setX(int newX) { X = newX; }
};
class Bird
{
int X;
int Y;
public:
Bird(int y = 20, int x = 40) :Y(y), X(x) {}
int getX() { return X; }
int getY() { return Y; }
void setY(int newy) { Y = newy; }
};
vector<Column> column;
void output(Bird &bird)//让鸟运动起来
{
if (keyboard == DOWN)
{
bird.setY(bird.getY() + 1);
}
else
{
bird.setY(bird.getY() - 1);
}
}
void input()//判断输入
{
Sleep(50);
if (_kbhit())
{
switch (_getch())
{
case 'w':
case 'W':
case 72://数字是wasd的键值
keyboard = UP;
break;
default:
keyboard = DOWN;
}
}
}
void draw()//重新绘制地图
{
for (int i = 0; i < High; i++) {
for (int j = 0; j < Long; j++) {
cout << GameMap[i][j];
}
cout << endl;
}
}
void gotoxy(int x, int y)//覆盖清屏
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
return;
}
void columnx()//x坐标更改
{
for (int i = 0; i < column.size(); i++)
{
for (int j = 1; j <= column[i].getUp(); j++)
{
GameMap[j][column[i].getX()] = ' ';
}
for (int j = column[i].getDown(); j < High-1; j++)
{
GameMap[j][column[i].getX()] = ' ';
}
}
for (int i = 0; i < column.size(); i++)
{
column[i].setX(column[i].getX() - 1);
}
if (!column.empty() && column.front().getX() <= 10) {
column.erase(column.begin());
}
}
void coordinate()//柱子的坐标
{
for (int i = 0; i < column.size(); i++)
{
for (int j = 1; j <= column[i].getUp(); j++)
{
GameMap[j][column[i].getX()] = '|';
}
for (int j = column[i].getDown(); j <High-1; j++)
{
GameMap[j][column[i].getX()] = '|';
}
}
}
void death(bool &gameover,Bird &bird)//死亡判定
{
if (bird.getY() >= 38 || bird.getY() <= 2)
{
gameover = false;
return;
}
for (int i = 0; i < column.size(); i++)
{
for (int j = 1; j <= column[i].getUp(); j++)
{
if (bird.getY() == j && bird.getX() == column[i].getX())
{
gameover = false;
return;
}
}
for (int j = column[i].getDown(); j < High - 1; j++)
{
if (bird.getY() == j && bird.getX() == column[i].getX())
{
gameover = false;
return;
}
}
}
}
void score(Bird &bird)
{
for (int i = 0; i < column.size(); i++)//分数判定
{
if (column[i].getX() == bird.getX())
{
sum++;
return;
}
}
Sleep(15);
}
int main() {
srand(time(NULL));
// 创建一个Column对象并添加到vector中
for (int i = 0; i < High; i++){
for (int j = 0; j < Long; j++){
if (i == 0 || i == 39)
{
GameMap[i][j] = '=';
}
else if (j == 0 ||j == 79)
{
GameMap[i][j] = '+';
}
else {
GameMap[i][j] = ' ';
}
}
}
Bird bird;
GameMap[bird.getY()][bird.getX()] = 'B';
for (int i = 0; i < High; i++){
for (int j = 0; j < Long; j++){
cout << GameMap[i][j];
}
cout << endl;
}
int k = 0;
while (k!=1)
{
cout << "按下 '1' 开始游戏"<<endl;
cin >> k;
}
bool gameover = true;
int time = 20;
while (gameover)
{
score(bird);
keyboard = DOWN;
GameMap[bird.getY()][bird.getX()] = ' ';
input();
output(bird);
GameMap[bird.getY()][bird.getX()] = 'B';
columnx();
coordinate();
gotoxy(0,0);
setvbuf(stdout, NULL, _IOFBF, 4096); // 1:修改刷新模式为手动刷新
Sleep(50);
draw();//清空后重新绘制
cout << "分数为: " << sum;
fflush(stdout); // 2:清空并输出缓冲区的内容
setvbuf(stdout, NULL, _IOLBF, 4096); // 3:刷新模式改回去
if (time == 30)
{
column.push_back(Column(78));
time = 0;
}
time++;
death(gameover, bird);
}
cout << "-------你已经死亡!!!-----"<<endl;
cout <<"总分为: " << sum<<"分";
system("color 04");
return 0;
}