-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake2.js
209 lines (173 loc) · 5.62 KB
/
snake2.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
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
var GAME_SPEED ;
var step=0;
function start(counter) {
GAME_SPEED = counter;
const SNAKE_COLOUR = '#B6FF00';
const SNAKE_BORDER_COLOUR = 'black';
const FOOD_COLOUR = 'red';
const FOOD_BORDER_COLOUR = 'darkred';
let snake = [
{x: 160, y: 160},
{x: 128, y: 160},
{x: 96, y: 160},
{x: 64, y: 160},
{x: 32, y: 160},
]
let score = 0;
let changingDirection = false;
let foodX;
let foodY;
let dx = 32;
let dy = 0;
const gameCanvas = document.getElementById("gameCanvas");
const ctx = gameCanvas.getContext("2d");
main();
createFood();
document.addEventListener("keydown", changeDirection);
function main() {
document.getElementById('step').innerHTML = "Step: " + step;
document.getElementById('score').innerHTML = "Score: " + score;
if (didGameEnd()) {
clearCanvas();
document.getElementById('gameOver').innerHTML = "<br>"+"Game Over!!"+ "<br>" + '<button onclick="Reload()" class="btn btn-primary tot">Restart</button>'
return
};
setTimeout(function onTick() {
changingDirection = false;
clearCanvas();
drawFood();
advanceSnake();
drawSnake();
main();
}, GAME_SPEED)
}
function clearCanvas() {
ctx.clearRect(0,0,gameCanvas.width,gameCanvas.height);
for(var i=0;i<gameCanvas.height;i+=64){
for(var j=0;j<gameCanvas.width;j+=64){
ctx.fillStyle = 'rgb(172, 115, 57)';
ctx.fillRect(i,j,32,32);
}
}
for(var i=32;i<gameCanvas.height;i+=64){
for(var j=32;j<gameCanvas.width;j+=64){
ctx.fillStyle = 'rgb(172, 115, 57)';
ctx.fillRect(i,j,32,32);
}
}
for(var i=0;i<gameCanvas.height;i+=64){
for(var j=32;j<gameCanvas.width;j+=64){
ctx.fillStyle = 'rgb(217, 179, 140)';
ctx.fillRect(i,j,32,32);
}
}
for(var i=32;i<gameCanvas.height;i+=64){
for(var j=0;j<gameCanvas.width;j+=64){
ctx.fillStyle = 'rgb(217, 179, 140)';
ctx.fillRect(i,j,32,32);
}
}
}
function drawFood() {
ctx.fillStyle = FOOD_COLOUR;
ctx.strokestyle = FOOD_BORDER_COLOUR;
ctx.fillRect(foodX, foodY, 32, 32);
ctx.strokeRect(foodX, foodY, 32, 32);
}
function advanceSnake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
const didEatFood = snake[0].x === foodX && snake[0].y === foodY;
if (didEatFood) {
score += 10;
createFood();
} else {
snake.pop();
}
}
function didGameEnd() {
for (let i = 4; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true
}
const hitLeftWall = snake[0].x < 0;
const hitRightWall = snake[0].x > gameCanvas.width - 32;
const hitToptWall = snake[0].y < 0;
const hitBottomWall = snake[0].y > gameCanvas.height - 32;
return hitLeftWall || hitRightWall || hitToptWall || hitBottomWall
}
/**
* Generates a random number that is a multiple of 10 given a minumum
* and a maximum number
* @param { number } min - The minimum number the random number can be
* @param { number } max - The maximum number the random number can be
*/
function randomTen(min, max) {
return Math.round((Math.random() * (max-min) + min) / 32) * 32;
}
function createFood() {
foodX = randomTen(0, gameCanvas.width - 32);
foodY = randomTen(0, gameCanvas.height - 32);
snake.forEach(function isFoodOnSnake(part) {
const foodIsoNsnake = part.x == foodX && part.y == foodY;
if (foodIsoNsnake) createFood();
});
}
function drawSnake() {
snake.forEach(drawSnakePart)
}
/**
* Draws a part of the snake on the canvas
* @param { object } snakePart - The coordinates where the part should be drawn
*/
function drawSnakePart(snakePart) {
ctx.fillStyle = SNAKE_COLOUR;
ctx.strokestyle = SNAKE_BORDER_COLOUR;
ctx.fillRect(snakePart.x, snakePart.y, 32, 32);
ctx.strokeRect(snakePart.x, snakePart.y, 32,32);
}
/**
* Changes the vertical and horizontal velocity of the snake according to the
* key that was pressed.
* The direction cannot be switched to the opposite direction, to prevent the snake
* from reversing
* For example if the the direction is 'right' it cannot become 'left'
* @param { object } event - The keydown event
*/
function changeDirection(event) {
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
const UP_KEY = 38;
const DOWN_KEY = 40;
if (changingDirection) return;
changingDirection = true;
const keyPressed = event.keyCode;
const goingUp = dy === -32;
const goingDown = dy === 32;
const goingRight = dx === 32;
const goingLeft = dx === -32;
if (keyPressed === LEFT_KEY && !goingRight) {
dx = -32;
dy = 0;
step+=1;
}
if (keyPressed === UP_KEY && !goingDown) {
dx = 0;
dy = -32;
step+=1;
}
if (keyPressed === RIGHT_KEY && !goingLeft) {
dx = 32;
dy = 0;
step+=1;
}
if (keyPressed === DOWN_KEY && !goingUp) {
dx = 0;
dy = 32;
step+=1;
}
}
}
function Reload(){
location.reload();
}
console.log("%cDHWAJ GUPTA! Thomso IITR", "color: green; font-size:30px;");