-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
66 lines (51 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
58
59
60
61
62
63
64
65
66
let snake;
let food;
let rez = 10;
let w, h;
let endGame;
function preload(){
endGame = loadImage("Assets/game over.jpg")
}
function setup() {
createCanvas(450, 375);
frameRate(15)
snake = new Snake();
w = floor(width/rez);
h = floor(height/rez);
createFood();
}
function createFood(){
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
}
function draw() {
scale(rez)
background("black");
snake.show();
snake.move();
if(snake.eat(food) ){
createFood();
snake.addBody();
}
if(snake.endGame() ){
console.log("Game is Over");
background(endGame, 200, 100, 200, 100);
noLoop();
}
noStroke();
fill("red")
rect(food.x, food.y, 1, 1)
}
function keyPressed(){
if(keyCode == LEFT_ARROW)
snake.changeDir(-1, 0)
else if(keyCode == UP_ARROW)
snake.changeDir(0, -1)
else if(keyCode == RIGHT_ARROW)
snake.changeDir(1, 0)
else if(keyCode == DOWN_ARROW)
snake.changeDir(0, 1)
else if(keyCode == "80")
snake.changeDir(0, 0)
}