-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (135 loc) · 3.79 KB
/
app.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
const cvs = document.getElementById("snake");
const ctx = cvs.getContext("2d");
//ADD EVENT LISTENER TO THE CANVAS
document.addEventListener("keydown", direction);
function direction(event) {
if (event.keyCode === 37 && d !== "RIGHT") {
d = "LEFT";
} else if (event.keyCode === 38 && d !== "DOWN") {
d = "UP";
} else if (event.keyCode === 39 && d !== "LEFT") {
d = "RIGHT";
} else if (event.keyCode === 40 && d !== "UP") {
d = "DOWN";
}
}
//BOX UNIT SET
const box = 25;
//canvas size of the snake its gonna move
const canvasSize = 23;
//SET DIRECTION
let d = "RIGHT";
//SET SCORE
let score = 0;
//SET SNAKE
snake = [];
snake[0] = {
x: 7 * box,
y: 10 * box,
};
snake[1] = {
x: 6 * box,
y: 10 * box,
};
//SET FOOD
let food = {
x: Math.floor(1 + Math.random() * (canvasSize - 1)) * box,
y: Math.floor(1 + Math.random() * (canvasSize - 1)) * box,
};
//THE DRAW FUNCTION
function draw() {
//DRAW BACKGROUND
ctx.fillStyle = "lightgreen";
ctx.fillRect(box, box, canvasSize * box - box, canvasSize * box - box);
//create TEXT
ctx.fillStyle = "white";
ctx.font = "30px Arial";
ctx.fillText(score, box, box);
//LOOP SNAKE TO DRAW THE BOXES
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i === 0 ? "red " : "black";
ctx.fillRect(snake[i].x, snake[i].y, box, box);
ctx.strokeStyle = "black";
ctx.strokeRect(snake[0].x, snake[0].y, box, box);
}
//SET THE OLD DIRECTIONS
snakeX = snake[0].x;
snakeY = snake[0].y;
//SET WHICH DIRECTION WE GONNA ADD THE NEW HEAD
if (d === "RIGHT") {
snakeX += box;
} else if (d === "LEFT") {
snakeX -= box;
} else if (d === "UP") {
snakeY -= box;
} else if (d === "DOWN") {
snakeY += box;
}
//SET FOOD
ctx.fillStyle = "red";
ctx.fillRect(food.x, food.y, box, box);
//SET THE NEW HEAD
let newHead = {
x: snakeX,
y: snakeY,
};
//GAME OVER IF CRASH ON WALLS
if (
snake[0].x > canvasSize * box - box * 2 ||
snake[0].x === box ||
snake[0].y > canvasSize * box - box * 2 ||
snake[0].y === box
) {
console.log("GAME OVER ");
ctx.fillStyle = "rgba(40,40,40, 0.5)";
ctx.fillRect(0, 0, 600, 600);
//game over text
ctx.font = "40px Arial";
ctx.fillStyle = "white";
ctx.fillText("GAME OVER!", 180, 260);
//restart game
ctx.font = "15px Arial";
ctx.fillStyle = "white";
ctx.fillText("Press any KEY to RESTART...", 210, 300);
clearInterval(game);
document.addEventListener("keydown", restart);
}
//GAME OVER IF CRASH ON ITSELF
for (let i = 1; i < snake.length; i++) {
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
console.log("GAME OVER ");
ctx.fillStyle = "rgba(40,40,40, 0.7)";
ctx.fillRect(0, 0, 600, 600);
//game over text
ctx.font = "40px Arial";
ctx.fillStyle = "white";
ctx.fillText("GAME OVER!", 180, 260);
//restart game
ctx.font = "15px Arial";
ctx.fillStyle = "white";
ctx.fillText("Press any KEY to RESTART...", 210, 300);
clearInterval(game);
document.addEventListener("keydown", restart);
}
}
//SET WHEN SNAKE EAT FOOD
if (snake[0].x === food.x && snake[0].y === food.y) {
food = {
x: Math.floor(1 + Math.random() * (canvasSize - 1)) * box,
y: Math.floor(1 + Math.random() * (canvasSize - 1)) * box,
};
ctx.clearRect(box, 0, 600, box);
score += 100;
} else {
//REMOVE THE LAST BOX OF THE SNAKE
snake.pop();
}
//WE PUSH INTO SNAKE ARRAY THE NEW HEAD
snake.unshift(newHead);
}
function restart(event) {
console.log("restart");
location.reload();
game = setInterval(draw, 100);
}
let game = setInterval(draw, 100);