-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
70 lines (63 loc) · 1.43 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
67
68
69
70
let TS = 25;
let snake = undefined;
let GAME_VELOCITY = undefined;
let food = undefined;
let gameScore = 0;
let scoreBoard = undefined;
function setup() {
createCanvas(500, 500);
GAME_VELOCITY = 5;
snake = new Snake();
scoreBoard = createP("SCORE = " + gameScore);
let refreshButton = createButton('Reset Game');
refreshButton.mousePressed(refresh);
}
function draw() {
frameRate(GAME_VELOCITY);
background(51);
snake.update();
if (snake.dead == 1) {
textSize(64);
textAlign(CENTER);
textStyle(BOLD);
fill(255, 0, 0);
text("YOU DIED!", width / 2, height / 2);
frameRate(0);
}
snake.show();
plant_food();
if (food.x === snake.pos_x && food.y === snake.pos_y) {
snake.eat();
food = undefined;
GAME_VELOCITY++;
gameScore++;
scoreBoard.html("SCORE = " + gameScore);
}
}
function plant_food() {
if (food === undefined) {
food = createVector(floor(random(floor(width / TS))), floor(random(floor(height / TS))));
}
stroke(51);
fill(255, 0, 0);
rect(food.x * TS, food.y * TS, TS, TS);
}
function keyPressed() {
switch (keyCode) {
case UP_ARROW:
snake.changeDirection("UP");
break;
case DOWN_ARROW:
snake.changeDirection("DOWN");
break;
case LEFT_ARROW:
snake.changeDirection("LEFT");
break;
case RIGHT_ARROW:
snake.changeDirection("RIGHT");
break;
}
}
function refresh() {
document.location.reload(false);
}