首先要下载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 positionIMAGE 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;        //上下挡板处于相同水平positiony3 = 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 positionIMAGE 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;     //上下挡板处于相同水平positiony3 = 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;
}

具体效果演示如下:

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

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

  1. c语言像素鸟游戏,掌控板制作Flappy bird(像素鸟)小游戏

    掌控板制作Flappy bird小游戏 大学时期火爆的手机游戏,记得那时候我在做unity开发,还用C#仿照写过这个游戏,用"空格键"操作. 先说一下这游戏的操作:游戏开始,点击屏 ...

  2. 开发一款C语言小游戏——骑士飞行棋

    需求分析 游戏规则和传统的飞行棋一样,支持两人对战 采用100格小型游戏棋盘 游戏规则:对战双方轮流掷骰子控制自己的骑兵前进或后退,在游戏棋盘上设置有关卡 普通 地雷 暂停 时空隧道 幸运轮盘(提供两 ...

  3. python实现2048小游戏(优化版)

    我在之前的博客用python实现2048 中,实现了一些基本的2048游戏功能.但在博客发布之后,收到部分博友留言称游戏在运行时出现了一些问题,同时为了实现在之前博客中说游戏小道具,因此本文对游戏进行 ...

  4. C语言--实现(三)井子棋小游戏(基础版)

    相信大家在生活中肯定知道和了解五子棋的玩法,三子棋的玩法和五子棋一样,当有三个一样的"棋子"连成一条线,不管是横竖斜,谁先完成这个条件,谁就获胜. 那大家有没有想过用编码的形式怎么 ...

  5. C语言小游戏之扫雷完整版

    C语言小游戏之扫雷 一.游戏介绍 二.游戏步骤及实现的功能 1.初始化雷盘 2.打印雷盘 3.随机布置雷 4.玩家排雷 5.防止玩家第一次被炸死 6.统计所选位置周围八个位置中雷的个数 7.递归拓展已 ...

  6. c语言小游戏代码矿井逃生_如何选择编程语言和逃生教程炼狱

    c语言小游戏代码矿井逃生 A few weeks ago, I posted about my experience attempting to learn JavaScript, C#, Pytho ...

  7. 如何用 JavaScript+Canvas 开发一款超级烧脑小游戏?

    作者 | huangjianke 责编 | 伍杏玲 出品 | CSDN(ID:CSDNnews) [CSDN 编者按]据微信最新数据,微信小游戏累计注册用户量已突破10亿.那么初学者如何开发一款好玩又 ...

  8. c语言min函数_C语言探索之旅 | 第一部分第十课:第一个C语言小游戏

    内容简介 前言 准备工作和建议 我的代码 改进方案 第一部分第十一课预告 1. 前言 上一课是 C语言探索之旅 | 第一部分第九课:循环语句 . 经过前面这么多课的努力,我们终于迎来了第一个比较正式的 ...

  9. 20款Adobe AIR小游戏

    20款Adobe AIR小游戏 什么时候有空去包装一下,跑在PlayBook上,我好成天天玩,嘿嘿 http://paranimage.com/20-adobe-air-mini-games/

最新文章

  1. 基于 Prometheus、InfluxDB 与 Grafana 打造监控平台
  2. 推荐10个毕业3年,月入5万技术大神的公众号!
  3. JavaScript 中对象的属性类型
  4. 10.27T2 线性DP+拆分
  5. 卸载后清理干净_想要清理你的Mac?选这几款软件就对了
  6. r语言聚类分析_图说层次聚类分析原理和R语言实现
  7. PHP监測memcache服务端的执行状况
  8. linux安装mysql5.7.29_linux 之centos7搭建mysql5.7.29的详细过程
  9. 2021年北京学校高考成绩查询,2021年北京高考成绩查询时间及入口【官方】
  10. server 2008 mysql 报错 0xc000007b_这十个MySQL经典错误,99%的程序员一定遇到过!你呢?...
  11. python怎么读发音百度翻译-用python实现百度翻译的示例代码
  12. 190304每日一句
  13. Meteor在手机上运行
  14. HaDoop安装配置笔记
  15. Java语言开发的开源商城系统——Javashop简介
  16. c语言粗大误差程序框图,《粗大误差C语言程序.doc
  17. 第八次作业-项目进度计划
  18. IE无法查看源文件原因及应用技巧
  19. 推荐6款UI设计师必备Sketch插件
  20. 青海电大随学随考计算机,[青海电大]17秋随学随考中国现当代文学名著导读(1)作业4资料...

热门文章

  1. k8s pod重启前的日志查看
  2. sql查询今天,近七天,近一个月,近一年的数量统计
  3. HTB walkthrough -- Admirer
  4. 文件夹拷贝,判断,生成当前用户权限的文件夹
  5. 就这样,我走完了程序员的前五年,共勉!
  6. android动态图制作,Android 教程:如何在手机上制作高质量的 GIF 图片
  7. 详细讲解如何映射网络驱动器
  8. 【C语言】文件管理<2>
  9. Lind.DDD.LindAspects方法拦截的介绍
  10. UE4 基本动画设置