0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鸿蒙上开发“小蜜蜂”游戏

OpenHarmony技术社区 来源:OST开源开发者 2023-04-03 11:27 次阅读

小时候我们有个熟悉的游戏叫小蜜蜂。本文教大家在鸿蒙上学做这个小蜜蜂游戏。

开发实战

①HAP 应用建立

前面我们介绍了简单的 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=false this.drawall() }) } .width('100%') } .height('100%') .backgroundColor("#000000") }
build 是基础页面的构造函数,用于界面的元素构造,其他的页面的生命周期函数如下:
declareclassCustomComponent{ /** *Customizethepop-upcontentconstructor. *@since7 */ build():void; /** *aboutToAppearMethod *@since7 */ aboutToAppear?():void; /** *aboutToDisappearMethod *@since7 */ aboutToDisappear?():void; /** *onPageShowMethod *@since7 */ onPageShow?():void; /** *onPageHideMethod *@since7 */ onPageHide?():void; /** *onBackPressMethod *@since7 */ onBackPress?():void; }

②Canvas 介绍

canvas 是画布组件用于自定义绘制图形,具体的 API页面如下:

https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-components-canvas-canvas-0000001333641081
页面显示前会调用 aboutToAppear() 函数,此函数为页面生命周期函数。 canvas 组件初始化完毕后会调用 onReady() 函数,函数内部实现小游戏的初始页面的绘制。

初始化页面数据:

