Skip to content

Commit

Permalink
Add simple gamepad support
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Apr 22, 2018
1 parent e929617 commit 0697eb9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
17 changes: 17 additions & 0 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ class Snake {
}

run() {
{
const gamepad = navigator.getGamepads()[0];
if (gamepad) {
if (gamepad.axes[0] > .5) {
this.nextDirection = Direction.Right;
} else if (gamepad.axes[0] < -.5) {
this.nextDirection = Direction.Left;
} else if (gamepad.axes[1] > .5) {
this.nextDirection = Direction.Down;
} else if (gamepad.axes[1] < -.5) {
this.nextDirection = Direction.Up;
}
}
}

this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
this.move();
if (!this.food) this.generateFood();
Expand Down

0 comments on commit 0697eb9

Please sign in to comment.