forked from JDStraughan/html5-snake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
216 lines (183 loc) · 5.34 KB
/
game.js
File metadata and controls
216 lines (183 loc) · 5.34 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var canvas = document.getElementById("the-game");
var context = canvas.getContext("2d");
var game, snake, food;
game = {
score: 0,
fps: 8,
over: false,
message: null,
start: function() {
game.over = false;
game.message = null;
game.score = 0;
game.fps = 8;
snake.init();
food.set();
},
stop: function() {
game.over = true;
game.message = 'GAME OVER - PRESS SPACEBAR';
},
drawBox: function(x, y, size, color) {
context.fillStyle = color;
context.beginPath();
context.moveTo(x - (size / 2), y - (size / 2));
context.lineTo(x + (size / 2), y - (size / 2));
context.lineTo(x + (size / 2), y + (size / 2));
context.lineTo(x - (size / 2), y + (size / 2));
context.closePath();
context.fill();
},
drawScore: function() {
context.fillStyle = '#999';
context.font = (canvas.height) + 'px Impact, sans-serif';
context.textAlign = 'center';
context.fillText(game.score, canvas.width / 2, canvas.height * 0.9);
},
drawMessage: function() {
if (game.message !== null) {
context.fillStyle = '#00F';
context.strokeStyle = '#FFF';
context.font = (canvas.height / 10) + 'px Impact';
context.textAlign = 'center';
context.fillText(game.message, canvas.width / 2, canvas.height / 2);
context.strokeText(game.message, canvas.width / 2, canvas.height / 2);
}
},
resetCanvas: function() {
context.clearRect(0, 0, canvas.width, canvas.height);
}
};
snake = {
size: canvas.width / 40,
x: null,
y: null,
color: '#0F0',
direction: 'left',
sections: [],
init: function() {
snake.sections = [];
snake.direction = 'left';
snake.x = canvas.width / 2 + snake.size / 2;
snake.y = canvas.height / 2 + snake.size / 2;
for (var i = snake.x + (5 * snake.size); i >= snake.x; i -= snake.size) {
snake.sections.push(i + ',' + snake.y);
}
},
move: function() {
switch (snake.direction) {
case 'up':
snake.y -= snake.size;
break;
case 'down':
snake.y += snake.size;
break;
case 'left':
snake.x -= snake.size;
break;
case 'right':
snake.x += snake.size;
break;
}
snake.checkCollision();
snake.checkGrowth();
snake.sections.push(snake.x + ',' + snake.y);
},
draw: function() {
for (var i = 0; i < snake.sections.length; i++) {
snake.drawSection(snake.sections[i].split(','));
}
},
drawSection: function(section) {
game.drawBox(parseInt(section[0]), parseInt(section[1]), snake.size, snake.color);
},
checkCollision: function() {
if (snake.isCollision(snake.x, snake.y) === true) {
game.stop();
}
},
isCollision: function(x, y) {
if (x < snake.size / 2 ||
x > canvas.width ||
y < snake.size / 2 ||
y > canvas.height ||
snake.sections.indexOf(x + ',' + y) >= 0) {
return true;
}
},
checkGrowth: function() {
if (snake.x == food.x && snake.y == food.y) {
game.score++;
if (game.score % 5 == 0 && game.fps < 60) {
game.fps++;
}
food.set();
} else {
snake.sections.shift();
}
}
};
food = {
size: null,
x: null,
y: null,
color: '#0FF',
set: function() {
food.size = snake.size;
food.x = (Math.ceil(Math.random() * 10) * snake.size * 4) - snake.size / 2;
food.y = (Math.ceil(Math.random() * 10) * snake.size * 3) - snake.size / 2;
},
draw: function() {
game.drawBox(food.x, food.y, food.size, food.color);
}
};
var inverseDirection = {
'up': 'down',
'left': 'right',
'right': 'left',
'down': 'up'
};
var keys = {
up: [38, 75, 87],
down: [40, 74, 83],
left: [37, 65, 72],
right: [39, 68, 76],
start_game: [13, 32]
};
function getKey(value){
for (var key in keys){
if (keys[key] instanceof Array && keys[key].indexOf(value) >= 0){
return key;
}
}
return null;
}
var CanPressButton = true
addEventListener("keydown", function (e) {
var lastKey = getKey(e.keyCode);
if (['up', 'down', 'left', 'right'].indexOf(lastKey) >= 0
&& lastKey != inverseDirection[snake.direction] && CanPressButton) {
snake.direction = lastKey;
CanPressButton = false // disable input until next frame
} else if (['start_game'].indexOf(lastKey) >= 0 && game.over) {
game.start();
}
}, false);
var requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame;
function loop() {
if (game.over == false) {
game.resetCanvas();
game.drawScore();
snake.move();
food.draw();
snake.draw();
game.drawMessage();
}
setTimeout(function() {
requestAnimationFrame(loop);
CanPressButton = true // reenable input for next frame NOTE: This is a bandaid fix I devised with about 3 hours of JS knowledge, it doesn't actually address the root problem.
}, 1000 / game.fps); // Ideally there would be a queue system to store the second input instead of dropping it entirely if there is two inputs on the same frame but I have no idea how to do that yet
} // Maybe I'll be back in a few months once I've finished this course to actually fix it, but at least right now instead of randomly dying, the game just drops the second input :p
requestAnimationFrame(loop);