-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.JS
172 lines (161 loc) · 4.33 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
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 xVelocity=0;
let yVelocity=0;
let appleX=5;
let appleY=5;
let score = 0;
const Eatsound = new Audio ("Eat.wav");
const GameOversound = new Audio ("GameOver.wav");
//game loop
function drawGame()
{
changeSnakeposition();
let result = isGameOver();
if(result){
return;
}
clearScreen();
checkAppleCollision();
drawSnake();
drawApple();
drawScore();
if(score > 2){
speed = 11;
}
if(score >5){
speed = 15
}
setTimeout(drawGame, 900/ speed);
}
function isGameOver(){
let gameOver = false;
if(yVelocity ===0 && xVelocity ===0){
return false;
}
//walls
if(headX < 0){
gameOver = true
}
else if(headX === tileCount){
gameOver = true
}
else if(headY <0){
gameOver = true
}
else if(headY === tileCount){
gameOver = true
}
for(let i=0; i < snakeParts.length; i++){
let parts = snakeParts[i];
if(parts.x === headX && parts.y === headY){
gameOver = true
break;
}
}
if(gameOver){
ctx.fillStyle = "white";
ctx.font = "50px Verdana";
//var gradiant = ctx.createLinearGradient (0, 0, canvas.width,0);
// gradiant.addColorStop("0","magenta");
//gradiant.addColorStop("0.5","blue");
// gradiant.addColorStop("1.0","red");
// Fill with gradient
// ctx.fillStyle = gradiant;
ctx.fillText("Game Over!", canvas.width / 6.5, canvas.height /2);
GameOversound.play();
}
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)); // put an item at the end of the list next to the head
while (snakeParts.length > tailLength){
snakeParts.shift(); // remove the furthers item from the snake parts if have more than our tail size.
}
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++;
Eatsound.play();
}
}
document.body.addEventListener('keydown', keyDown);
function keyDown(event)
{
//up
if (event.keyCode == 38) {
if(yVelocity == 1)
return;
yVelocity = -1;
xVelocity = 0;
}
//down
if (event.keyCode == 40) {
if(yVelocity == -1)
return;
yVelocity = 1;
xVelocity = 0;
}
//left
if (event.keyCode == 37) {
if(yVelocity == 1)
return;
yVelocity = 0;
xVelocity = -1;
}
//right
if (event.keyCode == 39) {
if(yVelocity == -1)
return;
yVelocity = 0;
xVelocity = 1;
}
}
drawGame ();