-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
193 lines (142 loc) · 4.97 KB
/
snake.html
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
//snake game - implement logic
// trascribed and editted from https://www.youtube.com/watch?v=PA57h3mFHik original file is here: https://goo.gl/KUsCZD
var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");
var cvsW = cvs.width;
var cvsH = cvs.height;
var snakeW = 10;
var snakeH = 10;
var gameSpeed = 70; //eGan this is the initial speed
var score = 0;
var direction = "right";//default direction
var timer = 100; //
//listen for players input
document.addEventListener("keydown", getDirection);
function getDirection(e) {
if (e.keyCode == 37 && direction != "right") {
direction = 'left';
} else if (e.keyCode == 38 && direction != "down") {
direction = 'up';
} else if (e.keyCode == 39 && direction != "left") {
direction = 'right';
} else if (e.keyCode == 40 && direction != "up") {
direction = 'down';
}
}
function drawSnake(x, y) {
ctx.fillStyle = "white";
ctx.fillRect(x * snakeW, y * snakeH, snakeW, snakeH);
ctx.strokeStyle = "black";
ctx.strokeRect(x * snakeW, y * snakeH, snakeW, snakeH);
}
//create a snake object with 4 cells
var len = 4;
var snake = [];
for (var i = (len - 1); i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
//create food
var food = {
x: Math.round(Math.random() * ((cvsW / snakeW) - 1)),
y: 1 + Math.round(Math.random() * ((cvsH / snakeH) - 2))//1+ and -2 used to keep initial food off y=0;
}
//draw food function
function drawFood(x, y) {
ctx.fillStyle = "red";
ctx.fillRect(x * snakeW, y * snakeH, snakeW, snakeH);
ctx.strokeStyle = "black";
ctx.strokeRect(x * snakeW, y * snakeH, snakeW, snakeH);
}
//check for self collision
function checkCollision(x, y, array) {
for (var i = 0; i < array.length; i++) {
if (x == array[i].x && y == array[i].y) {
return true;
}
} return false;
}
function drawScore(x) {
ctx.fillStyle = "orange";
ctx.font = "19px Impact";
ctx.fillText("score: " + Math.round(x), 10, cvsH - 5);
}
function playMoveSound() {
var soundWalk = new Audio("walk.wav");
soundWalk.play()
};
function playEatSound() {
var soundEat = new Audio("eat.wav");
soundEat.play()
};
function playEndSound() {
var soundEnd= new Audio("end.wav");
soundEnd.play()
};
playMoveSound();
setInterval(playMoveSound, 1300);
function draw() {
ctx.clearRect(0, 0, cvsW, cvsH);
timerFunction();
for (var i = 0; i < snake.length; i++) {
var x = snake[i].x;
var y = snake[i].y;
drawSnake(x, y);
}
//draw food
drawFood(food.x, food.y);
//snake head
var snakeX = snake[0].x;
var snakeY = snake[0].y;
//create a new head, based on previous head and direction
if (direction == "left") snakeX--;
else if (direction == "up") snakeY--;
else if (direction == "right") snakeX++;
else if (direction == "down") snakeY++;
//if hits wall is game over
if (snakeX < 0 || snakeY < 0 || snakeX >= cvsW / snakeW || snakeY >= cvsH / snakeH || checkCollision(snakeX, snakeY, snake)) {
location.reload();
}
//if snake eats food
if (snakeX == food.x && snakeY == food.y) {
food = {
x: Math.round(Math.random() * ((cvsW / snakeW) - 1)),
y: Math.round(Math.random() * ((cvsH) / snakeH) - (30 * (snakeH)))
}
//eGan check that new food is not on snake
for (i = 0; i < snake.length; i++) {
if (x == snake[i].x || y == snake[i].y) {
food = {
x: Math.round(Math.random() * ((cvsW / snakeW) - 1)),
y: Math.round(Math.random() * ((cvsH / snakeH) - 1))
}
}
}
var newHead = {
x: snakeX,
y: snakeY
};
playEatSound();
score += timer;
timer = 100
// score++;
//eGan added accelration each time snake eats
// clearInterval(game);
// gameSpeed-=10;
// game=setInterval(draw, gameSpeed);
} else {
snake.pop();
var newHead = {
x: snakeX,
y: snakeY
};
}
snake.unshift(newHead);
drawScore(score);
}
function timerFunction() {
ctx.fillStyle = "orange";
ctx.font = "19px impact ";
ctx.fillText("timer: " + Math.round(timer), 150, cvsH - 5);
timer -= .1;
}
var game = setInterval(draw, gameSpeed);