-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (163 loc) · 4.2 KB
/
index.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
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("gulp1.mp3");
const negativeSound = new Audio("negative.mp3");
//game loop
function drawGame(){
changeSnakePosition();
let result = isGameOver();
if(result){
return;
}
clearScreen();
checkAppleCollision();
drawApple();
drawSnake();
drawScore();
if(score > 4){
speed = 10;
}else if(score > 9){
speed = 13;
}else if(score > 14){
speed = 15;
}else if(score > 19){
speed = 18;
}
setTimeout(drawGame, 1000/speed);
}
function isGameOver(){
let gameOver = false;
if(xVelocity === 0 && yVelocity === 0){
return false;
}
//walls
if(headX < 0){
gameOver = true;
negativeSound.play();
}
else if(headX === tileCount){
gameOver = true;
negativeSound.play();
}
else if(headY < 0){
gameOver = true;
negativeSound.play();
}
else if(headY === tileCount){
gameOver = true;
negativeSound.play();
}
for(let j = 0; j < snakeParts.length; j++){
let part = snakeParts[j];
if(part.x === headX && part.y === headY){
gameOver = true;
negativeSound.play();
break;
}
}
if(gameOver){
ctx.fillStyle = 'white';
ctx.font = "50px Verdana";
var gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
//fill with gradient
ctx.fillStyle = gradient;
ctx.fillText("Game Over!", canvas.width / 6.5, canvas.height / 2);
}
return gameOver;
}
function drawScore(){
ctx.fillStyle = 'white';
ctx.font = '10px Verdana';
ctx.fillText("Score " + score, canvas.width - 50, 10);
}
function clearScreen(){
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawSnake(){
ctx.fillStyle = 'green';
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));
if(snakeParts.length > tailLength){
snakeParts.shift();
}
ctx.fillStyle = 'orange';
ctx.fillRect(headX * tileCount, headY * tileCount, tileSize, tileSize);
}
function changeSnakePosition(){
headX = headX + xVelocity;
headY = headY + yVelocity;
}
function drawApple(){
ctx.fillStyle = 'red';
ctx.fillRect(appleX * tileCount, appleY * tileCount, tileSize, tileSize);
}
function checkAppleCollision(){
if(appleX === headX && appleY === headY){
appleX = Math.floor(Math.random() * tileCount);
appleY = Math.floor(Math.random() * tileCount);
tailLength++;
score++;
gulpSound.play();
}
}
document.body.addEventListener('keydown', keyDown);
function keyDown(event){
// up arrow
if(event.keyCode == 38){
if(yVelocity == 1){
return;
}
yVelocity = -1;
xVelocity = 0;
}
// down arrow
if(event.keyCode == 40){
if(yVelocity == -1){
return;
}
yVelocity = 1;
xVelocity = 0;
}
// left arrow
if(event.keyCode == 37){
if(xVelocity == 1){
return;
}
yVelocity = 0;
xVelocity = -1;
}
// right arrow
if(event.keyCode == 39){
if(xVelocity == -1){
return;
}
yVelocity = 0;
xVelocity = 1;
}
}
drawGame();