Flappy Bird 一款C语言小游戏(图形优化版)(成功解决EasyX中putimage()未能实现第二张图片显示问题)

首先要下载EasyX库,如果网上找不到或者想要老版本的可以在评论区或者直接私信我

首先导入图片

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

int main()
{
	initgraph(350, 600);
	IMAGE img_bk;			//define IMAGE对象
	loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
	putimage(0, 0, &img_bk);
	IMAGE img_bd;
	loadimage(&img_bd, "D:\\bird2.jpg");
	putimage(100, 200, &img_bd);
	getch();				//等待输入,以防立刻退出
	closegraph();
	return 0;
}

图片资源:
bird2.jpg
在这里插入图片描述
background.jpg
在这里插入图片描述
运行效果如下:
在这里插入图片描述

遮罩图使用

由于小鸟无法完美融入背景,所以我们需要遮罩图
在这里插入图片描述
详细代码如下:

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

int main()
{
	initgraph(350, 600);
	IMAGE img_bk;			//define IMAGE对象
	loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
	putimage(0, 0, &img_bk);
	IMAGE img_bd1, img_bd2;
	loadimage(&img_bd1, "D:\\bird1.jpg");
	loadimage(&img_bd2, "D:\\bird2.jpg");
	putimage(100, 200, &img_bd1, NOTSRCERASE);
	putimage(100, 200, &img_bd2, SRCINVERT);
	getch();				//等待输入,以防立刻退出
	closegraph();
	return 0;
}

终极版Flappy Bird

这里背景图我是用的我自己的,各位可以按自己的想法DIY背景图乃至其他图形
在这里插入图片描述
详细代码如下:

#include<graphics.h>
#include<windows.h>
#include<stdio.h>
#include<mmsystem.h>
#include<conio.h>
#include<stdlib.h>


#pragma comment(lib, "Winmm.lib")		//引入Windows Multimedia API
HWND hwnd;		//hWnd:标识将被创建的消息框的拥有窗口.如果此参数为NULL,那么消息框没有拥有窗口
int score = 0;			//得分
int x2, y2;				//bar_up的position
int x3, y3;				//bar_down的position
int x, y;				//鸟's position

IMAGE img_bk;			//define IMAGE对象
IMAGE img_bd1, img_bd2;
IMAGE img_bar_up1, img_bar_up2;
IMAGE img_bar_down1, img_bar_down2;
IMAGE img_fail;


void startup()
{
	initgraph(350, 600);
	loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
	loadimage(&img_bd1, "D:\\bird1.jpg");
	loadimage(&img_bd2, "D:\\bird2.jpg");
	loadimage(&img_bar_up1, "D:\\bar_up1.gif");
	loadimage(&img_bar_up2, "D:\\bar_up2.gif");
	loadimage(&img_bar_down1, "D:\\bar_down1.gif");
	loadimage(&img_bar_down2, "D:\\bar_down2.gif");
	loadimage(&img_fail, "D:\\failed.png");
	x = 50;
	y = 200;
	BeginBatchDraw();
	x2 = 360;
	y2 = -300;
	score = 0;
	x3 = x2;		//上下挡板处于相同水平position
	y3 = 700 + y2;		//开始初始化挡板position
	//y3和y2有公式关系
	//当y3 = 600 + y2时,两档板无缝连接
	mciSendString("open D:\\tb.mp3 alias bkmusic", NULL, 0, NULL);		//打开音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);		//循环播放
}

