-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.js
More file actions
50 lines (44 loc) · 1.3 KB
/
Copy pathkeys.js
File metadata and controls
50 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { game_graphic } from './graphic';
import * as game from './game_status';
import { snake } from './snake/snake';
import { food } from './food/food';
function keyPressedLogic() {
if(game.lost) {
keyPressedWhileGameLost(game_graphic.keyCode);
} else if(game.pause) {
keyPressedWhilePaused(game_graphic.keyCode);
} else {
keyPressedWhilePlaying(game_graphic.keyCode);
}
}
function keyPressedWhilePlaying(keyCode) {
if(keyCode === game_graphic.UP_ARROW && !snake.isGoingDown()) {
snake.goUp();
} else if(keyCode === game_graphic.DOWN_ARROW && !snake.isGoingUp()) {
snake.goDown();
snake.changeSnakeDirection(0, 1)
} else if(keyCode === game_graphic.RIGHT_ARROW && !snake.isGoingLeft()) {
snake.goRight();
} else if(keyCode === game_graphic.LEFT_ARROW && !snake.isGoingRight()) {
snake.goLeft();
} else if(keyCode === game_graphic.ESCAPE) {
game_graphic.noLoop();
game.changePauseStatus();
}
}
function keyPressedWhilePaused(keyCode) {
if(keyCode === game_graphic.ESCAPE) {
game_graphic.loop();
game.changePauseStatus();
}
}
function keyPressedWhileGameLost(keyCode) {
let spaceBarCode = 32;
if(keyCode === spaceBarCode) {
game.changeLostStatus();
snake.init();
food.update();
game_graphic.loop();
}
}
export { keyPressedLogic };