drawall(){ this.context.clearRect(0,0,this.context.width,this.context.height) this.drawFj(); this.drawEn(); this.drawBullet(); this.drawScore(); }
绘制飞机:
drawFj(){ this.context.drawImage(this.fjImg,this.fjStartX,this.fjslotY,this.birdH,this.birdW) }
绘制害虫:
drawEn(){ for(letline=0;line< this.enemylist.length; line++) { for (let row=0; row < this.enemylist[line].length; row++) { if (this.enemylist[line][row] == 1) { if (line == 0) { this.context.drawImage( this.en1Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } else if (line == 1) { this.context.drawImage( this.en2Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } else if (line == 2) { this.context.drawImage( this.en3Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } } } } }

不同行的害虫长相不同,分值不同。

③游戏逻辑

简单的小游戏主体游戏逻辑为:点击鼠标移动飞机,飞机发射子弹,命中害虫,计算分数:

doClick(){ if(this.en1slotX<= 50) { this.en1slotX += this.birdW } else { this.en1slotX -= this.birdW } console.log("doclick----") this.moveFj(); }

④完整逻辑

@Entry @Component structIndex{ @Statemessage:string='HelloWorld' privatesettings:RenderingContextSettings=newRenderingContextSettings(true); privatecontext:CanvasRenderingContext2D=newCanvasRenderingContext2D(this.settings); privateblockType:number=0 privateblockSize:number=30 privateen1Img:ImageBitmap=newImageBitmap("common/images/mf1.png") privateen2Img:ImageBitmap=newImageBitmap("common/images/mf2.png") privateen3Img:ImageBitmap=newImageBitmap("common/images/mf3.png") privatefjImg:ImageBitmap=newImageBitmap("common/images/fj.png") privatestartX=30; privatestartY=100; privateenStartY=140; privatefjStartX=50; privatefjStartY=610; privatefjslotX=50; privatefjslotY=this.fjStartY; privateen1slotX=50; privateen1slotY=this.enStartY; privateen2slotX=50; privateen2slotY=this.enStartY; privatebulletX=65; privatebulletY=550; privatebirdH=40; privatebirdW=40; privatescore=0; privatefjDirection=1; privateenemylist=[ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1], ] moveFj(){ this.fjStartX=this.fjStartX+this.fjDirection*this.birdW if(this.fjStartX>=210){ this.fjDirection=-1 }elseif(this.fjStartX<= 50) { this.fjDirection = 1 } } drawFj() { this.context.drawImage( this.fjImg, this.fjStartX, this.fjslotY,this.birdH,this.birdW) } drawEn() { for (let line=0; line < this.enemylist.length; line++) { for (let row=0; row < this.enemylist[line].length; row++) { if (this.enemylist[line][row] == 1) { if (line == 0) { this.context.drawImage( this.en1Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } else if (line == 1) { this.context.drawImage( this.en2Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } else if (line == 2) { this.context.drawImage( this.en3Img, this.en1slotX+row*this.birdW,this.en1slotY-line*this.birdH,this.birdH,this.birdW); } } } } } drawBullet() { let isfind = false this.context.fillStyle = 'rgb(250,250,250)' this.context.font = '80px sans-serif' this.bulletX = this.fjStartX + 20 this.context.fillText(":", this.fjStartX+20, this.bulletY) for (let line=0; line < this.enemylist.length; line++) { if (Math.abs(this.bulletY - (this.en1slotY-line*this.birdH)) <= this.birdH) { console.log("find line: "+line) for (let row = 0; row < this.enemylist[line].length; row++) { let matchsize = Math.abs(this.bulletX - (this.en1slotX+row*this.birdW)) // console.log("find szie: "+matchsize.toString()+" row:"+row.toString()+" line:"+line.toString()+" bulletX:"+this.bulletX.toString()+" bulletY:"+ // this.bulletY.toString()+" en1slotX"+this.en1slotX.toString()+" en1slotY"+this.en1slotY.toString()) if (matchsize <= this.birdW) { if (this.enemylist[line][row] == 1) { console.log("row:"+row.toString()+" line:"+line.toString()+" bulletX:"+this.bulletX.toString()+" bulletY:"+ this.bulletY.toString()+" en1slotX"+this.en1slotX.toString()+" en1slotY"+this.en1slotY.toString()); this.enemylist[line][row] = 0 isfind = true switch (line) { case 0: this.score += 1; break; case 1: this.score += 2; break; case 2: this.score += 3; break; default: break; } //console.log("score: "+this.score.toString()) break } } } if (isfind) { break; } } } if (this.bulletY <= 100 || isfind == true) { this.bulletY = 550 } else { this.bulletY -= 50; } } drawScore() { this.context.fillStyle = 'rgb(250,250,250)' this.context.font = '80px sans-serif' this.context.fillText("Score:"+this.score.toString(), 20, 750) // this.context.fillText(":", 65, 550) } drawall() { this.context.clearRect(0,0,this.context.width,this.context.height) this.drawFj(); this.drawEn(); this.drawBullet(); this.drawScore(); } async sleep(ms: number) { var that = this; return new Promise((r) =>{ setInterval(()=>{ if(that.en1slotX<= 50) { that.en1slotX += that.birdW } else { that.en1slotX -= that.birdW } console.log(that.en1slotX.toString()) that.drawall() }, ms) }) } doClick() { if (this.en1slotX <= 50) { this.en1slotX += this.birdW } else { this.en1slotX -= this.birdW } console.log("doclick----") this.moveFj(); } aboutToAppear() { this.sleep(1000) } build() { Row() { Column() { Canvas(this.context) .width('100%') .height('100%') .onClick((ev: ClickEvent) =>{ console.info("click!!") this.doClick() }) .onReady(()=>{ this.context.imageSmoothingEnabled=false this.drawall() }) } .width('100%') } .height('100%') .backgroundColor("#000000") } }
遗留问题:

飞机的子弹可以多发

害虫可以攻击飞机

游戏声音问题:目前 ohos 不支持音频播放资源音频,看之后版本是否支持

DevEco 用 setInterval 重绘 canvas 会导致 ide 崩溃

总结

本文主要介绍了小游戏的开发,画布功能的使用,获取源码请通过“阅读原文”下载附件。

作者:王石

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 游戏
    +关注

    关注

    2

    文章

    714

    浏览量

    26188
  • API
    API
    +关注

    关注

    2

    文章

    1453

    浏览量

    61424
  • 函数
    +关注

    关注

    3

    文章

    4217

    浏览量

    61866
  • Canvas
    +关注

    关注

    0

    文章

    16

    浏览量

    10959
  • 鸿蒙
    +关注

    关注

    56

    文章

    2262

    浏览量

    42439

原文标题:鸿蒙上开发“小蜜蜂”游戏

文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    高云半导体小蜜蜂家族再添新成员——GW1NS-2 FPFA-SoC芯片揭开AI的序幕

    中国广州,2018年7月23日,广东高云半导体科技股份有限公司(以下简称“高云半导体”)今日宣布:高云半导体首款FPGA-SoC产品— 小蜜蜂®(LittleBee®)家族GW1NS系列GW1NS-2开始提供工程样片及 开发板,揭开了布局AI的序幕。
    的头像 发表于07-23 14:09 9529次阅读

    [转帖]这才叫高科技:美科学家用机械蜜蜂演奏007主题曲

    宾夕法尼亚大学研制的四旋翼机械 蜜蜂曾经令人印象深刻,但是从来没人想到,这些机械 小蜜蜂真的会干出一件和007相称的事:它们在一系列复杂的飞行之后,成功地演奏出了邦德的主题曲。这些机器人在电子琴键上降落
    发表于08-13 20:19

    小蜜蜂扩音器的电路图和元器件清单

    本帖最后由 gk320830 于 2015-3-7 13:41 编辑 小蜜蜂扩音器的电路图和元器件清单。我很急希望各位大大可以帮忙。谢谢大家了。。。{:4:}
    发表于04-19 22:44

    小蜜蜂】基于ZigBee的水情信息采集平台

    本帖最后由 wangjiamin2014 于 2015-1-9 11:38 编辑 项目名称:基于ZigBee的水情信息采集平台 团队名称: 小蜜蜂团队成员:张建祥、李如菊、李艳作品演示作品介绍水
    发表于12-31 11:19

    游戏玩家让自己儿子按年代顺序玩了六年古董游戏

    。1979年的Galaxian( 小蜜蜂)是他儿子的启蒙 游戏。他的儿子Eliot出生于2004年,因此Baio最近才发表了他长达十年的研究——“关于强迫怀旧和质疑教育的实验”。该研究是为了让他的儿子探索媒介
    发表于05-04 16:12

    小蜜蜂双轮平衡车

    `经过一个月的努力,做了点小东西,在这里跟大家分享一下。当然也欢迎大家多多喷我。由于本人等级有限没办法发视频连接,大家可以前往优酷,然后搜索---- 小蜜蜂双轮车测试视频.有什么问题可以加入个人qq:2424607185.`
    发表于01-15 17:27

    小蜜蜂机器人APP测试

    经过一个月的努力,做了点小东西(无广告),在这里跟大家分享一下。当然也欢迎大家多多喷我。下载方式:应用宝,然后搜索“ 小蜜蜂机器人”。由于本人等级有限没办法发视频连接,大家可以前往优酷,然后搜索---” 小蜜蜂机器人APP使用介绍“ .有什么问题可以加入个人qq:242460
    发表于01-17 17:09

    有一种微信营销神器叫云控系统,有它吸粉引流不会累!

    就研发了这样一款微信营销吸粉神器—— 小蜜蜂吸粉精灵云控系统。做过微商或者通过微信进行营销的人都知道,在通过微信进行微信营销吸粉引流的过程中,人为操作往往会出现各种各样的问题,需要耗费大量的时间、人力去
    发表于08-15 14:32

    【EFM8 Universal Bee试用体验】开箱+上电

    `晚上收到了 开发板,我打开一看,包装果然有大厂风范,一个大大的 蜜蜂,我姑娘看到了,以为里面装的是 小蜜蜂玩具呢,差点给就地正法。我赶快抢过来,告诉他这个是爸爸的玩具,我才有机会打开了看看里面有什么不但
    发表于11-07 12:34

    【FPGA开发者项目连载】一次开发经验浅谈

    ` 非常开心参加了高云半导体的的星核计划。也很荣幸收到了高云半导体赞助的 小蜜蜂 开发板。玩FPGA也有两三年的时间了,因为学校有时间,平常也喜欢做一些相关的东西。 这也是我第一次拿到咱们国产的FPGA
    发表于05-12 19:41

    海尔小蜜蜂智能修改版V1.03

    德赢Vwin官网 网站提供《海尔 小蜜蜂智能修改版V1.03.exe》资料免费下载
    发表于02-27 09:15 0次下载

    瞄准车载FPGA领域,高云半导体推小蜜蜂家族新品

    ,分别是使用嵌入式闪存工艺的非易失FPGA 小蜜蜂®家族和基于SRAM的中密度FPGA晨熙®家族,这两个家族的器件均已支持商业级(0C°-85C°)和工业级(-40C°~+100C°)温度标准。此次推出支持汽车级温度范围(-40C°~+125C°)的为 小蜜蜂®家族部分FPG
    的头像 发表于11-30 10:41 8944次阅读

    高云半导体小蜜蜂家族GW1NS系列产品入围Arm TechCon 2018年度最佳技术创新奖

    中国广州,2018年10月10日,广东高云半导体科技股份有限公司(以下简称“高云半导体”)今日宣布, 小蜜蜂家族GW1NS系列产品被提名入围Arm TechCon 2018年度最佳技术创新奖。
    的头像 发表于10-10 10:27 6022次阅读

    鸿蒙上实现“数字华容道”小游戏

    本篇文章教大家如何在 鸿蒙上实现“数字华容道”小 游戏
    的头像 发表于12-26 09:52 1050次阅读

    8位MCU市场中,飞来的这只“小蜜蜂”~

    8位MCU市场中,飞来的这只“ 小蜜蜂”~
    的头像 发表于10-26 15:44 641次阅读
    8位MCU市场中,飞来的这只“<b class='flag-5'>小蜜蜂</b>”~