void show()
{
	putimage(0, 0, &img_bk);		//显示背景
	putimage(x2, y2, &img_bar_up1, NOTSRCERASE);
	putimage(x2, y2, &img_bar_up2, SRCINVERT);
	putimage(x3, y3, &img_bar_down1, NOTSRCERASE);
	putimage(x3, y3, &img_bar_down2, SRCINVERT);
	putimage(x, y, &img_bd1, NOTSRCERASE);
	putimage(x, y, &img_bd2, SRCINVERT);
	setbkmode(TRANSPARENT);
	settextstyle(28, 0, _T("Consolas"));		//字体大小
	setcolor(YELLOW);				//字体颜色
	outtextxy(230, 10, "得分:"); 
	TCHAR s[5];
	_stprintf(s, _T("%d"), score);		//高版本VC推荐使用_stprintf_s函数
	outtextxy(310, 10, s);
	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()
{
	if(y <= 570)
		y += 10;
	x3 -= 1;
	x2 -= 1;
	int y4 = 600 + y2;		//y4上挡板的底线, y3下挡板的上线
	if((x <= x2 + 120) && (x >= x2))
	{
		if((y < y4) || (y > y3))	
		{
			loadimage(&img_fail, "D:\\failed.png");
			mciSendString("close bkmusic", NULL, 0, NULL);
			Sleep(1);
			putimage(0, 0, &img_fail);
			mciSendString("close fdmusic", NULL, 0, NULL);
			mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
			mciSendString("play fdmusic", NULL, 0, NULL);
			Sleep(600);
			system("pause");
			exit(0);
		}
	}

	if(x == x2 + 120)		//鸟和挡板相遇
	{
		if((y >= y4) && (y <= y3))	
			score++;
		else
		{
			loadimage(&img_fail, "D:\\failed.png");
			mciSendString("close bkmusic", NULL, 0, NULL);
			Sleep(1);
			putimage(200, 200, &img_fail);
			mciSendString("close fdmusic", NULL, 0, NULL);
			mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
			mciSendString("play fdmusic", NULL, 0, NULL);
			Sleep(100);
			MessageBox(hwnd, TEXT("Game Over"), _T("结果"), MB_OK);
			system("pause");
			exit(0);
		}
	}
	if(y <= 18 || y >= 579)
	{
		loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
		loadimage(&img_fail, "D:\\failed.png");
		putimage(0, 0, &img_bk);		//显示背景
		mciSendString("open D:\\tb.mp3 alias bkmusic", NULL, 0, NULL);		//打开音乐
		mciSendString("play bkmusic repeat", NULL, 0, NULL);		//循环播放
		Sleep(300);
		loadimage(&img_fail, "D:\\failed.png");
		mciSendString("close bkmusic", NULL, 0, NULL);
		Sleep(1);
		cleardevice();
		getimage(&img_fail, 70, 70, 60, 60);
		putimage(200, 200, &img_fail);
		mciSendString("close fdmusic", NULL, 0, NULL);
		mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
		mciSendString("play fdmusic", NULL, 0, NULL);
		Sleep(100);
		MessageBox(hwnd, TEXT("Game Over"), _T("结果"), MB_OK);
		putimage(x3, y3, &img_fail);
		system("pause");
		exit(0);
	}
	if(x2 < -140)
	{
		x2 = 360;
		x3 = x2;          
		int tmp = rand() * 10 % 400 + 100;
		y3 = tmp;
		y2 = y3 - 700;
	}
	
	setbkmode(TRANSPARENT);
	settextstyle(28, 0, _T("Consolas"));		//字体大小
	setcolor(YELLOW);				//字体颜色
	outtextxy(230, 10, "得分:"); 
	TCHAR s[5];
	_stprintf(s, _T("%d"), score);		// 高版本 VC 推荐使用 _stprintf_s 函数
	outtextxy(310, 10, s);
	//Sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ' && y > 20)
		{
			y -= 30;
			mciSendString("close jpmusic", NULL, 0, NULL);
			mciSendString("open D:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);
			mciSendString("play jpmusic", NULL, 0, NULL);
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}

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

重新修改优化版(实现了跳出失败图形页面)

花了很长时间一直在琢磨一个问题,为什么连Game Over音效都出来了而Game Over的图片不能显示出来,页面上仍是背景图

我做了很多次实验

比方说这个:目的是检验putimage能不能让程序先后显示两张背景图

#include<graphics.h>
#include<windows.h>
#include<stdio.h>
#include<mmsystem.h>
#include<conio.h>
#include<stdlib.h>


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

int main()
{
	initgraph(350, 600);
	IMAGE img_bk;			//define IMAGE对象
	IMAGE img_fail;
	loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
	loadimage(&img_fail, "D:\\failed.png");
	putimage(0, 0, &img_bk);		//显示背景
	mciSendString("open D:\\tb.mp3 alias bkmusic", NULL, 0, NULL);		//打开音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);		//循环播放
	Sleep(600);
	loadimage(&img_fail, "D:\\failed.png");
	mciSendString("close bkmusic", NULL, 0, NULL);
	Sleep(1);
	putimage(0, 0, &img_fail);
	mciSendString("close fdmusic", NULL, 0, NULL);
	mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
	mciSendString("play fdmusic", NULL, 0, NULL);
	Sleep(10);
	system("pause");
	exit(0);
}

在这里插入图片描述

答案是可以的,但是为什么大体框架都差不多,都是为了实现先后输出两个不一样的背景图,但前者可以实现,后者却毫无动静,为此我又在网上找了各种相关的解答,用了getimage()函数,后来发现其函数本身就不是用来解决这个的,后来又用messagebox函数,好用是好用,但是仍不能达到我想要的那种效果,毕竟弹出那么小的弹窗告诉你失败了,感觉太轻巧了。。。

最终我想到了一个办法,就是先让前面的图片清空,再加载另外背景图,终于实现了(虽然我不知道为什么putimage()在大型一点的代码里就不起作用了,如果有知道的朋友欢迎来评论区抒发自己的看法)

GAME OVER图片资源:
在这里插入图片描述
相关音频资源网上都可以搜到,我用的网址http://www.aigei.com/s?q=%E5%A4%B1%E6%95%97&type=sound&term=mp3&detailTab=file

详细代码如下:

#include<graphics.h>
#include<windows.h>
#include<stdio.h>
#include<mmsystem.h>
#include<conio.h>
#include<stdlib.h>


#pragma comment(lib, "Winmm.lib")		//引入Windows Multimedia API
HWND hwnd;		//hWnd:标识将被创建的消息框的拥有窗口.如果此参数为NULL,那么消息框没有拥有窗口
int score = 0;			//得分
int x2, y2;				//bar_up的position
int x3, y3;				//bar_down的position
int x, y;				//鸟's position

IMAGE img_bk;			//define IMAGE对象
IMAGE img_bd1, img_bd2;
IMAGE img_bar_up1, img_bar_up2;
IMAGE img_bar_down1, img_bar_down2;



void startup()
{
	initgraph(350, 600);
	loadimage(&img_bk, "D:\\background.jpg");		//读取图片到IMAGE对象
	loadimage(&img_bd1, "D:\\bird1.jpg");
	loadimage(&img_bd2, "D:\\bird2.jpg");
	loadimage(&img_bar_up1, "D:\\bar_up1.gif");
	loadimage(&img_bar_up2, "D:\\bar_up2.gif");
	loadimage(&img_bar_down1, "D:\\bar_down1.gif");
	loadimage(&img_bar_down2, "D:\\bar_down2.gif");
	
	x = 50;
	y = 200;
	BeginBatchDraw();
	x2 = 360;
	y2 = -300;
	score = 0;
	x3 = x2;		//上下挡板处于相同水平position
	y3 = 700 + y2;		//开始初始化挡板position
	//y3和y2有公式关系
	//当y3 = 600 + y2时,两档板无缝连接
	mciSendString("open D:\\tb.mp3 alias bkmusic", NULL, 0, NULL);		//打开音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);		//循环播放
}

void show()
{
	putimage(0, 0, &img_bk);		//显示背景
	putimage(x2, y2, &img_bar_up1, NOTSRCERASE);
	putimage(x2, y2, &img_bar_up2, SRCINVERT);
	putimage(x3, y3, &img_bar_down1, NOTSRCERASE);
	putimage(x3, y3, &img_bar_down2, SRCINVERT);
	putimage(x, y, &img_bd1, NOTSRCERASE);
	putimage(x, y, &img_bd2, SRCINVERT);
	setbkmode(TRANSPARENT);
	settextstyle(28, 0, _T("Consolas"));		//字体大小
	setcolor(YELLOW);				//字体颜色
	outtextxy(230, 10, "得分:"); 
	TCHAR s[5];
	_stprintf(s, _T("%d"), score);		//高版本VC推荐使用_stprintf_s函数
	outtextxy(310, 10, s);
	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()
{
	if(y <= 570)
		y += 10;
	x3 -= 1;
	x2 -= 1;
	int y4 = 600 + y2;		//y4上挡板的底线, y3下挡板的上线

	if(y <= 18 || y >= 579)			//小鸟出边界情况
	{
		cleardevice();
		IMAGE img_fail;
		loadimage(&img_fail, "D:\\failed.png");
		BeginBatchDraw();
		mciSendString("close bkmusic", NULL, 0, NULL);
		Sleep(1);
		putimage(0, 0, &img_fail);
		mciSendString("close fdmusic", NULL, 0, NULL);
		mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
		mciSendString("play fdmusic", NULL, 0, NULL);
		FlushBatchDraw();
		Sleep(100);
		MessageBox(hwnd, TEXT("Game Over"), _T("结果"), MB_OK);
		system("pause");
		exit(0);
	}

	if(x == x2 + 120)		//鸟和挡板相遇
	{
		if((y >= y4) && (y <= y3))	
			score++;
		else
		{
			EndBatchDraw();
			cleardevice();
			IMAGE img_fail;
			loadimage(&img_fail, "D:\\failed.png");
			BeginBatchDraw();
			mciSendString("close bkmusic", NULL, 0, NULL);
			Sleep(1);
			getimage(&img_fail, 70, 70, 60, 60);
			putimage(0, 0, &img_fail);
			mciSendString("close fdmusic", NULL, 0, NULL);
			mciSendString("open D:\\failed2.mp3 alias fdmusic", NULL, 0, NULL);
			mciSendString("play fdmusic", NULL, 0, NULL);
			FlushBatchDraw();
			Sleep(100);
			MessageBox(hwnd, TEXT("Game Over"), _T("结果"), MB_OK);
			system("pause");
			exit(0);
		}
	}
	
	if(x2 < -140)			//remake挡板position
	{
		x2 = 360;
		x3 = x2;          
		int tmp = rand() * 10 % 400 + 100;
		y3 = tmp;
		y2 = y3 - 700;
	}
	
	setbkmode(TRANSPARENT);
	settextstyle(28, 0, _T("Consolas"));		//字体大小
	setcolor(YELLOW);				//字体颜色
	outtextxy(230, 10, "得分:"); 
	TCHAR s[5];
	_stprintf(s, _T("%d"), score);		// 高版本 VC 推荐使用 _stprintf_s 函数
	outtextxy(310, 10, s);
	//Sleep(1);
}

void updateWithInput()
{
	char input;
	if(kbhit())
	{
		input = getch();
		if(input == ' ' && y > 20)
		{
			y -= 30;
			mciSendString("close jpmusic", NULL, 0, NULL);
			mciSendString("open D:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);
			mciSendString("play jpmusic", NULL, 0, NULL);
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}

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

具体效果演示如下:
在这里插入图片描述

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值