From e189e1ea06341cc3ec06e8dfd8ce487b47f9a1f9 Mon Sep 17 00:00:00 2001 From: Libon Date: Sun, 7 Apr 2024 18:04:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E7=8A=B6=E6=80=81=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/components/ToggleDark.vue | 2 +- src/composables/use-game-score.ts | 18 ++++++++ src/composables/use-game-sounds.ts | 2 +- src/composables/use-game-status.ts | 19 --------- src/composables/use-local-cache.ts | 2 +- src/views/game/[level].vue | 68 +++++++++++++++++++++++++----- 7 files changed, 80 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 0e7eb4e..6e8ec73 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [x] 练习模式 - [x] 历史最高分 - [ ] 游玩热力图 -- [ ] 本地缓存 +- [x] 本地缓存 - [ ] 彩色方块 - [x] 游戏音效 - [ ] i18n diff --git a/src/components/ToggleDark.vue b/src/components/ToggleDark.vue index 6f2bead..f41f467 100644 --- a/src/components/ToggleDark.vue +++ b/src/components/ToggleDark.vue @@ -1,6 +1,6 @@ @@ -263,7 +311,7 @@ onBeforeUnmount(() => {
{{ formatScore(gameScore) }} - BEST + BEST Date: Sun, 7 Apr 2024 21:46:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81PC=20=E7=AB=AF?= =?UTF-8?q?=E9=94=AE=E7=9B=98=E6=93=8D=E4=BD=9C=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/views/game/[level].vue | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e8ec73..31c7425 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ - [x] 本地缓存 - [ ] 彩色方块 - [x] 游戏音效 +- [x] 游戏界面增加键盘操作 - [ ] i18n - [ ] 积分商店,积分收集、兑换 - [ ] 道具系统 diff --git a/src/views/game/[level].vue b/src/views/game/[level].vue index 07bb573..c57b605 100644 --- a/src/views/game/[level].vue +++ b/src/views/game/[level].vue @@ -104,6 +104,10 @@ async function startGame() { } function onCheckResult() { + if (!gameStatus.value.playing) { + return + } + const { matched, blocks } = getAllCheckedResult() if (!matched && !blocks.length) { @@ -245,10 +249,25 @@ function onPageHideSaveLocalStatus() { })) } +// PC 端监听键盘按下事件 +function onBoardKeyDown({ code }: KeyboardEvent) { + const KEY_EVENTS_MAP = { + Enter: onCheckResult, + Delete: onResetBlocks, + } as const + + KEY_EVENTS_MAP[code as keyof typeof KEY_EVENTS_MAP]?.() +} + onMounted(() => { startGame() onRestoreLocalStatus() + // 非移动端增加键盘事件 + if (!window.ontouchstart) { + document.addEventListener('keydown', onBoardKeyDown) + } + document.addEventListener('visibilitychange', onGameTabVisibilityChange) window.addEventListener('pagehide', onPageHideSaveLocalStatus, { once: true }) }) @@ -256,9 +275,13 @@ onMounted(() => { onBeforeUnmount(() => { stopRecording() - document.removeEventListener('visibilitychange', onGameTabVisibilityChange) + if (!window.ontouchstart) { + document.removeEventListener('keydown', onBoardKeyDown) + } + // 如果是退出了当前页面再刷新的则不保存状态 window.removeEventListener('pagehide', onPageHideSaveLocalStatus) + document.removeEventListener('visibilitychange', onGameTabVisibilityChange) })