-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
57 lines (47 loc) · 1.04 KB
/
sketch.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
let snake;
let rez = 20;
let food;
let w;
let h;
function setup() {
createCanvas(400, 400);
w = floor(width/rez);
h = floor(height/rez);
frameRate(5); // to reduce the animation
snake = new Snake();
foodLocation();
}
function foodLocation(){
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x,y);
}
function keyPressed(){
if(keyCode == LEFT_ARROW){
snake.setDir(-1,0)
}
else if(keyCode == RIGHT_ARROW){
snake.setDir(1,0)
}else if(keyCode == DOWN_ARROW){
snake.setDir(0,1)
}else if(keyCode == UP_ARROW){
snake.setDir(0,-1)
}
}
function draw() {
scale(rez);
background(220);
if(snake.eat(food)){
foodLocation();
}
snake.update();
snake.show();
if(snake.endGame()){
print("GAME ENDED")
background(255,0,0);
noLoop();
}
noStroke(); // Disables drawing the stroke (outline)
fill(255,0,0);
rect(food.x,food.y,1,1);
}