From 78482622646cb5294d06c0aab1571f31b308b24c Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 9 Jul 2024 18:49:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E8=89=B2=E6=96=B9=E5=9D=97=20?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=AE=8C=E5=96=84=E3=80=82=201=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8C=89=E9=92=AE=E4=BA=8B=E4=BB=B6=202=20=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E8=BF=87=E5=85=B3=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/model/box.ets | 7 ++ entry/src/main/ets/model/boxes.ets | 27 ++++++++ entry/src/main/ets/pages/ColorBlocks.ets | 86 ++++++++++++++++++++++-- entry/src/main/ets/view/boxView.ets | 9 ++- 4 files changed, 121 insertions(+), 8 deletions(-) diff --git a/entry/src/main/ets/model/box.ets b/entry/src/main/ets/model/box.ets index 61971f7..30595ee 100644 --- a/entry/src/main/ets/model/box.ets +++ b/entry/src/main/ets/model/box.ets @@ -1,5 +1,12 @@ @Observed export class box { + setBox(colIndex: number, rowIndex: number, turned: boolean, index: number) { + this.colIndex = colIndex; + this.rowIndex = rowIndex; + this.turned = turned; + this.index = index; + } + colIndex: number rowIndex: number turned: boolean diff --git a/entry/src/main/ets/model/boxes.ets b/entry/src/main/ets/model/boxes.ets index 7246e5d..dc76108 100644 --- a/entry/src/main/ets/model/boxes.ets +++ b/entry/src/main/ets/model/boxes.ets @@ -3,6 +3,7 @@ import { box } from './box' export class boxes { boxes: Array = [] index: number = 0 + different: number = 0 constructor() { } @@ -15,6 +16,32 @@ export class boxes { } } } + + reload(startLine: number) { + this.different = startLine * startLine - (startLine - 1) * (startLine - 1) + for (let i = 0; i < this.different; i++) { + this.boxes.push(new box(0, 0, false, 0)) + } + this.index = 0 + for (let i = 1; i <= startLine; i++) { + for (let j = 1; j <= startLine; j++) { + this.boxes[this.index].setBox(i, j, false, this.index) + this.index++ + } + } + } + + reLevel() { + for (let index = 0; index < this.boxes.length; index++) { + this.boxes[index].turned = false; + } + } + + restart() { + this.index = 0; + this.boxes.splice(1, this.boxes.length - 1) + this.boxes[0].setBox(1, 1, false, 0) + } } const boxList = new boxes() diff --git a/entry/src/main/ets/pages/ColorBlocks.ets b/entry/src/main/ets/pages/ColorBlocks.ets index 58cf72a..5d6b57b 100644 --- a/entry/src/main/ets/pages/ColorBlocks.ets +++ b/entry/src/main/ets/pages/ColorBlocks.ets @@ -5,15 +5,53 @@ import { boxView } from '../view/boxView'; @Entry @Component +@Preview struct ColorBlocks { @State message: string = 'Hello World'; - @State startLine: number = 3; + @State startLine: number = 1; @State boxes: boxes = boxList; aboutToAppear(): void { boxList.init(this.startLine) } + turnover(box: box) { + if (box.rowIndex < this.startLine) { + this.boxes.boxes[box.index + 1].changeTurned() + } + if (box.rowIndex > 1) { + this.boxes.boxes[box.index - 1].changeTurned() + } + if (box.colIndex > 1) { + this.boxes.boxes[box.index - this.startLine].changeTurned() + } + if (box.colIndex < this.startLine) { + this.boxes.boxes[box.index + this.startLine].changeTurned() + } + box.turned = !box.turned + this.isWin() + } + + isWin() { + for (let i = 0; i < this.boxes.boxes.length; i++) { + if (!this.boxes.boxes[i].turned) { + return + } + } + AlertDialog.show({ + title: "成功过关", + message: `恭喜你成功通过${this.startLine}关`, + confirm: { + value: "ok", + action: () => { + this.startLine++ + this.boxes.reload(this.startLine) + } + } + }) + + } + build() { Column() { HdNav({ title: '变色方块', showRightIcon: false }) @@ -23,9 +61,44 @@ struct ColorBlocks { }.width("100%").height("13%") Row() { - Button("重新开始") - Button("重玩本关") - Button("游戏介绍") + Button("重新开始").onClick(() => { + AlertDialog.show({ + title: "重新开始", + message: "重新从第一关开始游戏", + confirm: { + value: "ok", + action: () => { + this.startLine = 1 + this.boxes.restart() + } + } + }) + }) + Button("重玩本关").onClick(() => { + AlertDialog.show({ + title: "重玩本关", + message: `重玩第${this.startLine}关`, + confirm: { + value: "ok", + action: () => { + this.boxes.reLevel() + } + } + }) + }) + Button("游戏介绍").onClick(() => { + AlertDialog.show({ + title: "游戏说明", + message: "1. 游戏玩法:点击色块会改变其自身和上下左右相邻色块的颜色" + + "\n" + + "2. 当所有色块全部变为橘色,即为过关", + confirm: { + value: "ok", + action: () => { + } + } + }) + }) }.width("98%").height("23%").justifyContent(FlexAlign.SpaceAround) Column() { @@ -37,10 +110,11 @@ struct ColorBlocks { Row() { boxView({ b: box }) }.onClick(() => { - box.turned = !box.turned + this.turnover(box) }) - } + }.width("60%") + .height(400 / this.startLine) }) } diff --git a/entry/src/main/ets/view/boxView.ets b/entry/src/main/ets/view/boxView.ets index e88c957..cf6f708 100644 --- a/entry/src/main/ets/view/boxView.ets +++ b/entry/src/main/ets/view/boxView.ets @@ -7,7 +7,12 @@ export struct boxView { build() { Row() { - }.height(80).width(80).backgroundColor(this.b.turned ? Color.Orange : Color.Blue) - .margin(20) + }.height("80%").width("90%").backgroundColor(this.b.turned ? Color.Orange : Color.Blue) + .margin({ + top: 5, + right: 0, + bottom: 0, + left: 0 + }) } } \ No newline at end of file