成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

跟著小白一起學鴻蒙—一起學做Tetris(上)

系統 OpenHarmony
小時候有個游戲叫俄羅斯方塊,大人小孩都喜歡玩,我們就一起看看如何能用OpenHarmony學習做個Tetris。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

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

簡介

小時候有個游戲叫俄羅斯方塊,大人小孩都喜歡玩,我們就一起看看如何能用OpenHarmony學習做個Tetris。

#盲盒+碼##跟著小白一起學鴻蒙# [番外三]一起學做Tetris(上)-開源基礎軟件社區

開發

1、HAP應用建立

《#跟著小白一起學鴻蒙#[六]如何編寫一個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.randomType()
this.drawall()
})
}
.width('100%')
}
.height('100%')
.backgroundColor("#cccccc")
}

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)初始化頁面數據
drawall() {
this.drawBox()
this.drawSideBlock()
this.drawBoxBlock()
this.drawScore()
}

因為都是畫布畫的,所以布局有點麻煩,需要畫幾個部分:

  • 中間的大框:方塊下落和堆疊區域。
  • 右邊提升框:下個方塊類型。
  • 中間方塊:方塊運動和堆疊。
  • 下方計分:行數得分。
(2)繪制大框
drawBox() {
this.context.lineWidth = 4
this.context.beginPath()
this.context.lineCap = 'butt'
this.context.moveTo(0, 100)
this.context.lineTo(270, 100)
this.context.moveTo(270, 100)
this.context.lineTo(270, 690)
this.context.moveTo(0, 690)
this.context.lineTo(270, 690)
}
(3)繪制提示方塊
drawSideBlock() {
this.context.fillStyle = 'rgb(250,0,0)'
let bs = this.blockSize
let coords = this.blockShapBasic[this.blockType]
let x = this.sideStartX + coords[0][0]*this.blockSize
let y = this.sideStartY + coords[0][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[1][0]*this.blockSize
y = this.sideStartY + coords[1][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[2][0]*this.blockSize
y = this.sideStartY + coords[2][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[3][0]*this.blockSize
y = this.sideStartY + coords[3][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
this.context.stroke()
}
(4)繪制運動方塊
drawBoxBlock() {
this.setDirection()
this.context.fillStyle = 'rgb(250,0,0)'
let bs = this.blockSize
let coords = this.curBlockShap
let starty = this.slotStartY + this.step * this.blockSize
let x = this.slotStartX + coords[0][0]*this.blockSize
let y = starty + coords[0][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[1][0]*this.blockSize
y = starty + coords[1][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[2][0]*this.blockSize
y = starty + coords[2][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[3][0]*this.blockSize
y = starty + coords[3][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
this.context.stroke()
this.slotBottomY = y
}
(5)繪制得分區域
drawScore() {
this.context.fillStyle = 'rgb(0,0,0)'
this.context.font = '80px sans-serif'
this.context.fillText("Score:"+this.score.toString(), 20, 740)
}

3、游戲邏輯

簡單的小游戲主體游戲邏輯為:等待開始,開始,結束流程圖如下:

graph LR
timer開始 --> 方塊下落
timer開始 --> click[點擊]
click[點擊] --> 方塊變形
方塊下落 --> |落到底| 能消除 --> 計分 --> 堆積
方塊下落 --> |落到底| 不能消除 --> 堆積
堆積 --> |堆積到頂| 滿了 --> 游戲結束
堆積 --> |堆積到頂| 未滿 --> 方塊下落
doClick() {
this.direction += 1
}

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 blockShapBasic = [
[[0,0],[0,1],[0,2],[0,3]],
[[0,0],[0,1],[0,2],[1,2]],
[[0,0],[0,1],[1,1],[0,2]],
[[0,0],[0,1],[1,1],[1,2]],
[[0,0],[0,1],[1,0],[1,1]],
]
private blockShap = [
[[0,0],[0,1],[0,2],[0,3]],
[[0,0],[0,1],[0,2],[1,2]],
[[0,0],[0,1],[1,1],[0,2]],
[[0,0],[0,1],[1,1],[1,2]],
[[0,0],[0,1],[1,0],[1,1]],
]
private curBlockShap = []
private sideStartX = 300;
private sideStartY = 150;
private slotStartX = 120;
private slotStartY = 150;
private slotBottomY = 150;;
private score = 0;
private step = 0;
private direction = 0;
aboutToDisappear() {
}
aboutToAppear() {
this.sleep(1000)
}
async sleep(ms: number) {
return new Promise((r) => {
setInterval(() => {
console.log(this.message)
this.drawStep()
}, ms)
})
}
doClick() {
this.direction += 1
}
drawBox() {
this.context.lineWidth = 4
this.context.beginPath()
this.context.lineCap = 'butt'
this.context.moveTo(0, 100)
this.context.lineTo(270, 100)
this.context.moveTo(270, 100)
this.context.lineTo(270, 690)
this.context.moveTo(0, 690)
this.context.lineTo(270, 690)
}
setDirection() {
this.curBlockShap = this.blockShap[this.blockType]
if (this.direction > 0) {
for (let i=0;i<4;i++) {
let x = this.curBlockShap[i][0]
this.curBlockShap[i][0] = this.curBlockShap[i][1]
this.curBlockShap[i][1] = x
}
this.direction = 0
}
}
drawSideBlock() {
this.context.fillStyle = 'rgb(250,0,0)'
let bs = this.blockSize
let coords = this.blockShapBasic[this.blockType]
let x = this.sideStartX + coords[0][0]*this.blockSize
let y = this.sideStartY + coords[0][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[1][0]*this.blockSize
y = this.sideStartY + coords[1][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[2][0]*this.blockSize
y = this.sideStartY + coords[2][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.sideStartX + coords[3][0]*this.blockSize
y = this.sideStartY + coords[3][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
this.context.stroke()
}
drawBoxBlock() {
this.setDirection()
this.context.fillStyle = 'rgb(250,0,0)'
let bs = this.blockSize
let coords = this.curBlockShap
let starty = this.slotStartY + this.step * this.blockSize
let x = this.slotStartX + coords[0][0]*this.blockSize
let y = starty + coords[0][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[1][0]*this.blockSize
y = starty + coords[1][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[2][0]*this.blockSize
y = starty + coords[2][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
x = this.slotStartX + coords[3][0]*this.blockSize
y = starty + coords[3][1]*this.blockSize
this.context.fillRect(x, y, bs, bs)
this.context.rect(x, y, bs, bs)
console.info("x,y"+x.toString()+":"+y.toString())
this.context.stroke()
this.slotBottomY = y
}
drawScore() {
this.context.fillStyle = 'rgb(0,0,0)'
this.context.font = '80px sans-serif'
this.context.fillText("Score:"+this.score.toString(), 20, 740)
}
randomType() {
this.blockType = Math.floor(Math.random()*5)
console.info("blocktype:"+this.blockType.toString())
}
drawStep() {
this.context.clearRect(0,0,this.context.width,this.context.height)
this.step += 1
this.drawBox()
this.drawSideBlock()
this.drawBoxBlock()
this.drawScore()
if (this.slotBottomY >= 660) {
this.step = 0
this.randomType()
}
}
drawall() {
this.drawBox()
this.drawSideBlock()
this.drawBoxBlock()
this.drawScore()
}
build() {
Row() {
Column() {
Canvas(this.context)
.width('100%')
.height('100%')
.onClick((ev: ClickEvent) => {
console.info("click!!")
this.doClick()
})
.onReady(() =>{
this.context.imageSmoothingEnabled = false
this.randomType()
this.drawall()
})
}
.width('100%')
}
.height('100%')
.backgroundColor("#cccccc")
}
}

遺留問題:

  1. 沒實現堆積計分(接下來會做)。
  2. 可實現網絡對戰(分布式對戰)。

5、獲取源碼

等游戲完整發布,會有兩個版本,單機和聯機版本。

總結

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

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

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

責任編輯:jianghua 來源: 51CTO開源基礎軟件社區
相關推薦

2022-12-02 14:20:09

Tetris鴻蒙

2022-11-14 17:01:34

游戲開發畫布功能

2023-03-30 09:32:27

2023-02-27 16:30:32

鴻蒙開源協議分析

2023-03-30 09:19:54

SELinux安全子系統

2022-08-19 19:02:20

開源鴻蒙操作系統

2023-04-04 09:24:11

鴻蒙HiDumper

2022-10-10 14:47:04

藍牙應用鴻蒙

2023-01-03 15:09:10

鴻蒙常用工具

2022-10-09 15:05:50

NAPI框架鴻蒙

2022-10-20 16:40:16

JS應用控制LED鴻蒙

2022-12-06 15:39:16

鴻蒙主干代碼

2023-03-15 16:19:03

BinderIPC工具

2022-11-24 14:34:41

Hap程序鴻蒙

2022-09-28 13:57:41

鴻蒙開源

2022-11-03 15:47:04

HTTP通信協議

2022-10-31 15:35:02

Wi-Fi藍牙子系統

2022-11-08 15:43:45

開源鴻蒙藍牙協議棧

2023-04-06 09:18:52

鴻蒙AVPlayerAVRecorder

2022-12-09 15:34:38

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久久久国产一区二区三区 | 久久久久久毛片免费观看 | 天天操精品视频 | 日韩电影中文字幕 | 久久综合久色欧美综合狠狠 | 国产精品一区二区av | 亚洲精品久久久一区二区三区 | 久久精品91 | 中文字幕成人在线 | 色婷婷一区二区三区四区 | 免费一二区| www操操| 亚州精品天堂中文字幕 | 亚洲有码转帖 | 天堂在线91 | 国产精品美女久久久久久久网站 | 亚洲一二三区在线观看 | 久久日韩精品 | 自拍 亚洲 欧美 老师 丝袜 | 成人av在线播放 | 91久久国产综合久久 | 国产精品一区一区三区 | 日韩综合在线视频 | 久久精品亚洲一区二区三区浴池 | 99资源| 国产小视频在线 | 一区二区三区精品视频 | 久久久久久久久久久久久9999 | 久久精品无码一区二区三区 | 中文成人无字幕乱码精品 | 91中文字幕 | 99国产精品99久久久久久 | 亚洲草草视频 | 97精品超碰一区二区三区 | 亚洲一区二区三区免费视频 | 国产999精品久久久久久绿帽 | 久草网免费 | 激情91 | 国产精品久久久久久中文字 | 999精品在线| 欧美日韩中|