-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (98 loc) · 3.14 KB
/
script.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
const gameCanvas = document.getElementById("gameCanvas");
const ctx = gameCanvas.getContext("2d");
let snake = [ {x:150,y:150},{x:140,y:150},{x:130,y:150},{x:120,y:150},{x:110,y:150},];
let dx = 10; // Horizontal Velocity
let dy = 0; // Vertical Velocity
let foodX = 0;
let foodY = 0;
let score = 0;
let changingDirection = false;
ctx.strokeRect(0, 0, 300, 300);
function drawSnakePart(snakePart){
ctx.fillStyle = 'lightgreen';
ctx.strokestyle = 'darkgreen';
ctx.fillRect(snakePart.x,snakePart.y,10,10);
ctx.strokeRect(snakePart.x,snakePart.y,10,10);
}
function drawSnake(){
snake.forEach(drawSnakePart);
}
function advanceSnake(){
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
const didEatFood = snake[0].x === foodX && snake[0].y === foodY;
if(didEatFood){
score+=10;
document.getElementById('score').innerHTML = score;
createFood();
}else{
snake.pop();
}
}
function clearCanvas(){
ctx.fillStyle = "white";
ctx.strokestyle = "black";
ctx.fillRect(0,0,gameCanvas.clientWidth,gameCanvas.clientHeight);
ctx.strokeRect(0,0,gameCanvas.clientWidth,gameCanvas.clientHeight);
}
function main(){
if(didGameEnd()) return;
setTimeout(function onTick(){
changingDirection = false;
clearCanvas();
drawFood();
advanceSnake();
drawSnake();
main();
},100);
}
function changeDirection(event){
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
const UP_KEY = 38;
const DOWN_KEY = 40;
if(changingDirection) return;
changingDirection = true;
const keyPressed = event.keyCode;
const goingUp = dy === -10;
const goingDown = dy === 10;
const goingLeft = dx === -10;
const goingRight = dx === 10;
if(keyPressed === LEFT_KEY && !goingRight){ dx =-10; dy = 0;}
if(keyPressed === UP_KEY && !goingDown) { dx =0; dy = -10;}
if(keyPressed === RIGHT_KEY && !goingLeft) { dx =10; dy = 0;}
if(keyPressed === DOWN_KEY && !goingUp) { dx =0; dy = 10;}
}
function randomTen(min,max){
return Math.round((Math.random()*(max-min)+min)/10)*10;
}
function createFood(){
foodX = randomTen(0, gameCanvas.clientWidth-10);
foodY = randomTen(0, gameCanvas.clientHeight-10);
snake.forEach(function isFoodOnSnake(part){
const foodIsOnSnake = part.x == foodX && part.y == foodY
if(foodIsOnSnake){
createFood();
}
});
}
function drawFood(){
ctx.fillStyle = 'red';
ctx.strokestyle = 'darkred';
ctx.fillRect(foodX,foodY,10,10);
ctx.strokeRect(foodX,foodY,10,10);
}
function didGameEnd(){
for(let i=4;i<snake.length;i++){
const didCollide = snake[i].x === snake[0].x && snake.y === snake[0].y
if(didCollide) return true;
}
const hitLeftWall = snake[0].x < 0;
const hitRightWall = snake[0].x > gameCanvas.clientWidth - 10;
const hitTopWall = snake[0].y < 0;
const hitBottomWall = snake[0].y > gameCanvas.clientHeight - 10;
return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall
}
createFood();
document.addEventListener("keydown",changeDirection);
main();