-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor game code to update score display on game end
- Loading branch information
Showing
8 changed files
with
159 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { gameParam } from "./gameConfig" | ||
import { IBrick, ICanvasWithMapCtx } from "./types" | ||
import { SinglePattern } from "./utils" | ||
|
||
class GameHelper { | ||
/** | ||
* @dec 记录方块的落点位置 以及它的颜色 | ||
* @returns 是否成功记录 如果失败就是游戏结束 | ||
*/ | ||
record( | ||
mapBinary: ICanvasWithMapCtx["mapBinary"], | ||
bg: ICanvasWithMapCtx["bg"], | ||
brick: IBrick | ||
) { | ||
// 记录失败返回会false | ||
if (this.isGameOver(brick)) { | ||
return false | ||
} | ||
const binary = brick.getBinary() | ||
for (let i = binary.length - 1; i >= 0; i--) { | ||
if (binary[i] === 0) continue | ||
mapBinary[brick.y + i] |= binary[i] | ||
for (let j = gameParam.column - 1, r = binary[i]; r !== 0; j--, r >>= 1) { | ||
if (r & 1) { | ||
bg[brick.y + i][j] = brick.color | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
/** | ||
* 消除行和颜色 | ||
*/ | ||
eliminate( | ||
mapBinary: ICanvasWithMapCtx["mapBinary"], | ||
bg: ICanvasWithMapCtx["bg"] | ||
) { | ||
let count = 0 | ||
for (let i = gameParam.row - 1; i >= 0; i--) { | ||
if (mapBinary[i] === 2 ** gameParam.column - 1) { | ||
count++ | ||
mapBinary.splice(i, 1) | ||
mapBinary.unshift(0) | ||
bg.splice(i, 1) | ||
bg.unshift(Array.from({ length: gameParam.column })) | ||
i++ | ||
} | ||
} | ||
return count | ||
} | ||
isGameOver(brick: IBrick) { | ||
// 这里只需要判断方块是否超出顶部即可 判断方块的每一行下标是不是越界(及y小于0) | ||
const len = brick.structure.length | ||
for (let i = 0; i < len; i++) { | ||
if (brick.y + i < 0) return true | ||
} | ||
return false | ||
} | ||
computeScore(row: number) { | ||
switch (row) { | ||
case 0: | ||
return 20 | ||
case 1: | ||
return 120 | ||
case 2: | ||
return 320 | ||
case 3: | ||
return 720 | ||
case 4: | ||
return 1520 | ||
default: | ||
return 20 | ||
} | ||
} | ||
} | ||
|
||
const SingleGameHelper = SinglePattern(GameHelper) | ||
const gameHelper = new SingleGameHelper() | ||
|
||
export { gameHelper } | ||
export type { GameHelper } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import Game from "./game"; | ||
import Game from "./game" | ||
import { $ } from "./utils" | ||
|
||
|
||
const game = new Game() | ||
const game = new Game((score) => { | ||
$(".score")!.textContent = score + "" | ||
}) | ||
game.startGame() | ||
console.log(game); | ||
console.log(game) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default class Scorer { | ||
static instance: Scorer | ||
private cb: ((score: number) => void) | undefined | ||
constructor(cb?: (score: number) => void) { | ||
if (Scorer.instance) { | ||
return Scorer.instance | ||
} | ||
if (cb) { | ||
this.cb = cb | ||
} | ||
Scorer.instance = this | ||
} | ||
private _score = 0 | ||
get score() { | ||
return this._score | ||
} | ||
bonusPoint(v: number) { | ||
this._score += v | ||
if (this.cb) { | ||
this.cb(this._score) | ||
} | ||
} | ||
makeZero() { | ||
this._score = 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters