-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
53 lines (48 loc) · 873 Bytes
/
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
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(10);
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===UP_ARROW) {
snake.setDir(0,-1);
}else if (keyCode===DOWN_ARROW) {
snake.setDir(0,1);
}
}
function draw() {
scale(rez);
background(70);
textSize(1);
text('Score:',14,2);
text(snake.returnLength(),17,2);
rect(food.x,food.y,1,1);
if(snake.eat(food)){
foodLocation();
rect(food.x,food.y,1,1);
}
snake.update();
snake.show();
if(snake.endGame()){
noLoop();
noStroke();
fill(0,255,0);
}
}