C++ FLAPPED BIRD

知乎上看到的,学习一下。。。


#include<iostream>
#include<conio.h>
#include<Windows.h>
using namespace std;

class game
{
private:
	int high,width;
	int bird_x, bird_y;
	int bar1_y, bar_xDown, bar_xTop;
	int score;
public:
	void startup();
	void show();
	void updateWithoutInput();
	void updateWithInput();
	void gotoxy(int x,int y);
}g;

void game::gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void game::startup()
{
	high = 20;
	width = 50;
	bird_x = 0;
	bird_y = width / 3;
	bar1_y = width / 2;
	bar_xDown = high / 3;
	bar_xTop = high / 2;
	score = 0;
}

void game::show()
{
	gotoxy(0,0);
	int i, j;

	for (i = 0; i < high; i++)
	{
		for (j = 0; j < width; j++)
		{
			if ((i == bird_x) && (j == bird_y))
			{
				cout << "@";
			}
			else if ((j == bar1_y) && ((i < bar_xDown) || (i > bar_xTop)))
			{
				cout << "*";
			}
			else
			{
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << "SCORE:" << score;
}

void game::updateWithoutInput()
{
	bird_x++;
	bar1_y--;
	if (bird_y==bar1_y)
	{
		if ((bird_x >= bar_xDown) && (bird_x <= bar_xTop))
		{
			score++;
		}
		else
		{
			system("cls");
			cout << "GAME   OVER!!!" << endl;
			cout << " your score:" << score << endl;
			system("pause");
			exit(0);
		}
	}
	if (bar1_y<0)
	{
		bar1_y = width;
		int temp = rand() % int(high*0.8);
		bar_xDown = temp - high / 10;
		bar_xTop = temp + high / 10;
	}
	Sleep(200);
}

void game::updateWithInput()
{
	char input;
	if (kbhit())
	{
		input = getch();
		if (input == ' ')
		{
			bird_x -= 2;
		}
	}
}

int main()
{
	g.startup();
	while (1)
	{
		g.show();
		g.updateWithoutInput();
		g.updateWithInput();
	}
	return 0;
}






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值