VisualStudio2022 C++ EasyX EGE FlappyBird小游戏

欸嘿,我从不托更哈,今天如约带来flappy fird的代码,图形版的哈,很好玩,比其他的黑窗窗好玩多了,前提是你要能找到素材图片【手动滑稽】

求点赞、关注、收藏、转发

还说啥,直接上代码

#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream>
#include<graphics.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
//#define _CRT_SECURE_NO_WANINGS
#define WIDTH					288
#define HEIGHT					603
using namespace std;
IMAGE background;
IMAGE background2;
IMAGE bigBird[3][2];
IMAGE down[2];
IMAGE up[2];
IMAGE banner[2], start[2];
IMAGE gameover;
IMAGE SGB[2];
IMAGE pause[2][2];
HWND hwnd;
int timeSleep = 90;
bool day = false;
bool wings = true;
bool GameOver = false;
bool g1 = true, g2 = true, g3 = true;
int xy = 0, score = 0;
struct bird {
	int x, y;
	int speed;
	int pos;
} flayBird = { 124,304,50,1 };
struct pillar {
	int x, y;
	int height;
} arrayPillar[3];
DWORD WINAPI playMusic(LPVOID lpvoid) {
	mciSendString("open 2.mp3", 0, 0, 0);
	mciSendString("play 2.mp3 wait", 0, 0, 0);
	mciSendString("close 2.mp3", 0, 0, 0);
	return 0;
}
void initPillar(pillar arrayPillar[], int i) {
	arrayPillar[i].height = rand() % 100 + 160;
	arrayPillar[i].x = 288;
	arrayPillar[i].y = 0;
}
void drawPillar(pillar curPillar) {
	putimage(curPillar.x, 0, 52, curPillar.height, &down[0], 0, 320 - curPillar.height, SRCAND);
	putimage(curPillar.x, 0, 52, curPillar.height, &down[1], 0, 320 - curPillar.height, SRCPAINT);
	putimage(curPillar.x, 512 - 320 + curPillar.height, 52, 320 - curPillar.height, &up[0], 0, 0, SRCAND);
	putimage(curPillar.x, 512 - 320 + curPillar.height, 52, 320 - curPillar.height, &up[1], 0, 0, SRCPAINT);
}
bool ButtonHit() {
	MOUSEMSG msg;
	PeekMouseMsg(&msg);
	if (msg.x >= 253 && msg.x <= 288 && msg.y >= 0 && msg.y <= 35) {
		loadimage(&SGB[1], "关闭2.png", 35, 35);
		if (msg.mkLButton) {
			exit(0);
			return true;
		}
	}
	else {
		loadimage(&SGB[1], "关闭.png", 35, 35);
	}
	putimage(253, 0, &SGB[0], SRCAND);
	putimage(253, 0, &SGB[1], SRCPAINT);
	return false;
}
void Start() {
	putimage(55, 85, &banner[0], SRCAND);
	putimage(55, 85, &banner[1], SRCPAINT);
	putimage(86, 250, &start[0], SRCAND);
	putimage(86, 250, &start[1], SRCPAINT);
	MOUSEMSG msg;
	BeginBatchDraw();
	while (1) {
		PeekMouseMsg(&msg);
		if (msg.mkLButton && msg.x >= 86 && msg.x <= 86 + 116 && msg.y >= 250 && msg.y <= 320) {
			break;
		}
		FlushBatchDraw();
	}
	EndBatchDraw();
}
void loadResource() {
	loadimage(&background, "background.png");
	loadimage(&background2, "background2.png");
	loadimage(&down[0], "downy.png");
	loadimage(&down[1], "down.png");
	loadimage(&up[0], "upy.png");
	loadimage(&up[1], "up.png");
	loadimage(&bigBird[0][1], "bird1.png", 48, 48);
	loadimage(&bigBird[0][0], "bird1y.png", 48, 48);
	loadimage(&bigBird[1][1], "bird2.png", 48, 48);
	loadimage(&bigBird[1][0], "bird2y.png", 48, 48);
	loadimage(&bigBird[2][1], "bird3.png", 48, 48);
	loadimage(&bigBird[2][0], "bird3y.png", 48, 48);
	loadimage(&banner[0], "bannery.png");
	loadimage(&banner[1], "banner.png");
	loadimage(&start[0], "starty.png");
	loadimage(&start[1], "start.png");
	loadimage(&gameover, "GameOver.jpeg");
	loadimage(&SGB[0], "关闭掩码图.png", 35, 35);
	loadimage(&SGB[1], "关闭.png", 35, 35);
	loadimage(&pause[0][0], "暂停1y.png", 35, 35);
	loadimage(&pause[0][1], "暂停1.png", 35, 35);
	loadimage(&pause[1][0], "暂停2y.png", 35, 35);
	loadimage(&pause[1][1], "暂停2.png", 35, 35);
}
void drawBird() {
	switch (flayBird.pos) {
	case 1:
		putimage(flayBird.x, flayBird.y, &bigBird[0][0], SRCAND);
		putimage(flayBird.x, flayBird.y, &bigBird[0][1], SRCPAINT);
		break;
	case 2:
		putimage(flayBird.x, flayBird.y, &bigBird[1][0], SRCAND);
		putimage(flayBird.x, flayBird.y, &bigBird[1][1], SRCPAINT);
		break;
	case 3:
		putimage(flayBird.x, flayBird.y, &bigBird[2][0], SRCAND);
		putimage(flayBird.x, flayBird.y, &bigBird[2][1], SRCPAINT);
		break;
	}
}
void keyDown() {
	if (_kbhit()) {
		switch (_getch()) {
		default:
			break;
		}
		CreateThread(NULL, NULL, playMusic, NULL, NULL, NULL);
		flayBird.y -= flayBird.speed;
	}
}
void hit() {
	if (flayBird.y + 48 >= 512 || flayBird.y <= 0) {
		GameOver = true;
	}
	for (int i = 0; i < 3; i++) {
		if (flayBird.x + 48 >= arrayPillar[i].x && flayBird.x + 48 <= arrayPillar[i].x + 52 || flayBird.x >= arrayPillar[i].x && flayBird.x <= arrayPillar[i].x + 52) {
			if (flayBird.y + 48 <= arrayPillar[i].height || flayBird.y <= arrayPillar[i].height) {
				GameOver = true;
			}
			else if (flayBird.y >= 512 - 320 + arrayPillar[i].height || flayBird.y + 48 >= 512 - 320 + arrayPillar[i].height) {
				GameOver = true;
			}
		}
	}
}
void End() {
	hwnd = initgraph(300, 300);
	HWND hd = GetDesktopWindow();
	RECT rect;
	GetClientRect(hd, &rect);
	int width = (rect.right - rect.left);
	int height = (rect.bottom - rect.top);
	std::cout << "client width:" << width << std::endl;
	std::cout << "client height:" << height << std::endl;
	LONG style = GetWindowLong(hwnd, GWL_STYLE);
	style &= ~(WS_BORDER | WS_CAPTION | WS_THICKFRAME);
	SetWindowLong(hwnd, GWL_STYLE, style);
	SetWindowPos(hwnd, NULL, (width - 300) / 2, (height - 300) / 2, 300, 300, NULL);
	putimage(0, 0, &gameover);
	EndBatchDraw();
}
int main() {
	hwnd = initgraph(WIDTH, HEIGHT);
	HWND hd = GetDesktopWindow();
	RECT rect;
	GetClientRect(hd, &rect);
	int w = (rect.right - rect.left);
	int h = (rect.bottom - rect.top);
	LONG style = GetWindowLong(hwnd, GWL_STYLE);
	style &= ~(WS_BORDER | WS_CAPTION | WS_THICKFRAME);
	SetWindowLong(hwnd, GWL_STYLE, style);
	SetWindowPos(hwnd, NULL, (w - WIDTH) / 2, (h - HEIGHT) / 2, WIDTH, HEIGHT, NULL);
	mciSendString("open 1.mp3 alias 1", 0, 0, 0);
	mciSendString("play 1 repeat", 0, 0, 0);
	DWORD t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0;
	srand(time(NULL));
	loadResource();
	for (int i = 0; i < 3; i++) {
		initPillar(arrayPillar, i);
		arrayPillar[i].x = 288 + i * 150;
	}
	putimage(0, 0, &background, SRCPAINT);
	putimage(0, 512, &background2, SRCPAINT);
	Start();
	cleardevice();
	settextcolor(RED);
	settextstyle(30, 0, "华文琥珀");
	setbkmode(TRANSPARENT);
	char ch[100];
	BeginBatchDraw();
	while (GameOver == false) {
		cleardevice();
		putimage(0, 0, &background, SRCPAINT);
		putimage(xy, 512, &background2, SRCPAINT);
		keyDown();
		drawBird();
		hit();
		for (int i = 0; i < 3; i++) {
			drawPillar(arrayPillar[i]);
		}
		if (t1 - t2 >= 100) {
			flayBird.y += 6;
			t2 = t1;
		}
		if (t3 - t4 >= timeSleep) {
			for (int i = 0; i < 3; i++) {
				arrayPillar[i].x -= 5;
				if (arrayPillar[i].x < (-52 - 150)) {
					initPillar(arrayPillar, i);
					switch (i) {
					case 0:
						g1 = true;
						break;
					case 1:
						g2 = true;
						break;
					case 2:
						g3 = true;
						break;
					}
				}
				if (arrayPillar[i].x < flayBird.x) {
					switch (i) {
					case 0:
						if (g1 == true) {
							g1 = false;
							score++;
						}
						break;
					case 1:
						if (g2 == true) {
							g2 = false;
							score++;
						}
						break;
					case 2:
						if (g3 == true) {
							g3 = false;
							score++;
						}
						break;
					}
				}
			}
			t4 = t3;
		}
		if (t5 - t6 >= timeSleep / 4) {
			xy--;
			t6 = t5;
		}
		if (t7 - t8 >= 60000) {
			switch (day) {
			case false:
				loadimage(&background, "background.png");
				day = true;
				break;
			case true:
				loadimage(&background, "background0.png");
				day = false;
				break;
			}
			t8 = t7;
		}
		if (t9 - t10 >= 10000) {
			if (timeSleep - 2 >= 0) {
				timeSleep -= 2;
			}
			t10 = t9;
		}
		t1 = GetTickCount();
		t3 = GetTickCount();
		t5 = GetTickCount();
		t7 = GetTickCount();
		t9 = GetTickCount();
		if (wings) flayBird.pos++;
		else flayBird.pos--;
		switch (flayBird.pos) {
		case 0:
			flayBird.pos = 2;
			wings = true;
			break;
		case 4:
			flayBird.pos = 2;
			wings = false;
			break;
		}
		if (xy < -23) xy = -1;
		if (score == 999) GameOver = true;
		sprintf(ch, "%d", score);
		int spaceH = (getwidth() - textwidth(ch)) / 2;
		outtextxy(spaceH, 0, ch);
		ButtonHit();
		FlushBatchDraw();
	}
	putimage(0, 0, &background, SRCPAINT);
	putimage(xy, 512, &background2, SRCPAINT);
	Sleep(1000);
	EndBatchDraw();
	End();
	MessageBox(hwnd, "         GameOver", "温馨提示", MB_OK);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++小盆友

你的鼓励将是我创作的最大动力呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值