其他分享
首页 > 其他分享> > 简易小游戏——Flappy Bird

简易小游戏——Flappy Bird

作者:互联网

软件使用:EasyX、VS2013
效果图:在这里插入图片描述
源代码:

#include <stdint.h>
#include <graphics.h>
#include <conio.h>
int main()
{
	int score = 0;
	float width = 600.0, height = 400.0, gravity = 0.25;
	float rect_width = 40.0, rect_height = 125.0;
	float rect_vx = -1.9;
	float one_top_rect_height = 125.0, one_bot_rect_height = 125.0, one_top_left_x = width / 5, one_top_left_y = 0.0;
	float two_top_rect_height = 125.0, two_bot_rect_height = 125.0, two_top_left_x = width * 4 / 5, two_top_left_y = 0.0;
	float radius = 20, ball_x = width * 2 / 5, ball_y = height - radius, ball_vy = 0.0;
	initgraph(width, height);

	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if (input == ' ')
				ball_vy = -6.0;
		}
		if ((ball_x + radius >= one_top_left_x && ball_y - radius <= one_top_left_y + one_top_rect_height && ball_x - radius <= one_top_left_x + rect_width) ||
			(ball_x + radius >= one_top_left_x && ball_y + radius >= height - one_bot_rect_height && ball_x - radius <= one_top_left_x + rect_width) ||
			(ball_x + radius >= two_top_left_x && ball_y - radius <= two_top_left_y + two_top_rect_height && ball_x - radius <= two_top_left_x + rect_width) ||
			(ball_x + radius >= two_top_left_x && ball_y + radius >= height - two_bot_rect_height && ball_x - radius <= two_top_left_x + rect_width))
		{
			score = 0;
			Sleep(100);
		}
		ball_vy += gravity;
		ball_y += ball_vy;
		if (ball_y >= height - radius)
		{
			ball_vy = 0;
			ball_y = height - radius;
		}
		one_top_left_x += rect_vx;
		two_top_left_x += rect_vx;
		if (one_top_left_x + rect_width <= 0)
		{
			score += 1;
			one_top_left_x = width;
			int tmp = one_top_rect_height, diff = 0;
			one_top_rect_height = rand() % int(height / 2 + 50);
			diff = abs(int(one_top_rect_height - tmp));
			if (one_top_rect_height < tmp)
				one_bot_rect_height += diff;
			else
				one_bot_rect_height -= diff;
		}
		if (two_top_left_x + rect_width <= 0)
		{
			score += 1;
			two_top_left_x = width;
			int tmp = two_top_rect_height, diff = 0;
			two_top_rect_height = rand() % int(height / 2 + 50);
			diff = abs(int(two_top_rect_height - tmp));
			if (two_top_rect_height < tmp)
				two_bot_rect_height += diff;
			else
				two_bot_rect_height -= diff;
		}
		cleardevice();
		fillcircle(ball_x, ball_y, radius);
		fillrectangle(one_top_left_x, one_top_left_y, one_top_left_x + rect_width, one_top_left_y + one_top_rect_height);
		fillrectangle(one_top_left_x, height - one_bot_rect_height, one_top_left_x + rect_width, height);
		fillrectangle(two_top_left_x, two_top_left_y, two_top_left_x + rect_width, one_top_left_y + two_top_rect_height);
		fillrectangle(two_top_left_x, height - two_bot_rect_height, two_top_left_x + rect_width, height);
		TCHAR s[20];
		_stprintf_s(s, _T("%d"), score);
		settextstyle(40, 0, _T("宋体"));
		outtextxy(50, 30, s);
		Sleep(10);
	}
	_getch();
	return 0;
}

标签:ball,Flappy,top,two,height,小游戏,Bird,rect,left
来源: https://blog.csdn.net/weixin_43318827/article/details/113809937