-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
299 lines (286 loc) · 8.1 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//change this to change length of game
var gametime = 20; //this var is used in reset as well
let timer = gametime; //seconds of timer
var interval; //for counting down
var buffId = 1864; //seed id
var startbutton, resetbutton;
var start = false;
var resetKey = false; //play again spacebar
var mobile = false; //display differently for phone
/*Fish Variables*/
var f_x, f_y;
const radius = 65;
var fishCollected = false;
var score = 0; //fish collected
var blobfish;
var letter; //letter attached to fish
new p5()
/*Bubble Variables and Functions*/
var bubbles = 20; //number of bubbles
var elements = []; //bubble elements
// location restriction of target
var x = 200;
var y = 200;
// changes in x, y
var dx = 0;
var dy = 0;
// set the initial properties of each element
function initElement(element, i) {
element.id = i; // id
element.x = random(width); // random x position
element.y = random(height); // random y postion
element.s = random(100); // random size
element.color1 = color(139, 224, 237);
element.color2 = color(211, 249, 255);
element.direction = 0.7;
}
//bubble elements appear on screen at random locations and sizes
function drawElement(element) {
stroke(element.color2);
strokeWeight(1);
fill(element.color1);
ellipse(element.x, element.y, element.s);
fill(255);
}
// update a given element
function updateElement(element) {
element.x = element.x + random(-1, 1);
// Moving up at a constant speed
element.y = element.y - 1;
// Reset to the bottom
if (element.y < 0) {
element.y = height;
}
}
function preload() {
blobfish = createImg('blobfish.gif');
}
function setup() {
randomSeed(buffId)
createCanvas(windowWidth - 100, windowHeight - 100);
if (windowWidth/windowHeight < 0.7){
mobile = true;
}
//start button
startbutton = createButton("Start");
if(mobile){
startbutton.position(width / 2 - 100, 5*height/6);
startbutton.size(300, 150);
startbutton.style("font-size", "70px");
}
else{
startbutton.position(width / 2, 5*height/6);
startbutton.size(100, 50);
startbutton.style("font-size", "25px");
}
startbutton.mousePressed(reset);
startbutton.hide(); //hide for now
//reset button
resetbutton = createButton("Play Again");
if(mobile){
resetbutton.position(width / 2 - 100, 5*height/6);
resetbutton.size(300, 150);
resetbutton.style("font-size", "55px");
}
else{
resetbutton.position(width / 2 -25, 5*height/6);
resetbutton.size(150, 50);
resetbutton.style("font-size", "25px");
}
resetbutton.mousePressed(startScreen);
resetbutton.hide(); //hide for now
frameRate(60);
f_x = random(windowWidth-280);
f_y = random(windowHeight - 280);
letter = '';
createBubbles();
interval = setInterval(decrementTimer, 1000);
}
function reset() {
timer = gametime;
score = 0;
startbutton.hide();
start = true;
newFish();
blobfish.show();
createBubbles();
loop();
}
function draw() {
background(98, 203, 219);
// iterate through bubble array
for (var i = 0; i < elements.length; i++) {
// update the attributes of the i-th element
updateElement(elements[i]);
// call drawElement to draw the i-th element
drawElement(elements[i]);
}
//Timer
fill(150);
textAlign(CENTER);
//Fish collected
fill(255, 255, 255);
if(mobile){
textSize(75)
text('Fish Collected: ' + score, width / 2, height / 20);
}else{
textSize(30);
text('Fish Collected: ' + score, width / 2, height / 13.5);
}
//fish
blobfish.position(f_x-30, f_y-25);
//invisible circle around fish
noStroke();
noFill();
ellipse(f_x, f_y, radius * 2, radius * 2);
fill(255, 255, 255);
textAlign(CENTER, CENTER);
if(mobile){
textStyle(BOLD);
textSize(75);
text('Time: ' + timer, width / 2, height-80);
}else{
textStyle(NORMAL);
textSize(30);
text('Time: ' + timer, width / 2, height / 8);
}
if (timer == 0) {
resetKey = true;
gameOver();
}
if (!start) {
blobfish.hide();
letter = '';
startScreen();
}
if(!mobile){
//letter for fish - putting this here so letter doesn't linger
fill(242, 118, 188);
textSize(40);
textStyle(BOLD);
text(letter, f_x + 80, f_y + 10);
}
}
//game start and instructions
function startScreen() {
start = false;
resetKey = false;
resetbutton.hide();
fill(50, 123, 163);
//stroke(28, 31, 51);
if(mobile){
rect( 0 ,0, width, height);
fill(255, 255, 255);
textSize(95);
text('Blobby Pop!', width / 2, height / 9);
textSize(75);
text('Instructions:', width / 2, height / 9 + 200);
text('Collect blobfish', width / 2, height / 9 + 275);
text('by tapping on them', width / 2, height / 9 + 350);
text('before the time', width / 2, height / 9 + 425);
text('runs out!', width / 2, height / 9 + 500);
}
else{
rect( windowWidth/4-50, 0, windowWidth/2, windowHeight-100, 20);
fill(255, 255, 255);
textSize(40);
text('Blobby Pop!', width / 2, height / 9);
textSize(35);
text('Instructions:', width / 2, height / 9 + 50);
text('Collect blobfish before', width / 2, height / 9 + 85);
text('the time runs out!', width / 2, height / 9 + 115);
text('To collect blobfish:', width / 2, height / 9 + 180);
text('Click or press', width / 2, height / 9 + 215);
text('corresponding key', width / 2, height / 9 + 250);
}
startbutton.show();
}
//game over and restart
function gameOver() {
fill(50, 123, 163);
if(mobile){
rect( 0 ,0, width, height);
fill(255, 255, 255);
textSize(75);
text("GAME OVER", width / 2, height/3);
textSize(75);
textAlign(CENTER);
text('Fish Collected: ' + score, width / 2, height/2);
}
else{
rect( windowWidth/4-50, 0, windowWidth/2, windowHeight-100);
fill(255, 255, 255);
textSize(30);
text("GAME OVER", width / 2, height/3);
textSize(30);
textAlign(CENTER);
text('Fish Collected: ' + score, width / 2, height/2);
}
blobfish.hide();
letter = '';
resetbutton.show();
noLoop();
}
//decrement timer every second
function decrementTimer() {
if (timer > 0) {
timer--;
}
}
function createBubbles() {
for (var i = 0; i < bubbles; i++) {
// create a new element as a blank javascript object
var newElement = {};
// call to initial the element
initElement(newElement, i);
// add the element
elements.push(newElement);
}
}
function update() {
if (pause) {
dx = 0;
dy = 0;
}
else {
x = x + dx;
y = y + dy;
}
}
function newFish() {
//new letter and location
letter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
f_x = random(windowWidth - 280);
f_y = random(windowHeight - 280);
}
//when the user clicks
function mousePressed() {
var d = dist(mouseX, mouseY, f_x, f_y);
if (d < radius) {
newFish();
score++;
}
}
function keyTyped() {
//add more bubbles
if (key === ',') {
bubbles ++;
createBubbles();
}
if (key.toLowerCase() === letter.toLowerCase()) {
newFish();
score++;
}
//spacebar option to start/play again instead of clicking button
if (key === ' '){
if(!start){
reset();
}
if(resetKey){
startScreen();
}
}
}
function windowResized() {
resizeCanvas(windowWidth - 100, windowHeight - 100);
}