-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.js
119 lines (103 loc) · 2.76 KB
/
food.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
// eaten by snake
var resetAlert=function(){
alertCond=false;
}
function Food(){
// food color code
var pink = [249, 116, 109]; // increase speed
var blue = [109, 224, 249]; //decrease speed
var green = [109, 249, 133]; //make more food
var purple = [163, 109, 249]; //shrink snake
var yellow = [240, 249, 109]; //elongate snake
// pick random color
var arrColors = [pink, blue, green, purple, yellow];
var ranCol = arrColors[Math.floor(Math.random() * arrColors.length)];
// start at random color, random position
this.color = ranCol;
this.x = Math.floor(Math.random()*(gameSize/snakeSize))*snakeSize;
this.y = Math.floor(Math.random()*(gameSize/snakeSize))*snakeSize;
// display food
this.show = function() {
fill(ranCol[0], ranCol[1], ranCol[2]);
rect(this.x, this.y, snakeSize, snakeSize);
}
// food is eaten
this.delete = function(){
for(i = 0; i < foods.length; i++){
if(this.x === foods[i].x && this.y === foods[i].y){
foods[i] == null;
for( j = i; j < foods.length-1; j++){
foods[j] = foods[j+1];
}
foods.pop();
}
}
}
// eaten 5 consecutive same colored food
this.hasFive = function(){
switch(this.color){
// speed up
case pink:
console.log('Pink Power Up: increase speed');
var timesQuick = 0;
alertMessage='Pink Power Up: increase speed';
alertCond=true;
var quick = setInterval(function(){
timesQuick++;
if(timesQuick === 3000){
clearInterval(quick);
frameRate(10);
return;
}
frameRate(30);
},0);
break;
// slow down
case blue:
console.log('Blue Power Up: decrease speed');
var timesSlow = 0;
alertMessage='Blue Power Up: decrease speed';
alertCond=true;
var quick = setInterval(function(){
timesSlow++;
if(timesSlow === 3000){
clearInterval(quick);
frameRate(10);
return;
}
frameRate(5);
},0);
break;
// more food
case green:
console.log('Green Power Up: Makes more food');
for(i = 0; i < 6; i++){
foods.push(new Food());
}
alertMessage='Green Power Up: Makes more food';
alertCond=true;
break;
// shrink snake
case purple:
console.log('Purple Power Up: Shrinks snake');
var sLength=snake.tail.length;
if(sLength > 4){
}
var ratio = Math.floor(sLength * 0.7);
snake.tail.splice(sLength-ratio, ratio);
alertMessage='Purple Power Up: Shrinks snake';
alertCond=true;
break;
// elongate snake
case yellow:
console.log('Yellow Power Up: Elongates snake');
for(i = 0; i < 5; i++){
snake.grow();
alertMessage='Yellow Power Up: Elongates snake';
alertCond=true;
}
snake.points =- 5;
break;
}
}
}