From 7ff8021d5fbc960f7eecae612fb9ad6500a2f65c Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Mon, 6 Feb 2023 23:35:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=80=E5=A7=8B=E6=97=B6?= =?UTF-8?q?=E7=AB=8B=E5=8D=B3=E7=BB=93=E6=9D=9F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/GameController.ts | 6 ++++++ src/modules/Snake.ts | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/modules/GameController.ts b/src/modules/GameController.ts index 6c57bfe..8a33c01 100644 --- a/src/modules/GameController.ts +++ b/src/modules/GameController.ts @@ -49,6 +49,7 @@ class GameController { const horizontal = ["ArrowLeft", "Left", "ArrowRight", "Right"]; const vertical = ["ArrowUp", "Up", "ArrowDown", "Down"]; + const beginBan = ["ArrowUp", "Up", "ArrowLeft", "Left"]; // 防止水平调头 if (horizontal.includes(this.lastKeyDown) && horizontal.includes(event.key)) { @@ -60,6 +61,11 @@ class GameController return; } + // 防止游戏开始即结束 + if (this.snake.nowDirection === "" && beginBan.includes(event.key)) { + return; + } + // 设置当前蛇的行走方向 this.snake.nowDirection = event.key; // 记录此次键盘操作 diff --git a/src/modules/Snake.ts b/src/modules/Snake.ts index 07b8652..1744abe 100644 --- a/src/modules/Snake.ts +++ b/src/modules/Snake.ts @@ -78,6 +78,14 @@ class Snake this.direction = value; } + /** + * 返回蛇的行走方向 + */ + get nowDirection() : string + { + return this.direction + } + /** * 获取蛇的身体 */