自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

跟著小白一起學(xué)鴻蒙—一起學(xué)做小蜜蜂

系統(tǒng) OpenHarmony
本文主要介紹了小游戲的開發(fā),畫布功能的使用,希望能夠幫助到你!

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

簡(jiǎn)介

小時(shí)候我們有個(gè)熟悉的游戲叫小蜜蜂。本文中引用的圖片資源均來(lái)自與Github。

#創(chuàng)作者激勵(lì)# #跟著小白一起學(xué)鴻蒙# [番外四]一起學(xué)做小蜜蜂-開源基礎(chǔ)軟件社區(qū)

開發(fā)

1、HAP應(yīng)用建立

《#跟著小白一起學(xué)鴻蒙#[六]如何編寫一個(gè)hap應(yīng)用》里我們介紹了簡(jiǎn)單的Hap應(yīng)用的開發(fā)以及基礎(chǔ)控件的介紹,這里我們就不贅述Hap項(xiàng)目的建立過(guò)程,以下就是基礎(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ǔ)頁(yè)面的構(gòu)造函數(shù),用于界面的元素構(gòu)造,其他的頁(yè)面的生命周期函數(shù)如下:

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頁(yè)面如下:

https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-components-canvas-canvas-0000001333641081

頁(yè)面顯示前會(huì)調(diào)用aboutToAppear()函數(shù),此函數(shù)為頁(yè)面生命周期函數(shù)。

canvas組件初始化完畢后會(huì)調(diào)用onReady()函數(shù),函數(shù)內(nèi)部實(shí)現(xiàn)小游戲的初始頁(yè)面的繪制。

(1)初始化頁(yè)面數(shù)據(jù)
drawall() {
this.context.clearRect(0,0,this.context.width,this.context.height)
this.drawFj();
this.drawEn();
this.drawBullet();
this.drawScore();
}
(2)繪制飛機(jī)
drawFj() {
this.context.drawImage( this.fjImg, this.fjStartX, this.fjslotY,this.birdH,this.birdW)
}
(3)繪制害蟲
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);
}
}
}
}
}

不同行的害蟲長(zhǎng)相不同,分值不同。

3、游戲邏輯

簡(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();
}

4、完整邏輯

@Entry
@Component
struct Index {
@State message: string = 'Hello World'
private settings: RenderingContextSettings = new RenderingContextSettings(true);
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
private blockType: number = 0
private blockSize: number = 30
private en1Img:ImageBitmap = new ImageBitmap("common/images/mf1.png")
private en2Img:ImageBitmap = new ImageBitmap("common/images/mf2.png")
private en3Img:ImageBitmap = new ImageBitmap("common/images/mf3.png")
private fjImg:ImageBitmap = new ImageBitmap("common/images/fj.png")

private startX = 30;
private startY = 100;
private enStartY = 140;
private fjStartX = 50;
private fjStartY = 610;
private fjslotX = 50;
private fjslotY = this.fjStartY;
private en1slotX = 50;
private en1slotY = this.enStartY;
private en2slotX = 50;
private en2slotY = this.enStartY;
private bulletX = 65;
private bulletY = 550;
private birdH = 40;
private birdW = 40;
private score = 0;
private fjDirection = 1;

private enemylist = [
[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
} else if (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")
}
}

遺留問(wèn)題:

  1. 飛機(jī)的子彈可以多發(fā)
  2. 害蟲可以攻擊飛機(jī)
  3. 游戲聲音問(wèn)題:目前ohos不支持音頻播放資源音頻,看之后版本是否支持
  4. DevEco用setInterval重繪canvas會(huì)導(dǎo)致ide崩潰

5、獲取源碼

見(jiàn)附件:

https://ost.51cto.com/resource/2670。

總結(jié)

本文主要介紹了小游戲的開發(fā),畫布功能的使用。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

責(zé)任編輯:jianghua 來(lái)源: 51CTO 開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-11-29 16:35:02

Tetris鴻蒙

2022-12-02 14:20:09

Tetris鴻蒙

2022-11-14 17:01:34

游戲開發(fā)畫布功能

2023-04-04 09:24:11

鴻蒙HiDumper

2022-10-10 14:47:04

藍(lán)牙應(yīng)用鴻蒙

2023-02-27 16:30:32

鴻蒙開源協(xié)議分析

2022-08-19 19:02:20

開源鴻蒙操作系統(tǒng)

2023-03-30 09:19:54

SELinux安全子系統(tǒng)

2023-01-03 15:09:10

鴻蒙常用工具

2022-11-24 14:34:41

Hap程序鴻蒙

2023-03-15 16:19:03

BinderIPC工具

2022-12-06 15:39:16

鴻蒙主干代碼

2022-10-20 16:40:16

JS應(yīng)用控制LED鴻蒙

2022-10-09 15:05:50

NAPI框架鴻蒙

2022-11-28 15:42:39

分布式軟總線鴻蒙

2023-04-06 09:18:52

鴻蒙AVPlayerAVRecorder

2022-11-22 15:15:46

Wi-Fi鴻蒙

2022-10-17 14:29:24

鴻蒙應(yīng)用開發(fā)

2022-12-05 15:02:14

鴻蒙用戶鑒權(quán)

2023-02-24 16:02:45

WebSocket網(wǎng)絡(luò)通訊協(xié)議
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)