小時(shí)候我們有個(gè)熟悉的游戲叫小蜜蜂。本文教大家在鴻蒙上學(xué)做這個(gè)小蜜蜂游戲。
開發(fā)實(shí)戰(zhàn)
①HAP 應(yīng)用建立
前面我們介紹了簡(jiǎn)單的 Hap 應(yīng)用的開發(fā)以及基礎(chǔ)控件的介紹,這里我們就不贅述 Hap 項(xiàng)目的建立過程,以下就是基礎(chǔ)的 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 是基礎(chǔ)頁面的構(gòu)造函數(shù),用于界面的元素構(gòu)造,其他的頁面的生命周期函數(shù)如下:
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頁面顯示前會(huì)調(diào)用 aboutToAppear() 函數(shù),此函數(shù)為頁面生命周期函數(shù)。 canvas 組件初始化完畢后會(huì)調(diào)用 onReady() 函數(shù),函數(shù)內(nèi)部實(shí)現(xiàn)小游戲的初始頁面的繪制。
初始化頁面數(shù)據(jù):
drawall(){
this.context.clearRect(0,0,this.context.width,this.context.height)
this.drawFj();
this.drawEn();
this.drawBullet();
this.drawScore();
}
繪制飛機(jī):
drawFj(){
this.context.drawImage(this.fjImg,this.fjStartX,this.fjslotY,this.birdH,this.birdW)
}
繪制害蟲:
drawEn(){
for(letline=0;line
不同行的害蟲長相不同,分值不同。
③游戲邏輯
簡(jiǎn)單的小游戲主體游戲邏輯為:點(diǎn)擊鼠標(biāo)移動(dòng)飛機(jī),飛機(jī)發(fā)射子彈,命中害蟲,計(jì)算分?jǐn)?shù):
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?{
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")
}
}
遺留問題:
飛機(jī)的子彈可以多發(fā)
害蟲可以攻擊飛機(jī)
游戲聲音問題:目前 ohos 不支持音頻播放資源音頻,看之后版本是否支持
DevEco 用 setInterval 重繪 canvas 會(huì)導(dǎo)致 ide 崩潰
總結(jié)
本文主要介紹了小游戲的開發(fā),畫布功能的使用,獲取源碼請(qǐng)通過“閱讀原文”下載附件。
作者:王石
審核編輯:湯梓紅
-
游戲
+關(guān)注
關(guān)注
2文章
790瀏覽量
27458 -
API
+關(guān)注
關(guān)注
2文章
2368瀏覽量
66752 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4417瀏覽量
67499 -
Canvas
+關(guān)注
關(guān)注
0文章
21瀏覽量
11428 -
鴻蒙
+關(guān)注
關(guān)注
60文章
2963瀏覽量
45883
原文標(biāo)題:鴻蒙上開發(fā)“小蜜蜂”游戲
文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
鴻蒙上開發(fā)“小蜜蜂”游戲
評(píng)論