-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
133 lines (113 loc) · 2.39 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// score displayed in message
var scoreMessage;
var alertMessage;
// game parameters
var endGame = false;
var alertCond=false;
var run = true;
var pause = false;
var gameSize = 600;
// snake parameters
var snake;
var snakeSize = 20;
var foods = [];
// initial setup
function setup(){
// highscore display
if (localStorage.getItem("highscore") === null){
localStorage.setItem("highscore", "0");
}
// create game board
createCanvas(gameSize, gameSize);
frameRate(10);
// create snake
snake = new Snake();
score = new Score();
foods=[];
skins=[];
// start with 5 foods
for(i = 0; i < 5; i++){
foods.push(new Food());
}
}
//display the functions of the powerups
// display game board and elements
function draw(){
// background color
background(30);
if (!endGame){
if (pause){
fill(200);
textSize(50);
text("PAUSED", 200, 300);
}
if(alertCond){
fill(200);
textSize(30);
text(alertMessage, 50, 250);
setTimeout(resetAlert,1200);
}
snake.update(run);
snake.show();
for(i = 0; i < foods.length; i++){
foods[i].show();
snake.eat(foods[i],i);
}
for(i=0;i<skins.length;i++){
skins[i].show();
snake.touchSkin(skins[i]);
}
fill(255);
textSize(14);
text("Score: " + score.getScore(), Math.floor(gameSize*0.85), Math.floor(gameSize*0.93));
}else{
background(20);
fill(240);
textSize(26);
text(scoreMessage, 215, 300);
}
}
function keyPressed() {
var PAUSE = 80;
var RESTART = 82;
if (endGame){
score.score = 0;
}
endGame = false;
if (keyCode === UP_ARROW) {
snake.changeDir(0, -1);
}
else if (keyCode === DOWN_ARROW) {
snake.changeDir(0, 1);
}
else if (keyCode === RIGHT_ARROW) {
snake.changeDir(1, 0);
}
else if (keyCode === LEFT_ARROW) {
snake.changeDir(-1, 0);
}
else if (keyCode === PAUSE) {
var resume_xspeed;
var resume_yspeed;
if(snake.xspeed != 0 || snake.yspeed != 0){
console.log('Pause');
run = false;
pause = true;
resume_xspeed = snake.xspeed;
resume_yspeed = snake.yspeed;
snake.xspeed = 0;
snake.yspeed = 0;
}
else {
console.log('Resume');
run = true;
pause = false;
snake.xspeed = resume_xspeed;
snake.yspeed = resume_yspeed;
}
}
else if (keyCode === RESTART) {
console.log('Restart');
snake = new Snake();
}
}