Skip to content

Commit

Permalink
feat: move
Browse files Browse the repository at this point in the history
  • Loading branch information
tianqing617 committed Nov 12, 2021
1 parent f5b31b8 commit 4fd4f35
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/GameControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class GameControl {

direction = '';
isAlive = true;
setup = 10; // 每次移动的距离

constructor() {
this.snake = new Snake();
Expand All @@ -26,9 +27,34 @@ export default class GameControl {
keydownHandler(event: KeyboardEvent): void {
console.log(event);
this.direction = event.key;
this.run();
}

run(): void {
// ArrowRight ArrowLeft ArrowUp ArrowDown
const pointer = this.snake.headPointer;

switch (this.direction) {
case 'ArrowUp':
pointer.y -= 10;
break;
case 'ArrowDown':
pointer.y += 10;
break;
case 'ArrowLeft':
pointer.x -= 10;
break;
case 'ArrowRight':
pointer.x += 10;
break;
}

try {
this.snake.headPointer = pointer;
} catch (error: any) {
const message = error.message;
console.log(message);
this.isAlive = false;
}
}
}

0 comments on commit 4fd4f35

Please sign in to comment.