-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
168 lines (163 loc) · 4.38 KB
/
script.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
const gameBoard = document.querySelector("#gameBoard");
const ctx = gameBoard.getContext("2d");
const scoreText = document.querySelector("#scoreText");
const resetBtn = document.querySelector("#resetBtn");
const gameWidth = gameBoard.width;
const gameHeight = gameBoard.height;
const boardBackground = "white";
const snakeColor = "lightblue";
const snakeBorder = "black";
const foodColor = "red";
const unitSize = 25;
let running = false;
let xVelocity = unitSize;
let yVelocity = 0;
let foodX;
let foodY;
let score = 0;
let snake = [
{x:unitSize*4, y:0},
{x:unitSize*3, y:0},
{x:unitSize*2, y:0},
{x:unitSize, y:0},
{x:0, y:0},
]
window.addEventListener("keydown", changeDirection);
resetBtn.addEventListener("click", resetGame);
gameStart();
function gameStart() {
running = true;
scoreText.textContent = `${score}/${(gameWidth * gameHeight)/1000}`;
createFood();
drawFood();
nextTick();
};
function nextTick() {
if (running) {
setTimeout(() => {
clearBoard();
drawFood();
moveSnake();
drawSnake();
checkGameOver();
checkWin();
nextTick();
}, 100)
}
else {
displayGameOver();
}
};
function clearBoard() {
ctx.fillStyle = boardBackground;
ctx.fillRect(0, 0, gameWidth, gameHeight);
};
function createFood() {
function randomFood(min, max) {
const randNum = Math.round((Math.random() * (max - min) + min) / unitSize) * unitSize;
return randNum
}
foodX = randomFood(0, gameWidth - unitSize);
foodY = randomFood(0, gameWidth - unitSize);
};
function drawFood() {
ctx.fillStyle = foodColor;
ctx.fillRect(foodX, foodY, unitSize, unitSize);
};
function moveSnake() {
const head = {x: snake[0].x + xVelocity, y: snake[0].y + yVelocity};
snake.unshift(head);
if (snake[0].x == foodX && snake[0].y == foodY) {
score += 1;
scoreText.textContent = `${score}/${(gameWidth * gameHeight)/1000}`;
createFood();
}
else {
snake.pop();
}
};
function drawSnake() {
ctx.fillStyle = snakeColor;
ctx.strokeStyle = snakeBorder;
snake.forEach(snakePart => {
ctx.fillRect(snakePart.x, snakePart.y, unitSize, unitSize);
ctx.strokeRect(snakePart.x, snakePart.y, unitSize, unitSize);
})
};
function changeDirection(event) {
const keyPressed = event.keyCode
const LEFT = 37;
const UP = 38;
const RIGHT = 39;
const DOWN = 40;
const goingUp = (yVelocity == -unitSize);
const goingDown = (yVelocity == unitSize);
const goingRight = (xVelocity == unitSize);
const goingLeft = (xVelocity == -unitSize);
switch(true) {
case(keyPressed == LEFT && !goingRight):
xVelocity = -unitSize;
yVelocity = 0;
break;
case(keyPressed == UP && !goingDown):
xVelocity = 0;
yVelocity = -unitSize;
break;
case(keyPressed == RIGHT && !goingLeft):
xVelocity = unitSize;
yVelocity = 0;
break;
case(keyPressed == DOWN && !goingUp):
xVelocity = 0;
yVelocity = unitSize;
break;
}
};
function checkWin() {
if (score == ((gameWidth * gameHeight)/1000)) {
ctx.font = "50px MV Boli";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText("You WON!", gameWidth / 2, gameHeight / 2)
}
}
function checkGameOver() {
switch(true) {
case (snake[0].x < 0):
running = false;
break;
case (snake[0].x >= gameWidth):
running = false;
break;
case (snake[0].y < 0):
running = false;
break;
case (snake[0].y >= gameHeight):
running = false;
break;
}
for (let i = 1; i < snake.length; i++) {
if (snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
running = false;
}
}
};
function displayGameOver() {
ctx.font = "50px MV Boli";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText("GAME OVER!", gameWidth / 2, gameHeight / 2)
};
function resetGame() {
score = 0;
xVelocity = unitSize;
yVelocity = 0;
snake = [
{x:unitSize*4, y:0},
{x:unitSize*3, y:0},
{x:unitSize*2, y:0},
{x:unitSize, y:0},
{x:0, y:0},
];
gameStart();
};