-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakegame.html
More file actions
222 lines (179 loc) · 5.5 KB
/
Copy pathsnakegame.html
File metadata and controls
222 lines (179 loc) · 5.5 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
217
218
219
220
221
222
<!-- Hi! It's nice to see you here. :) If you have questions or suggestions,
please email me at simrankbhatt@gmail.com. Happy snaking!
Modifications:
Snake resizes itself to 4 after reaching a length of 50
Added a reset button (R on the keyboard)
Added a score and high score display
snake now bounces back from the left & right walls and ceiling of the canvas rather than passing through to the other side.
Canvas size is larger
-->
<!DOCTYPE html>
<html>
<head>
<title>Snake!</title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 10px solid white;
}
</style>
</head>
<body>
<canvas width="800" height="600" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
//snake object
var snake = {
x: 80,
y: 80,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
score: 0,
highScore: 0,
// length of the snake. grows when eating an apple
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
//function to produce a random integer within a given range
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function loop() {
requestAnimationFrame(loop);
//speed of snake. 60/15 = 4
if(++count < 4) {
return;
}
count = 0;
context.clearRect(0, 0, canvas.width, canvas.height);
//moving the snake by its defined velocity (see snake object)
snake.x += snake.dx;
snake.y += snake.dy;
//keeping the snake within canvas boundaries
if(snake.x <= 0) {
snake.x = 0;
snake.dx = grid;
snake.dy = 0;
} else if(snake.x >= canvas.width) {
snake.x = canvas.width;
snake.dx = -grid;
snake.dy = 0;
}
if(snake.y <= 0) {
snake.y = 0;
snake.dy = 16;
snake.dx = 0;
} else if(snake.y >= canvas.height) {
snake.y = 0;
snake.dy = 16;
snake.dx = 0;
}
//adding the current coordinate of the snake to the array keeping track of snake length
snake.cells.unshift({x: snake.x, y: snake.y});
//make sure the snake length is as defined
if(snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
//score count
context.fillStyle = 'yellow';
context.font = "20px Georgia";
context.fillText("Score: ", 15, 30);
context.font = "23px Georgia";
context.fillText(snake.score, 75, 30);
//high score count
context.font = "14px Serif";
context.fillText("High Score: ", 15, 55);
context.font = "15px Serif";
context.fillText(snake.highScore, 85, 55);
//draw apple
context.fillStyle = 'red';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
//draw snake
context.fillStyle = 'green';
//fill each coordinate stored in the cells array (in the snake object) with green
snake.cells.forEach(function(cell, index) {
context.fillRect(cell.x, cell.y, grid-1, grid-1);
//increase snake length when it 'eats' the apple
if(cell.x == apple.x && cell.y == apple.y) {
snake.maxCells++;
snake.score++;
if(snake.score > snake.highScore) {
snake.highScore = snake.score;
}
//canvas size is 800x600 = 75x25 grids?
apple.x = getRandomInteger(0, 50) * grid;
apple.y = getRandomInteger(0, 25) * grid;
}
}); //close forEach (snake drawing function)
//reset snake size when it reaches a size of 50. This prevents the snake from taking up most of the screen and affecting the gameplay
if(snake.maxCells >= 50) {
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
} //close if statement
} //close game loop
// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
// prevent snake from backtracking on itself by checking that it's
// not already moving on the same axis (pressing left while moving
// left won't do anything, and pressing right while moving left
// shouldn't let you collide with your own body)
//reset button (press R on the keyboard to reset the game)
if(e.which == 82) {
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
snake.score = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
// left arrow key
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
// start the game
requestAnimationFrame(loop);
</script> <!-- javascript ends -->
</body>
</html>