C++ flappybird(基于easyx)

在这里插入图片描述

#include<graphics.h>
#include<conio.h>
#include<Windows.h>
#include<mmstream.h>

#pragma comment(lib,"Winmm.lib")

//障碍物移动,所以鸟看起来移动
struct bar {
    
    
	
	int x;	int y;
	int speed;
};
struct bar bar1 = {
    
     200,-500,4 };
struct bar bar2 = {
    
     400,300,4 };

class preGraph {
    
    
public:
	preGraph(int w, int h)
	{
    
    
		initgraph(w, h);
	}
	~preGraph()
	{
    
    
		closegraph();
	}
};
IMAGE img_bk;
IMAGE img_bird_mask;
IMAGE img_bird;
IMAGE img_bar_down;
IMAGE img_bar_down_mask;
int width;
int height;
IMAGE img_bar_up;
IMAGE img_bar_up_mask;
int birdx, birdy;
int birdWidth, birdHeight;
int barHeight;
int time;
int speed;
int jump;

void lose() {
    
    
	EndBatchDraw();
//	Sleep(500);
	cleardevice();
	char str[] = "Game over!";
	outtextxy(width/2 - 10, height/2, str);
	_getch();
	closegraph();
}

void start()
{
    
    
	time = 0;
	mciSendString("open .\\flappy bird图片音乐素材\\background.mp3 alias bkmusic", NULL, 0, NULL);//设置背景音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);

	loadimage(&img_bk, ".\\flappy bird图片音乐素材\\background.jpg");
	width = img_bk.getwidth();
	height = img_bk.getheight();
	loadimage(&img_bird_mask, ".\\flappy bird图片音乐素材\\bird1.jpg");
	loadimage(&img_bird, ".\\flappy bird图片音乐素材\\bird2.jpg");
	loadimage(&img_bar_up_mask, ".\\flappy bird图片音乐素材\\bar_up1.gif");
	loadimage(&img_bar_up, ".\\flappy bird图片音乐素材\\bar_up2.gif");
	loadimage(&img_bar_down_mask, ".\\flappy bird图片音乐素材\\bar_down1.gif");
	loadimage(&img_bar_down, ".\\flappy bird图片音乐素材\\bar_down2.gif");
	barHeight = img_bar_up.getheight();
	birdx = 50;
	birdy = 200;
	birdHeight = img_bird.getheight();
	birdWidth = img_bird.getwidth();
	speed = 6;
	jump = 30;
}
void show()
{
    
    
	putimage(0, 0, &img_bk, SRCCOPY);

	putimage(bar1.x, bar1.y, &img_bar_up_mask, NOTSRCERASE);
	putimage(bar1.x, bar1.y, &img_bar_up, SRCINVERT);

	putimage(bar2.x, bar2.y, &img_bar_down_mask, NOTSRCERASE);
	putimage(bar2.x, bar2.y, &img_bar_down, SRCINVERT);

	putimage(birdx, birdy, &img_bird_mask, NOTSRCERASE);
	putimage(birdx, birdy, &img_bird, SRCINVERT);
	Sleep(50);
}
void updateWithoutInput()
{
    
    

	if (bar1.x < -100) {
    
    //障碍物移动
		bar1.x = 400;
		bar1.y = -300 + (rand() % 200);
	}
	if (bar2.x < -100) {
    
    
		bar2.x = 400;
		bar2.y = -300 + (rand() % 200);
	}

	if (birdy < height - birdHeight)//下坠
		birdy += speed;
	if (birdy >= height - birdHeight)//下边界
		lose();
	if ((birdx + birdWidth >= bar1.x && birdx <= bar1.x + img_bar_down.getwidth()) && (birdy <= bar1.y + barHeight)) {
    
    
		lose();
	}
	if ((birdx + birdWidth >= bar2.x && birdx <= bar2.x + img_bar_down.getwidth()) && (birdy + birdHeight >= bar2.y)) {
    
    
		lose();
	}
	bar1.x -= bar1.speed;
	bar2.x -= bar2.speed;
}
void updateWithInput()
{
    
    
	char input;
	if (_kbhit())
	{
    
    
		input = _getch();
		if (input == ' ')
			birdy -= jump;
	}
}



int main()
{
    
    
	start();
	preGraph initgp(width, height);//初始化界面
	while (1)
	{
    
    
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_45311187/article/details/120932963