跟着小白一起学鸿蒙—[番外]一起学做FlappyBird
跟着小白一起学鸿蒙—[番外]一起学做FlappyBird
作者:王石 2022-11-14 17:01:34系统 OpenHarmony 本文主要介绍小游戏的开发,画布功能的使用。想了解更多关于开源的内容,请访问:
51CTO开源基础软件社区
https://ost.51cto.com
简介
记得很久以前有个大火的像素游戏叫FlappyBird,我们就一起看看如何能用OpenHarmony学习做个FlappyBird。本文中引用的图片资源均来自与Github。
开发
1、HAP应用建立
这里我们就不赘述Hap项目的建立过程,以下就是基础的Hap的page文件:index.ets
build() {Row() {Column() {Canvas(this.context).width('100%').height('100%').onClick((ev: ClickEvent) => {console.info("click!!")//响应鼠标左击this.doClick()}).onReady(() =>{//绘制基础this.context.imageSmoothingEnabled = falsethis.drawBlock()})}.width('100%')}.height('100%').backgroundImage($r("app.media.backgroundday")).backgroundImageSize(ImageSize.Cover)}build是基础页面的构造函数,用于界面的元素构造,其他的页面的生命周期函数如下:
declare class CustomComponent {/** * Customize the pop-up content constructor. * @since 7 */build(): void;/** * aboutToAppear Method * @since 7 */aboutToAppear?(): void;/** * aboutToDisappear Method * @since 7 */aboutToDisappear?(): void;/** * onPageShow Method * @since 7 */onPageShow?(): void;/** * onPageHide Method * @since 7 */onPageHide?(): void;/** * onBackPress Method * @since 7 */onBackPress?(): void;}2、Canvas介绍
canvas是画布组件用于自定义绘制图形,具体的API页面如下:
https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-components-canvas-canvas-0000001333641081。
页面显示前会调用aboutToAppear()函数,此函数为页面生命周期函数。
canvas组件初始化完毕后会调用onReady()函数,函数内部实现小游戏的初始页面的绘制。
(1)初始化页面数据
drawBlock() {this.context.clearRect(0,0,this.context.width,this.context.height)this.context.drawImage( this.baseImg,this.baseX,this.baseY,500,300)switch(this.flappyState) {case 0:this.context.drawImage( this.messageImg,this.startX,this.startY,300,500)this.drawBird()break;case 1:this.drawBird()this.context.drawImage( this.pipegreenImg,this.pipeX,this.pipeY,50,150)break;case 2:this.context.drawImage( this.gameoverImg,this.startX,this.startY*3,300,90)break}}页面状态有三:
0:等待开始界面1:游戏进行2:游戏结束(2)绘制Bird
drawBird() {switch(this.birdType) {case 0:this.context.drawImage( this.midbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)breakcase 1:this.context.drawImage( this.upbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)break;case 2:this.context.drawImage( this.downbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)break;default:break;}}小鸟飞行状态有三种:
翅膀在中间:0翅膀在上:1翅膀在下:23、游戏逻辑
简单的小游戏主体游戏逻辑为:等待开始,开始,结束流程图如下:
graph LR等待开始 --> click[点击]click[点击] --> 游戏开始游戏开始 --> 点击 --> |游戏开始|小鸟飞,水管动 --> |小鸟碰到水管| 游戏结束 --> 点击 --> |游戏结束| 等待开始小鸟飞,水管动 --> |小鸟没碰到水管| 游戏继续 --> 点击doClick() {switch (this.flappyState) {case 0:{// 开始this.flappyState = 1break}case 1:{//上下飞//this.flappyState = 2this.slotY -= this.flyHeightconsole.log(this.slotY.toString())break}case 2:{//由结束到待开始this.flappyState = 0this.slotY = this.slotStartYthis.pipeX = this.pipeStartXbreak}default:break}this.drawBlock()}4、完整逻辑
@Entry@Componentstruct Index {@State message: string = 'Hello World'private baseImg:ImageBitmap = new ImageBitmap("common/images/base.png")private messageImg:ImageBitmap = new ImageBitmap("common/images/message.png")private zeroImg:ImageBitmap = new ImageBitmap("common/images/0.png")private gameoverImg:ImageBitmap = new ImageBitmap("common/images/gameover.png")private upbirdImg:ImageBitmap = new ImageBitmap("common/images/bluebirdupflap.png")private midbirdImg:ImageBitmap = new ImageBitmap("common/images/bluebirdmidflap.png")private downbirdImg:ImageBitmap = new ImageBitmap("common/images/bluebirddownflap.png")private pipegreenImg:ImageBitmap = new ImageBitmap("common/images/pipegreen.png")private settings: RenderingContextSettings = new RenderingContextSettings(true);private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);private flappyState: number = 0private startX = 30;private startY = 100;private slotStartY = 410;private slotX = 50;private slotY = this.slotStartY;private baseX = 0;private baseY = 650;private pipeStartX = 330;private pipeX = this.pipeStartX;private pipeY = 500;private birdH = 60;private birdW = 50;private birdTimer: number;private birdType: number = 0;private count = 1;private flyHeight = 20;private pipeMove = 10;drawBird() {switch(this.birdType) {case 0:this.context.drawImage( this.midbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)breakcase 1:this.context.drawImage( this.upbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)break;case 2:this.context.drawImage( this.downbirdImg,this.slotX,this.slotY,this.birdH,this.birdW)break;default:break;}}drawBlock() {this.context.clearRect(0,0,this.context.width,this.context.height)this.context.drawImage( this.baseImg,this.baseX,this.baseY,500,300)switch(this.flappyState) {case 0:this.context.drawImage( this.messageImg,this.startX,this.startY,300,500)this.drawBird()break;case 1:this.drawBird()this.context.drawImage( this.pipegreenImg,this.pipeX,this.pipeY,50,150)break;case 2:this.context.drawImage( this.gameoverImg,this.startX,this.startY*3,300,90)break}}doClick() {switch (this.flappyState) {case 0:{// 开始this.flappyState = 1break}case 1:{//上下飞//this.flappyState = 2this.slotY -= this.flyHeightconsole.log(this.slotY.toString())break}case 2:{//由结束到待开始this.flappyState = 0this.slotY = this.slotStartYthis.pipeX = this.pipeStartXbreak}default:break}this.drawBlock()}doFly(): void {console.log("dofly ------ !!")this.birdType += 1if (this.birdType/5 == 0) {this.message = "dofly ---555--- !!"}}async sleep(ms: number) {return new Promise((r) => {setInterval(() => {this.birdType += 1this.message = this.birdType.toString()if (this.birdType == 3) {this.birdType = 0}console.log(this.message)if (this.flappyState == 1) {this.pipeX -= this.pipeMoveif (this.pipeX < 0) {this.pipeX = 330}this.slotY += this.flyHeight/5}if ((((this.pipeX-this.slotX) <= this.birdW) && ((this.pipeY-this.slotY) <= this.birdH)) ||this.pipeY >= this.baseY) {this.flappyState = 2}this.drawBlock()}, ms)})}aboutToDisappear() {}aboutToAppear() {this.sleep(200)}build() {Row() {Column() {Canvas(this.context).width('100%').height('100%').onClick((ev: ClickEvent) => {console.info("click!!")this.doClick()}).onReady(() =>{this.context.imageSmoothingEnabled = falsethis.drawBlock()})}.width('100%')}.height('100%').backgroundImage($r("app.media.backgroundday")).backgroundImageSize(ImageSize.Cover)}}遗留问题:
水管只在下层显示:可以在上层显示;地面没有让动游戏声音问题:目前ohos不支持音频播放资源音频,看之后版本是否支持DevEcoy用setInterval重绘canvas会导致ide崩溃5、获取源码
见附件https://gitee.com/wshikh/ohosflappybird。
总结
本文主要介绍了小游戏的开发,画布功能的使用。
想了解更多关于开源的内容,请访问:
51CTO开源基础软件社区
https://ost.51cto.com。
责任编辑:jianghua 来源:51CTO开源基础软件社区 游戏开发画布功能推荐系统
电脑公司Ghost Win8.1 x32 精选纯净版2022年7月(免激活) ISO镜像高速下载
语言:中文版系统大小:2.98GB系统类型:Win8电脑公司Ghost Win8.1x32位纯净版V2022年7月版本集成了自2022流行的各种硬件驱动,首次进入系统即全部硬件已安装完毕。电脑公司Ghost Win8.1x32位纯净版具有更安全、更稳定、更人性化等特点。集成最常用的装机软件,精心挑选的系统维护工具,加上绿茶独有
微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载
语言:中文版系统大小:5.13GB系统类型:Win11微软Win11原版22H2下载_Win11GHOST 免 激活密钥 22H2正式版64位免费下载系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Win11 64位 Office办公版(免费)优化 1、保留 Edge浏览器。 2、隐藏“操作中心”托盘图标。 3、保留常用组件(微软商店,计算器,图片查看器等)。 5、关闭天气资讯。
Win11 21H2 官方正式版下载_Win11 21H2最新系统免激活下载
语言:中文版系统大小:4.75GB系统类型:Win11Ghost Win11 21H2是微软在系统方面技术积累雄厚深耕多年,Ghost Win11 21H2系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。Ghost Win11 21H2是微软最新发布的KB5019961补丁升级而来的最新版的21H2系统,以Windows 11 21H2 22000 1219 专业版为基础进行优化,保持原汁原味,系统流畅稳定,保留常用组件
windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载
语言:中文版系统大小:5.31GB系统类型:Win11windows11中文版镜像 微软win11正式版简体中文GHOST ISO镜像64位系统下载,微软win11发布快大半年了,其中做了很多次补丁和修复一些BUG,比之前的版本有一些功能上的调整,目前已经升级到最新版本的镜像系统,并且优化了自动激活,永久使用。windows11中文版镜像国内镜像下载地址微软windows11正式版镜像 介绍:1、对函数算法进行了一定程度的简化和优化
微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载
语言:中文版系统大小:5.31GB系统类型:Win11微软windows11正式版GHOST ISO镜像 win11下载 国内最新版渠道下载,微软2022年正式推出了win11系统,很多人迫不及待的要体验,本站提供了最新版的微软Windows11正式版系统下载,微软windows11正式版镜像 是一款功能超级强大的装机系统,是微软方面全新推出的装机系统,这款系统可以通过pe直接的完成安装,对此系统感兴趣,想要使用的用户们就快来下载
微软windows11系统下载 微软原版 Ghost win11 X64 正式版ISO镜像文件
语言:中文版系统大小:0MB系统类型:Win11微软Ghost win11 正式版镜像文件是一款由微软方面推出的优秀全新装机系统,这款系统的新功能非常多,用户们能够在这里体验到最富有人性化的设计等,且全新的柔软界面,看起来非常的舒服~微软Ghost win11 正式版镜像文件介绍:1、与各种硬件设备兼容。 更好地完成用户安装并有效地使用。2、稳定使用蓝屏,系统不再兼容,更能享受无缝的系统服务。3、为
雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载
语言:中文版系统大小:4.75GB系统类型:雨林木风Windows11专业版 Ghost Win11官方正式版 (22H2) 系统下载在系统方面技术积累雄厚深耕多年,打造了国内重装系统行业的雨林木风品牌,其系统口碑得到许多人认可,积累了广大的用户群体,雨林木风是一款稳定流畅的系统,一直以来都以用户为中心,是由雨林木风团队推出的Windows11国内镜像版,基于国内用户的习惯,做了系统性能的优化,采用了新的系统
雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO
语言:中文版系统大小:5.91GB系统类型:Win7雨林木风win7旗舰版系统下载 win7 32位旗舰版 GHOST 免激活镜像ISO在系统方面技术积累雄厚深耕多年,加固了系统安全策略,雨林木风win7旗舰版系统在家用办公上跑分表现都是非常优秀,完美的兼容各种硬件和软件,运行环境安全可靠稳定。win7 32位旗舰装机版 v2019 05能够帮助用户们进行系统的一键安装、快速装机等,系统中的内容全面,能够为广大用户
相关文章
- 怎样安装固态硬盘?怎样给固态硬盘分区?
- 10.9.2 win7
- System 76 将不会发布 Pop!_OS 22.10 Linux 发行版
- win7系统如何添加计划任务?在win7系统添加任务计划的详细步骤-系统城
- 走出wiwin7物理内存设置n7下物理内存设置的误区
- Win8用户账户控制怎样取消?
- QQ空间装扮向好友索要的办法步骤_腾讯QQ
- WinXP如何设置程序兼容模式
- win8.1怎么还原系统 win8.1通过还原点恢复系统方法
- 1499元四核平板!神舟Pcpad配Win8首发送键盘
- ChatGPT 创建逃避 EDR 检测的变异恶意软件
- 天猫电器城贺岁新品 海尔W1048S促1599元
- 崩坏星穹铁道星芒战幕攻略大全,星穹铁道星芒战幕阵容配队推荐
- Win11卸载更新后果是什么 Win11卸载更新的方法【详解】
- 装机高手教你怎样装win7系统
- Windows 10 Build 10074泄露版下载大家来看看
- 完美新世界网络连接错误/无法进入游戏的解决办法
- 不想合并QQ聊天窗口怎样分开_腾讯QQ
热门系统
- 1华硕笔记本&台式机专用系统 GhostWin7 32位正式旗舰版2018年8月(32位)ISO镜像下载
- 2深度技术 Windows 10 x86 企业版 电脑城装机版2018年10月(32位) ISO镜像免费下载
- 3雨林木风 Ghost Win7 SP1 装机版 2020年4月(32位) 提供下载
- 4电脑公司 装机专用系统Windows10 x86喜迎国庆 企业版2020年10月(32位) ISO镜像快速下载
- 5深度技术 Windows 10 x86 企业版 六一节 电脑城装机版 版本1903 2022年6月(32位) ISO镜像免费下载
- 6深度技术 Windows 10 x64 企业版 电脑城装机版2021年1月(64位) 高速下载
- 7新萝卜家园电脑城专用系统 Windows10 x64 企业版2019年10月(64位) ISO镜像免费下载
- 8新萝卜家园 GhostWin7 SP1 最新电脑城极速装机版2018年8月(32位)ISO镜像下载
- 9电脑公司Ghost Win8.1 x32 精选纯净版2022年5月(免激活) ISO镜像高速下载
- 10新萝卜家园Ghost Win8.1 X32 最新纯净版2018年05(自动激活) ISO镜像免费下载
热门文章
常用系统
- 1深度技术Ghost win7 64位 多驱动装机版下载 v2022.10最新免费下载
- 2电脑公司 GHOST XP SP3 装机特别版 V2011.04 下载
- 3新雨林木风 Windows10 x86 企业装机版2021年6月(32位) ISO镜像高速下载
- 4深度技术Ghost Win8.1 32位 快速纯净版2018年5月(免激活) ISO镜像免费下载
- 5电脑公司WIN10 IOS镜像文件下载_电脑公司GHOST WIN10 32位稳定专业版下载V2021.02
- 6深度技术XP装机版系统下载_深度技术GHOST XP SP3通用装机版免费下载V2023.04
- 7雨林木风系统 Ghost XP SP3 装机版 YN2021年4月 ISO镜像高速下载
- 8Win11最新版22H2下载_Win11游戏系统镜像优化版激活下载
- 9雨林木风Ghost Win10 64位 纯净版v2023.06最新下载
- 10萝卜家园 GHOST WIN7 SP1 X64 快速装机专业版 V2023.03(64位) 下载