-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
241 lines (207 loc) · 5.14 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//////////////// Audio & Audio Context ///////////////
const AudioContext = window.AudioContext || window.webkitAudioContext
const audioCtx = new AudioContext()
const audioElement = document.querySelector('audio')
const track = audioCtx.createMediaElementSource(audioElement)
const gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5
track.connect(audioCtx.destination)
//////////////// Canvas & Render Context ///////////////
const canvas = document.getElementById('game')
const ctx = canvas.getContext('2d')
//////////////// Variables ///////////////
let touchStartY
let touchStartX
let lastTouchDirection
const frameTime = 90
const gridWidthInCells = 50
const cellSize = canvas.width / gridWidthInCells
// Game Objects
const game = {
score: 0,
}
const snake = {
parts: [{ x: 10, y: 10 }],
direction: 'RIGHT',
grow: function () {
this.parts.push({
x: snake.parts[snake.parts.length - 1].x,
y: snake.parts[snake.parts.length - 1].y,
})
},
}
const apple = {
x: 19,
y: 10,
alive: true,
}
const keysDown = {}
//////////////// Eventlisteners ///////////////
window.addEventListener('load', startGame)
window.addEventListener('keydown', (e) => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
const pressedKey = e.key
for (let key in keysDown) {
keysDown[key] = false
}
keysDown[pressedKey] = true
})
window.addEventListener('keyup', (e) => {
const key = e.key
keysDown[key] = false
})
window.addEventListener('touchstart', (e) => {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
touchStartY = e.touches[0].clientY
touchStartX = e.touches[0].clientX
})
window.addEventListener('touchmove', (e) => {
const touchEndY = e.touches[0].clientY
const touchEndX = e.touches[0].clientX
if (['RIGHT', 'LEFT'].includes(snake.direction)) {
if (touchStartY < touchEndY) {
lastTouchDirection = 'DOWN'
} else {
lastTouchDirection = 'UP'
}
} else {
if (touchStartX < touchEndX) {
lastTouchDirection = 'RIGHT'
} else {
lastTouchDirection = 'LEFT'
}
}
})
window.addEventListener('touchend', () => {
if(lastTouchDirection !== snake.direction) snake.direction = lastTouchDirection
})
////////////// Functions ///////////////////
function newApple() {
apple.x = Math.floor(Math.random() * gridWidthInCells)
apple.y = Math.floor(Math.random() * gridWidthInCells)
apple.alive = true
}
function drawSnake(snake) {
ctx.fillStyle = 'black'
snake.parts.forEach((part) => {
const xInPixels = part.x * cellSize
const yInPixels = part.y * cellSize
ctx.fillRect(xInPixels, yInPixels, cellSize, cellSize)
})
}
function drawApple(apple) {
if (apple.alive === true) {
const xInPixels = apple.x * cellSize
const yInPixels = apple.y * cellSize
ctx.fillStyle = 'red'
ctx.fillRect(xInPixels, yInPixels, cellSize, cellSize)
}
}
function inputHandler() {
if (snake.direction === 'UP' || snake.direction === 'DOWN') {
if (keysDown.ArrowLeft) {
snake.direction = 'LEFT'
}
if (keysDown.ArrowRight) {
snake.direction = 'RIGHT'
}
}
if (snake.direction === 'LEFT' || snake.direction === 'RIGHT') {
if (keysDown.ArrowUp) {
snake.direction = 'UP'
}
if (keysDown.ArrowDown) {
snake.direction = 'DOWN'
}
}
}
function keepSnakeOnCanvas() {
let head = snake.parts[0]
if (head.x * cellSize + cellSize > canvas.width) {
head.x = 0
}
if (head.y * cellSize + cellSize > canvas.height) {
head.y = 0
}
if (head.x * cellSize < 0) {
head.x = gridWidthInCells
}
if (head.y * cellSize < 0) {
head.y = gridWidthInCells
}
}
function moveSnake() {
keepSnakeOnCanvas()
const tail = snake.parts[snake.parts.length - 1]
const head = snake.parts[0]
switch (snake.direction) {
case 'RIGHT':
tail.x = head.x + 1
tail.y = head.y
break
case 'DOWN':
tail.y = head.y + 1
tail.x = head.x
break
case 'LEFT':
tail.x = head.x - 1
tail.y = head.y
break
case 'UP':
tail.y = head.y - 1
tail.x = head.x
break
}
snake.parts.unshift(snake.parts.pop())
}
function eatApple() {
if (snake.parts[0].x === apple.x && snake.parts[0].y === apple.y) {
apple.alive = false
snake.grow()
if(audioCtx.state === 'running') audioElement.play()
newApple()
game.score += 100
}
}
function renderScore() {
document.getElementById('score').innerHTML = 'Score: ' + game.score
}
function loseGame() {
const head = snake.parts[0]
if (snake.parts.length >= 3) {
for (i = 1; i < snake.parts.length; i++) {
let part = snake.parts[i]
if (head.x === part.x && head.y === part.y) {
alert(`Game Over! Your score is + ${game.score}.`)
reset()
}
}
}
}
function reset() {
snake.parts = [{ x: 10, y: 10 }]
snake.direction = 'RIGHT'
location.reload()
}
function updateGame() {
inputHandler()
moveSnake()
eatApple()
renderScore()
loseGame()
}
function drawFrame() {
canvas.width = canvas.width // clear canvas FTW?!
drawSnake(snake)
drawApple(apple)
}
function startGame() {
setInterval(() => {
updateGame()
drawFrame()
}, frameTime)
}