-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnake.js
More file actions
47 lines (41 loc) · 801 Bytes
/
snake.js
File metadata and controls
47 lines (41 loc) · 801 Bytes
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
let bg;
let s;
let food;
function setup() {
s = new Snakegame();
createCanvas(1352,655);//height,width
bg = loadImage('Background.png');
frameRate(10);
pickLocation();
}
function pickLocation(){
let cols = floor(width/20);
let rows = floor(height/20);
food = createVector(floor(random(cols)), floor(random(rows)));
food.mult(20);
}
function keyPressed(){
if(keyCode === UP_ARROW){
s.dir(0,-1);
}
if(keyCode === DOWN_ARROW){
s.dir(0,1);
}
if(keyCode === LEFT_ARROW){
s.dir(-1,0);
}
if(keyCode === RIGHT_ARROW){
s.dir(1,0);
}
}
function draw() {
background(bg);
if(s.eats(food)){
pickLocation();
}
s.death();
s.update();
s.show();
fill(0,0,255);
rect(food.x,food.y,20,20);
}