-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (55 loc) · 1.1 KB
/
Copy pathapp.js
File metadata and controls
65 lines (55 loc) · 1.1 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
let snake;
let scl = 10;
let dadu;
let w;
let h;
let Score = 0;
function setup() {
createCanvas(400, 400);
w = floor(width / scl);
h = floor(height / scl);
frameRate(10)
snake = new Snake();
daduLocation();
}
function daduLocation(){
let x = floor(random(w));
let y = floor(random(h));
dadu = createVector(x, y);
}
function keyPressed() {
if (keyCode === UP_ARROW) {
snake.setDir(0,-1);
} else if (keyCode === DOWN_ARROW) {
snake.setDir(0,1);
} else if (keyCode === LEFT_ARROW) {
snake.setDir(-1,0);
} else if (keyCode === RIGHT_ARROW) {
snake.setDir(1,0);
}else if (keyCode === ' ') {
snake.grow();
}
}
function draw() {
scale(scl);
background(0);
fill(255)
textSize(1.8)
text('Score : ' + Score, 0.5, 2)
if(snake.endGame()){
background(255,0,0);
fill(255)
textSize(5)
text("Game END!", 5, 20)
noLoop();
}
if(snake.eat(dadu)){
daduLocation();
Score += 1;
}
snake.update();
snake.show();
fill(255,0,0);
noStroke();
rect(dadu.x,dadu.y,1,1);
}