-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.js
More file actions
146 lines (111 loc) · 2.57 KB
/
Copy pathGame.js
File metadata and controls
146 lines (111 loc) · 2.57 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
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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const box = 25;
const canvasSize = 23;
//score variable
let score = 0;
//load snake starting position
let snake = [];
snake[0] =
{
x: Math.floor((canvasSize/2)) * box,
y: Math.floor((canvasSize/2)) * box
};
//set direction getting pressed by arrow keys
let dir;
var pressed = false;
document.addEventListener('keydown', direction);
function direction(event){
if(event.keyCode == 37 && dir != 'RIGHT'){
dir = "LEFT";
}
else if(event.keyCode == 38 && dir != 'DOWN'){
dir = "UP";
}
else if(event.keyCode == 39 && dir != 'LEFT'){
dir = "RIGHT";
}
else if(event.keyCode == 40 && dir != 'UP'){
dir = "DOWN";
}
}
// set the location of our food
let food = {
x: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box,
y: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box
}
//draw function
function draw(){
//draw the background
ctx.fillStyle = 'lightgreen';
ctx.fillRect(box, box, canvasSize*box - box, canvasSize*box-box);
//draw the snake head and tail
for(let i = 0; i < snake.length; i++)
{
ctx.fillStyle = 'green';
ctx.fillRect(snake[i].x, snake[i].y, box, box);
if(snake[i].x == food.x && snake[i].y == food.y)
{
food = {
x: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box,
y: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box
}
}
}
//move the snake head
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if(dir == "LEFT")
snakeX -= box;
if(dir == "RIGHT")
snakeX += box;
if(dir == "UP")
snakeY -= box;
if(dir == "DOWN")
snakeY += box;
// if the snake eats the food
if(snakeX == food.x && snakeY == food.y)
{
score+=1;
food = {
x: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box,
y: Math.floor(1 + (Math.random() * (canvasSize - 1))) * box
}
}
else
{
snake.pop();
}
let newHead = {
x: snakeX,
y: snakeY
};
//check collision
function collision(head, array){
for(let i = 0; i < array.length; i++)
{
if(head.x == array[i].x && head.y == array[i].y)
{
return true;
}
}
return false;
}
//game over
if(snakeX < box || snakeY < box ||
snakeX > ((canvasSize - 1) * box)|| snakeY > ((canvasSize - 1) * box) ||
collision(newHead,snake))
{
clearInterval(game);
}
snake.unshift(newHead);
//draw in food
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, box, box);
//draw score
ctx.fillStyle = 'white';
ctx.font = '24px Changa one';
ctx.clearRect(0, 0, 50, 25);
ctx.fillText(score, box, 0.8 * box);
}
let game = setInterval(draw, 100);