-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (149 loc) · 3.57 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
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
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
class SnakePart {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let speed = 7;
let tileCount = 20;
let tileSize = canvas.width / tileCount - 2;
let headX = 10;
let headY = 10;
const snakeParts = [];
let tailLength = 2;
let AppleX = 5;
let AppleY = 5;
let xVelocity = 0;
let yVelocity = 0;
let score = 0;
const gulpSound = new Audio("gulp.mp3")
const failSound = new Audio("fail.mp3")
// Game Loop
const drawGame = () => {
changeSnakePosition()
let result = isGameOver()
if (result) {
return
}
clearScreen()
checkAppleColison()
drawApple()
drawSnake()
drawScore()
if(score > 2) {
speed = 11
}
if(score > 5) {
speed = 25
}
setTimeout(drawGame, 1000 / speed)
}
const isGameOver = () => {
let gameOver = false
if (yVelocity === 0 && xVelocity === 0) {
return false
}
if (headX < 0) {
gameOver = true
failSound.play()
}
else if (headX === tileCount) {
gameOver = true
failSound.play()
}
else if (headY < 0) {
gameOver = true
failSound.play()
}
else if (headY === tileCount) {
gameOver = true
failSound.play()
}
for (let i = 0; i < snakeParts.length; i++) {
let part = snakeParts[i]
if (part.x === headX && part.y === headY) {
gameOver = true
failSound.play()
break
}
}
if (gameOver) {
ctx.filStyle = "white"
ctx.font = "50px Courier"
ctx.fillText("Game Over", canvas.width / 6.6, canvas.height / 2)
}
return gameOver;
}
const clearScreen = () => {
ctx.fillStyle = '#333';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
const drawScore = () => {
ctx.fillStyle = "green";
ctx.font = '10px Verdana';
ctx.fillText("Score: " + score, canvas.width - 50, 10);
}
const drawSnake = () => {
ctx.filStyle = '#000';
for (let i = 0; i < snakeParts.length; i++) {
let part = snakeParts[i]
ctx.fillRect(part.x * tileCount, part.y * tileCount, tileSize, tileSize)
}
snakeParts.push(new SnakePart(headX, headY));
while (snakeParts.length > tailLength) {
snakeParts.shift()
}
ctx.fillStyle = '#FFF';
ctx.fillRect(headX * tileCount, headY * tileCount, tileSize, tileSize)
}
const changeSnakePosition = () => {
headX = headX + xVelocity;
headY = headY + yVelocity;
}
const drawApple = () => {
ctx.fillStyle = 'purple';
ctx.fillRect(AppleX * tileCount, AppleY * tileCount, tileSize, tileSize)
}
const checkAppleColison = () => {
if (AppleX === headX && AppleY === headY) {
AppleX = Math.floor(Math.random() * tileCount)
AppleY = Math.floor(Math.random() * tileCount)
tailLength++
gulpSound.play()
score++
}
}
const keyDown = (event) => {
// Up key
if (event.keyCode == 38) {
if (yVelocity == 1)
return;
yVelocity = -1;
xVelocity = 0
}
// Down Key
if (event.keyCode == 40) {
if (yVelocity == -1)
return;
yVelocity = 1;
xVelocity = 0
}
// Left Key
if (event.keyCode == 37) {
if (xVelocity == 1)
return;
yVelocity = 0;
xVelocity = -1
}
// Right Key
if (event.keyCode == 39) {
if (xVelocity == -1)
return;
yVelocity = 0;
xVelocity = 1
}
}
document.body.addEventListener('keydown', keyDown)
drawGame()