This repository has been archived by the owner on May 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_controller.js
130 lines (102 loc) · 3.19 KB
/
snake_controller.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var controller = {
init: function() {
model.gridSize = view.getGridSize();
model.moveSpeed = view.getMoveSpeed();
model.rotateSpeed = 100;
view.init(model.gridSize);
view.setBoard();
this.setItems();
view.renderMenu('Press SPACE to begin!');
view.waitForStart();
},
setItems: function() {
// Set snakeCoords and appleCoords in model
model.setSnakeInitial();
model.setApple();
// Render snake & apple via view object
view.renderSnake(model.snakeCoords);
view.addApple(model.appleCoords);
},
gameLoop: undefined,
rotateLoop: undefined,
runGameLoop: function() {
// Disable input so that space can't be pressed again
view.disableInput();
// Game loop that updates every model.moveSpeed seconds
this.gameLoop = setInterval(function() {
// Move snake
model.moveSnake();
view.renderSnake(model.snakeCoords, model.oldTailCoords);
// Look for direction changes (event listener)
view.lookForDirectionInput();
// Check if snake head and apple match, then act accordingly
if (model.appleEaten()) {
model.growSnake();
view.renderSnake(model.snakeCoords, model.oldTailCoords);
view.removeApple(model.appleCoords);
model.appleCoords = model.getRandCoords();
view.addApple(model.appleCoords);
};
// Check if snake collided with itself or a wall, and act accordingly
if (model.snakeCollided()) {
// Clear intervals
clearInterval(controller.gameLoop);
clearInterval(controller.rotateLoop);
// Reset game
view.renderMenu('Game over! Press SPACE to play again.');
model.resetGame();
view.resetBoard();
controller.setItems();
view.disableInput();
view.waitForStart();
};
}, model.moveSpeed);
},
runRotateLoop: function() {
// Rotate board
this.rotateLoop = setInterval(function() {
// Set rotation direction && randomize speed
if (model.rotation === model.rotationMax || model.rotation === model.rotationMin) {
let newRotation = (Math.floor(Math.random() * 5) + 1) * 0.5;
if (model.rotationRate > 0) {
model.rotationRate = -newRotation;
} else {
model.rotationRate = newRotation;
}
}
// Increase rotation angle
model.rotation += model.rotationRate;
// Set CSS
$('.gameboard').css({
'-ms-transform': 'rotate(' + model.rotation + 'deg)',
'-webkit-transform': 'rotate(' + model.rotation + 'deg)',
'transform': 'rotate((' + model.rotation + 'deg)'
})
}, model.rotateSpeed);
},
directionResponse: function(keyCode) {
switch(keyCode) {
case 38: // Arrow Up
case 87: // 'w'
model.updateDirection('U');
break;
case 40: // Arrow Down
case 83: // 's'
model.updateDirection('D');
break;
case 37: // Arrow Left
case 65: // 'a'
model.updateDirection('L');
break;
case 39: // Arrow Right
case 68: // 'd'
model.updateDirection('R');
break;
default:
return;
};
}
}
$(document).ready(function() {
controller.init();
});