-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (174 loc) · 3.97 KB
/
index.js
File metadata and controls
220 lines (174 loc) · 3.97 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
// Declare UI vars
let scoreDisplay = document.getElementById("score")
const restartBtn = document.getElementById("restartBtn")
const canvas = document.getElementById("game")
const ctx = canvas.getContext("2d")
let gameOver = false
class snakeBody {
constructor(x, y) {
this.x = x
this.y = y
}
}
let speed = 5 // Initial speed
let squareCount = 20 // Size of each square
let squareSize = canvas.width / squareCount - 2 // Size of snake
let foodSize = canvas.width / squareCount - 10 // size of food
let score = 0
let headX;
let headY;
let snakeParts = [];
let tailLength;
let appleX;
let appleY;
let xDirection;
let yDirection;
function init() {
// Snake head position
headX = 5
headY = 2
snakeParts = []
tailLength = 2
// Food position
appleX = 12
appleY = 8
// Snake initial direction
xDirection = 0
yDirection = 0
xDirection = 1
displayScore()
}
init()
function drawGame() {
moveSnake()
gameOver = false
let result = isGameOver()
if(result) {
init();
return
}
clearScreen()
generateApple()
drawApple()
drawSnake()
if(score > 4) {
speed = 8
}
if(score > 7) {
speed = 11
}
setTimeout(drawGame, 1000 / speed)
}
function isGameOver() {
let gameOver = false
// Game hasnt started
if (xDirection === 0 && yDirection === 0) {
return false
}
// Hit walls
if(headX < 0) {
gameOver = true
} else if (headX === squareCount) {
gameOver = true
} else if (headY < 0) {
gameOver = true
} else if (headY === squareCount) {
gameOver = true
}
// Hit snake body parts
for (let i = 0; i <snakeParts.length; i++) {
let part = snakeParts[i]
if(part.x === headX && part.y === headY) {
gameOver = true
break
}
}
if(gameOver) {
// ctx.fillStyle = "yellow"
ctx.font = "40px Monospace"
var gradient = ctx.createLinearGradient(0, 0, canvas.width, 0)
gradient.addColorStop(0.1, "lawngreen")
gradient.addColorStop(0.5, "blue")
gradient.addColorStop(0.7, "orangered")
ctx.fillStyle = gradient
ctx.fillText("G A M E O V E R", canvas.width / 15, canvas.height / 2)
}
return gameOver
}
function clearScreen() {
ctx.fillStyle = "black"
ctx.fillRect(0, 0, canvas.width, canvas.height)
}
function drawSnake() {
ctx.fillStyle = "magenta"
for (let i = 0; i < snakeParts.length; i++) {
let part = snakeParts[i]
ctx.fillRect(part.x * squareCount, part.y * squareCount, squareSize, squareSize)
}
snakeParts.push(new snakeBody(headX, headY)) // add an item at the end (last) which is next to the head
while (snakeParts.length > tailLength) {
snakeParts.shift() // remove furthest item from the snake parts
}
ctx.fillStyle = "lime"
ctx.fillRect(headX * squareCount, headY * squareCount, squareSize, squareSize)
}
function moveSnake() {
headX = headX + xDirection
headY = headY + yDirection
}
function drawApple() {
ctx.fillStyle = "red"
ctx.fillRect(appleX * squareCount, appleY * squareCount, foodSize, foodSize)
}
// When Snake eats apple
function generateApple() {
if (appleX === headX && appleY === headY) {
appleX = Math.floor(Math.random() * squareCount)
appleY = Math.floor(Math.random() * squareCount)
tailLength++
score++
displayScore()
}
}
function displayScore() {
scoreDisplay.textContent = score
if (score > 4) {
score += 1
}
if (score > 9) {
score += 1
}
}
document.addEventListener("keyup", control)
function control(e) {
// up
if (e.keyCode === 38) {
if (yDirection === 1)
return;
xDirection = 0
yDirection = -1
}
// right
if (e.keyCode === 39) {
if(xDirection === -1)
return;
xDirection = 1
yDirection = 0
}
// left
if (e.keyCode === 37) {
if (xDirection === 1)
return;
xDirection = -1
yDirection = 0
}
// down
if (e.keyCode === 40) {
if (yDirection === -1)
return;
xDirection = 0
yDirection = 1
}
}
drawGame()
restartBtn.addEventListener("click", drawGame);