-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.js
48 lines (39 loc) · 833 Bytes
/
snake_game.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
let snake;
const scale = 20;
let food;
function setup() {
createCanvas(600, 600);
frameRate(10);
snake = new Snake(200, 200);
pickLocation();
}
function pickLocation() {
const cols = floor(width / scale);
const rows = floor(height / scale);
food = createVector(floor(random(cols)), floor(random(rows)));
food.mult(scale);
}
function draw() {
background(0);
if (snake.eat(food)) {
pickLocation();
}
snake.death();
snake.update();
snake.show();
rect(food.x, food.y, scale, scale);
}
function mousePressed() {
snake.total++;
}
function keyPressed() {
if (keyCode === UP_ARROW) {
snake.dir(0, -1);
} else if (keyCode === DOWN_ARROW) {
snake.dir(0, 1);
} else if (keyCode === LEFT_ARROW) {
snake.dir(-1, 0);
} else if (keyCode === RIGHT_ARROW) {
snake.dir(1, 0);
}
}