【推荐福利】c++使用easyx做出像素鸟,简单上手小游戏

346 阅读2分钟

c++使用easyx做出像素鸟,简单上手小游戏

老规矩,先上效果图
在这里插入图片描述
1首先加载一堆资源图片

// 加载图片资源
void loadRes()
{
	loadimage(&BK[0], _T("res\\bg_day.png"));
	loadimage(&BK[1], _T("res\\bg_night.png"));

	loadimage(&bk, _T("res\\bg_day.png"));

	loadimage(&OVEIMG, _T("res\\bg_day.png"));

	loadimage(&BIRD[0][0], _T("res\\bird0_0.png"));
	loadimage(&BIRD[0][1], _T("res\\bird0_1.png"));
	loadimage(&BIRD[0][2], _T("res\\bird0_2.png"));
	loadimage(&BIRD[1][0], _T("res\\bird1_0.png"));
	loadimage(&BIRD[1][1], _T("res\\bird1_1.png"));
	loadimage(&BIRD[1][2], _T("res\\bird1_2.png"));
	loadimage(&BIRD[2][0], _T("res\\bird2_0.png"));
	loadimage(&BIRD[2][1], _T("res\\bird2_1.png"));
	loadimage(&BIRD[2][2], _T("res\\bird2_2.png"));

	loadimage(&PIPE[0][0], _T("res\\pipe_down.png"));
	loadimage(&PIPE[0][1], _T("res\\pipe_up.png"));
	loadimage(&PIPE[1][0], _T("res\\pipe2_down.png"));
	loadimage(&PIPE[1][1], _T("res\\pipe2_up.png"));

	loadimage(&NUMBER[0][0], _T("res\\font_048.png"));
	loadimage(&NUMBER[0][1], _T("res\\font_049.png"));
	loadimage(&NUMBER[0][2], _T("res\\font_050.png"));
	loadimage(&NUMBER[0][3], _T("res\\font_051.png"));
	loadimage(&NUMBER[0][4], _T("res\\font_052.png"));
	loadimage(&NUMBER[0][5], _T("res\\font_053.png"));
	loadimage(&NUMBER[0][6], _T("res\\font_054.png"));
	loadimage(&NUMBER[0][7], _T("res\\font_055.png"));
	loadimage(&NUMBER[0][8], _T("res\\font_056.png"));
	loadimage(&NUMBER[0][9], _T("res\\font_057.png"));

	loadimage(&NUMBER[1][0], _T("res\\number_context_00.png"));
	loadimage(&NUMBER[1][1], _T("res\\number_context_01.png"));
	loadimage(&NUMBER[1][2], _T("res\\number_context_02.png"));
	loadimage(&NUMBER[1][3], _T("res\\number_context_03.png"));
	loadimage(&NUMBER[1][4], _T("res\\number_context_04.png"));
	loadimage(&NUMBER[1][5], _T("res\\number_context_05.png"));
	loadimage(&NUMBER[1][6], _T("res\\number_context_06.png"));
	loadimage(&NUMBER[1][7], _T("res\\number_context_07.png"));
}

2 初始化游戏资源

void init()
{
	// 加载图形资源
	loadRes();
	// 初始化图形界面
	initgraph(288, 512);
	// 初始化随机数种子
	srand((unsigned int)time(NULL));
	// 初始化变量
	best = 0;
	isDay = rand() % 2;
	birdColor = rand() % 3;
	// 游戏初始时间
	clock_t time = clock();
	// 开场动画
	while (true)
	{
		reset();
		drawAlpha(&bk, 60, 120, &TITLE);
		drawAlpha(&bk, 125, 200, &BIRD[birdColor][pose]);
		drawAlpha(&bk, 90, 270, &PLAY);
		pose = ((clock() - time) / diff) % 3;
		put();
		if (GetControl())
		{
			break;
		}
		Sleep(10);
	}
}

3开始游戏

// 游戏预处理
void start()
{
	// 初始化数据
	isDay = rand() % 2;
	birdColor = rand() % 3;
	pipeX[0] = 288 + 30;
	pipeX[1] = 288 + 30 + 190;
	pipeY[0] = rand() % 250;
	pipeY[1] = rand() % 250;
	pose = 0;
	landX = 0;
	score = 0;
	y = 220;
	vy = 0;

	// 游戏初始时间
	clock_t time = clock();
	// 开场动画
	clock_t t = clock();
	while (true)
	{
		reset();
		drawScore(60, score, 13, 26, 144, 0);
		drawAlpha(&bk, 50, 120, &READY);
		drawAlpha(&bk, (int)x, (int)y, &BIRD[birdColor][pose]);
		drawAlpha(&bk, 90, 220, &TUTORIAL);
		drawAlpha(&bk, (int)landX, (int)landY, &LAND);
		landX -= (clock() - t) * vx / 100;
		t = clock();
		pose = ((clock() - time) / diff) % 3;
		put();
		if (landX < -44)
		{
			landX = -20;
		}
		if (GetControl())
		{
			break;
		}
		Sleep(10);
	}
}

4操控像素鸟

bool GetControl() 
{
	bool res = false;

	if (_kbhit()) 
	{
		char ch = _getch();
		if (ch == ' ') 
		{
			res = true;
		}
	}

	MOUSEMSG msg;
	while (MouseHit()) 
	{
		msg = GetMouseMsg();
		if (msg.mkLButton) 
		{
			res = true;
		}
	}

	return res;
}

这里面操控比较简单,就是鼠标单机/按下空格键,像素鸟都会弹一下。

5鸟死亡,判断gameover的逻辑

// 判断鸟死亡
bool isDie()
{
	if (y + buttom > landY)
		return true;

	if (x + right > pipeX[0] && x + left < pipeX[0] + 52)
	{
		if (y + top < pipeY[0] + 40 || y + buttom > pipeY[0] + 140)
			return true;
	}

	if (x + right > pipeX[1] && x + left < pipeX[1] + 52)
	{
		if (y + top < pipeY[1] + 40 || y + buttom > pipeY[1] + 140)
			return true;
	}

	return false;
}

…完整源码,请关注公众号,后台回复领取

在这里插入图片描述