-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.js
More file actions
77 lines (69 loc) · 1.56 KB
/
Copy pathfunctions.js
File metadata and controls
77 lines (69 loc) · 1.56 KB
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
//------------------SNAKE MOVEMENTS------------------//
var e;
document.onkeydown = function (event) {
e = event || window.event;
};
function pressL(e){
if(e instanceof KeyboardEvent){
return e.key == "ArrowLeft";
}
}
function pressR(e){
if(e instanceof KeyboardEvent){
return e.key == "ArrowRight";
}
}
function pressUp(e){
if(e instanceof KeyboardEvent){
return e.key == "ArrowUp";
}
}
function pressDown(e){
if(e instanceof KeyboardEvent){
return e.key == "ArrowDown";
}
}
//------------------SNAKE COLLISIONS------------------//
//snake collides with apple
function eatFood(){
return (appleX==headX && appleY==headY)
}
//snake collides with wall
function hitWall(){
let gameOver = false
if(headX<0){
gameOver=true;
}
else if(headX===tileCount){
gameOver=true;
}
else if(headY<0){
gameOver=true;
}
else if(headY===tileCount){
gameOver=true;
}
gameOverScreen(gameOver);
return gameOver;
}
//snake collides with snake
function hitSelf(){
let gameOver = false
for(let i=0; i<snake.length;i++){
let part=snake[i];
if(part.x===headX && part.y===headY){
gameOver=true;
console.log("hit self")
break;
}
}
gameOverScreen(gameOver);
return gameOver;
}
function gameOverScreen(gameOver){
if(gameOver){
ctx.fillStyle="white";
ctx.font="45px Changa one";
ctx.fillText("GAME OVER", canvas.clientWidth/6.5, canvas.clientHeight/2);
}
}