Skip to content

Commit

Permalink
feat: direction
Browse files Browse the repository at this point in the history
  • Loading branch information
tianqing617 committed Nov 18, 2021
1 parent 954d833 commit 8129981
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
37 changes: 20 additions & 17 deletions src/GameControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class GameControl {
score: Score;

timer = -1;
direction = DirectionEnum.RIGHT;
direction = DirectionEnum.ArrowRight;
isAlive = true;
setup = 10; // 每次移动的距离

Expand Down Expand Up @@ -46,20 +46,23 @@ export default class GameControl {
}

keydownHandler(event: KeyboardEvent): void {
// console.log(event);
const key = Object.keys(DirectionEnum).find(key => {
console.log(event);
// TODO: 枚举,判断是否属于枚举中的某项目的方案
console.log('enum', DirectionEnum);
const keys = Object.keys(DirectionEnum);
// 是否是此四个键:ArrowRight ArrowLeft ArrowUp ArrowDown
if (keys.includes(event.key)) {
// @ts-ignore
return DirectionEnum[key] === event.key;
});

console.log('directionType', key);

if (key) {
// @ts-ignore
this.direction = DirectionEnum[key];
console.log('num', DirectionEnum[event.key], this.direction);

// @ts-ignore 2 表示枚举中间值
const available = (DirectionEnum[event.key] > 2 && this.direction < 2) || (DirectionEnum[event.key] < 2 && this.direction > 2);
if (available) {
// 大于2,表示左右;小于2,表示上下
// @ts-ignore
this.direction = DirectionEnum[event.key];
}
}

// this.run();
}

getSpeed(): number {
Expand All @@ -82,16 +85,16 @@ export default class GameControl {
const pointer = this.snake.headPointer;

switch (this.direction) {
case 'ArrowUp':
case DirectionEnum.ArrowUp:
pointer.y -= 10;
break;
case 'ArrowDown':
case DirectionEnum.ArrowDown:
pointer.y += 10;
break;
case 'ArrowLeft':
case DirectionEnum.ArrowLeft:
pointer.x -= 10;
break;
case 'ArrowRight':
case DirectionEnum.ArrowRight:
pointer.x += 10;
break;
}
Expand Down
9 changes: 5 additions & 4 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ArrowRight ArrowLeft ArrowUp ArrowDown
// 蛇的移动方向
enum DirectionEnum {
UP = 'ArrowUp',
DOWN = 'ArrowDown',
LEFT = 'ArrowLeft',
RIGHT = 'ArrowRight',
ArrowUp = 0,
ArrowDown = 1,

ArrowLeft = 3,
ArrowRight = 4,
}

export {
Expand Down

0 comments on commit 8129981

Please sign in to comment.