-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
84 lines (84 loc) · 2.06 KB
/
sketch.js
File metadata and controls
84 lines (84 loc) · 2.06 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
let w;
let h;
let rez=20;
let food;
let snake;
let started=false;
let firstPlay=true;
let muted=false;
let score;
let lose;
function preload() {
score=loadSound('sounds/score.mp3');
lose=loadSound('sounds/lose.mp3');
}
function setup() {
let canvas=createCanvas(800, 800);
canvas.parent('game');
document.getElementById('score').innerHTML=1;
document.getElementById('display').innerHTML='';
frameRate(10);
w=floor(width/rez);
h=floor(height/rez);
snake=new Snake();
randomizeFood();
}
function draw() {
background(60, 60, 100);
if(snake.eat(food)) {
if(!muted) score.play();
document.getElementById('score').innerHTML++;
randomizeFood();
} else if(snake.gameOver()) {
if(!muted) lose.play();
document.getElementById('display').innerHTML='Game Over!';
if(firstPlay) {
document.getElementById('display').innerHTML+='<br><span style="font-size: 50%;">(Press R/Space to restart!)</span>';
firstPlay=false;
}
noLoop();
}
snake.update();
fill(255, 0, 0);
rect(food.x, food.y, rez, rez);
}
function randomizeFood() {
while(true) {
let toBreak=true;
food=createVector(floor(random(w)), floor(random(h))).mult(rez);
for(let i=0;i<snake.body.length;i++) {
if(food.equals(snake.body[i])) {
toBreak=false;
break;
}
}
if(toBreak) break;
}
}
function keyPressed() {
switch(keyCode) {
case LEFT_ARROW:
case 65:
if(snake.xdir!=1) snake.setDir(-1, 0);
break;
case UP_ARROW:
case 87:
if(snake.ydir!=1) snake.setDir(0, -1);
break;
case RIGHT_ARROW:
case 68:
if(snake.xdir!=-1) snake.setDir(1, 0);
break;
case DOWN_ARROW:
case 83:
if(snake.ydir!=-1) snake.setDir(0, 1);
break;
case 32:
case 82:
setup();
loop();
break;
case 77:
muted=!muted;
}
}