-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicsnake.js
59 lines (51 loc) · 943 Bytes
/
basicsnake.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
let snake;
let rez=20;
let food;
let w;
let h;
function setup()
{
createCanvas(640, 480);
w=floor(width/rez);
h=floor(height/rez);
frameRate(10);
snake=new Snake();
foodLocation();
}
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 foodLocation(){
let x=floor(random(w));
let y=floor(random(h));
food =createVector(x,y);
}
function draw(){
background(0);
scale(rez);
if(snake.checkDie(w,h))
{
background(0,0,255);
fill(255,255,0);
textSize(5);
text('GAME OVER',1,4);
noLoop();
}
if(snake.eatfood(food)){
foodLocation();
}
snake.update();
snake.show();
noStroke();
fill(0,255,0);
rect(food.x,food.y,1,1);
}