-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
180 lines (163 loc) · 4.97 KB
/
snake.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
const SnakeGame = function() {
const CANVAS_BORDER_COLOUR = 'black';
const CANVAS_BACKGROUND_COLOUR = 'white';
const SNAKE_FILL_COLOUR = 'lightGreen';
const SNAKE_BORDER_COLOUR = 'darkGreen';
const FOOD_FILL_COLOUR = 'red';
const FOOD_BORDER_COLOUR = 'darkRed';
const GAME_CANVAS = document.getElementById('gameCanvas');
const CTX = GAME_CANVAS.getContext('2d');
let score;
let snakeSpeed;
let gameLoop;
let snake;
let velocityX;
let velocityY;
let foodX;
let foodY;
let changingDirection;
let inProgress;
let changeSpeedValue;
let gridCellSize;
let foodScoreValue;
const initialize = () => {
score = 0;
snakeSpeed = 200;
snake = [
{ x: 150, y: 150 },
{ x: 140, y: 150 },
{ x: 130, y: 150 },
{ x: 120, y: 150 },
{ x: 110, y: 150 }
];
velocityX = 10;
velocityY = 0;
changingDirection = false;
inProgress = true;
changeSpeedValue = 10;
gridCellSize = 10;
foodScoreValue = 10;
updateGameData();
};
//create game elements
const setCanvas = () => {
CTX.fillStyle = CANVAS_BACKGROUND_COLOUR;
CTX.strokeStyle = CANVAS_BORDER_COLOUR;
CTX.fillRect(0, 0, GAME_CANVAS.width, GAME_CANVAS.height);
CTX.strokeRect(0, 0, GAME_CANVAS.width, GAME_CANVAS.height);
};
const drawSnakeUnit = snakeUnit => {
CTX.fillStyle = SNAKE_FILL_COLOUR;
CTX.strokeStyle = SNAKE_BORDER_COLOUR;
CTX.fillRect(snakeUnit.x, snakeUnit.y, gridCellSize, gridCellSize);
CTX.strokeRect(snakeUnit.x, snakeUnit.y, gridCellSize, gridCellSize);
};
const drawFood = () => {
CTX.fillStyle = FOOD_FILL_COLOUR;
CTX.strokeStyle = FOOD_BORDER_COLOUR;
CTX.fillRect(foodX, foodY, gridCellSize, gridCellSize);
CTX.strokeRect(foodX, foodY, gridCellSize, gridCellSize);
};
//food logic
const randomMultipleOfTen = (min, max) => {
return (
Math.round((Math.random() * (max - min) + min) / gridCellSize) *
gridCellSize
);
};
const makeFood = () => {
foodX = randomMultipleOfTen(0, GAME_CANVAS.width - gridCellSize);
foodY = randomMultipleOfTen(0, GAME_CANVAS.height - gridCellSize);
snake.forEach(function isFoodOnSnake(part) {
const foodIsOnSnake = part.x == foodX && part.y == foodY;
if (foodIsOnSnake) makeFood();
});
};
//snake logic
const drawSnake = () => {
snake.forEach(drawSnakeUnit);
};
const moveSnake = () => {
const head = { x: snake[0].x + velocityX, y: snake[0].y + velocityY };
snake.unshift(head);
const snakeAteFood = snake[0].x === foodX && snake[0].y === foodY;
if (snakeAteFood) {
score += foodScoreValue;
snakeSpeed -= changeSpeedValue;
updateGameData();
makeFood();
} else {
snake.pop();
}
};
const changeDirection = event => {
const keyPressed = event.code;
const movingUp = velocityY === -gridCellSize;
const movingDown = velocityY === gridCellSize;
const movingRight = velocityX === gridCellSize;
const movingLeft = velocityX === -gridCellSize;
if (changingDirection) return;
changingDirection = true;
if (keyPressed === 'ArrowLeft' && !movingRight) {
event.preventDefault();
velocityX = -gridCellSize;
velocityY = 0;
} else if (keyPressed === 'ArrowUp' && !movingDown) {
event.preventDefault();
velocityX = 0;
velocityY = -gridCellSize;
} else if (keyPressed === 'ArrowRight' && !movingLeft) {
event.preventDefault();
velocityX = gridCellSize;
velocityY = 0;
} else if (keyPressed === 'ArrowDown' && !movingUp) {
event.preventDefault();
velocityX = 0;
velocityY = gridCellSize;
}
};
//game logic
const didGameEnd = () => {
// init i at 4 because first 4 units of snake cannot collide with each other
for (let i = 4; i < snake.length; i++) {
const didCollide = snake[i].x === snake[0].x && snake[i].y === snake[0].y;
if (didCollide) return true;
}
const hitLeftBorder = snake[0].x < 0;
const hitRightBorder = snake[0].x > GAME_CANVAS.width - gridCellSize;
const hitTopBorder = snake[0].y < 0;
const hitBottomBorder = snake[0].y > GAME_CANVAS.height - gridCellSize;
return hitLeftBorder || hitRightBorder || hitTopBorder || hitBottomBorder;
};
const updateGameData = () => {
document.getElementById('score').innerHTML = score;
document.getElementById('speed').innerHTML = snakeSpeed;
};
const gameEngine = () => {
if (didGameEnd()) {
alert('You died!');
clearInterval(gameLoop);
inProgress = false;
return;
}
gameLoop = setTimeout(function() {
changingDirection = false;
setCanvas();
drawSnake();
drawFood();
moveSnake();
gameEngine();
}, snakeSpeed);
};
document.addEventListener('keydown', changeDirection);
document.getElementById('startGame').addEventListener('click', function() {
if (!inProgress) {
initialize();
gameEngine();
makeFood();
} else {
return;
}
});
};
SnakeGame();