-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
425 lines (373 loc) · 10.3 KB
/
script.js
File metadata and controls
425 lines (373 loc) · 10.3 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
let minInput, hrInput, alarmMin, alarmHr, alarmDisplay, alarmSound1, ampmButton, alarmAMPM, alarmScene, froggerScene, alarmWin, alarmRun, songButton, currentSong;
let executeAlarmOnce;
let initializeOnce;
let isAMPM = "AM";
let frogX, frogY, score, lives, gameIsOver, car1X, car1Y, car1V, safeY, globalS, globalB, move, car2X, car2Y, car2V, car3X, car3Y, car3V;
function preload() {
console.log("Entering Preload")
soundFormats('mp3');
alarmSound1 = loadSound("On_Fire.mp3");
alarmSound2 = loadSound("Grass_Skirt_Chase");
alarmSound3 = loadSound("calmAlarm.mp3");
alarmSound4 = loadSound("alarm_rooster-hug.mp3");
alarmSound5 = loadSound("Progressive.mp3");
alarmSound6 = loadSound("still_with_you_by_jk.mp3")
console.log("Exiting Preload")
}
function setup(){
createCanvas(600, 400);
colorMode(HSB, 360, 100, 100);
backgroundColor = 0;
textAlign(CENTER);
//Creates the Hour input button
hrInput = createInput('');
hrInput.size(40, 20);
hrInput.position(210, 380);
//Creates the Minute input button
minInput = createInput('');
minInput.size(40, 20);
minInput.position(270, 380);
//Create the button to select AM or PM
ampmButton = createSelect();
ampmButton.position(330, 380);
ampmButton.option("AM");
ampmButton.option("PM");
//Create the button to choose song
songButton = createSelect();
songButton.position(400, 380);
songButton.option("Grass Skirt Chase")
songButton.option("On Fire");
songButton.option("Calming Guitar");
songButton.option("Rooster");
songButton.option("Build Up");
songButton.option("Still With You");
alarmDisplay = false;
executeAlarmOnce = false; //-Zach
initializeOnce = false;
alarmWin = false;
alarmRun = false;
alarmScene = true;
gameIsOver = false;
currentSong = ("On Fire");
// Frogger setup
globalS = 80;
globalB = 80;
frogX = random(width);
frogY = height + 30;
score = 0;
highScore = 0;
lives = 5;
gameIsOver = false;
//Car 1
car1X = 0;
car1Y = 100;
//car1V = 6;
car1V = 6;
//Car 2
car2X = width
car2Y = 300;
// car2V = 4;
car2V = 4;
//Car 3
car3X = 0;
car3Y = 200;
//car3V = 6;
car3V = 6;
safeY = height;
move = 20;
}
function draw(){
if (alarmScene == true){
background(backgroundColor);
alarm();
alarmCheck();
} else if (froggerScene == true){
background(360);
// Code for gold goal line
fill(60, globalS, globalB);
rect(0, 0, width, 50);
//Code for frog Safe Zone
fill(212, globalS, globalB);
rect(0, safeY, width, height);
// Code to display Frog
fill(120, globalS, globalB);
ellipse(frogX, frogY, 20);
moveCars();
drawCars();
checkCollisions();
checkBoundaries();
checkWin();
displayScores();
}
}
function keyPressed(){
if (keyCode == ENTER && alarmScene == true){
alarmDisplay = true;
alarmMin = int(minInput.value());
minInput.value('');
if ((alarmMin < 10)){
alarmMin = "0" + alarmMin
}
alarmHr = int(hrInput.value());
hrInput.value('');
alarmAMPM = ampmButton.value();
if ((alarmAMPM == "PM") && (alarmHr != 12)){
alarmHr = alarmHr + 12;
}
if ((alarmHr == 12) && (alarmAMPM == "AM")){
alarmHr = alarmHr - 12;
}
console.log('Alarm set to ' + alarmHr + ':' + alarmMin + " " + alarmAMPM);
}
if (keyCode === UP_ARROW) {
frogY -= move;
}
if (keyCode === DOWN_ARROW){
frogY += move;
}
if (keyCode === RIGHT_ARROW){
frogX += move;
}
if (keyCode === LEFT_ARROW){
frogX -= move;
}
}
function keyTyped(){
if ((key === ' ') && (gameIsOver == true)){
froggerScene = false;
alarmScene = true;
alarmWin = true;
gameIsOver = false;
executeAlarmOnce = false;
resizeCanvas(600, 400);
//Creates the Hour input button
hrInput = createInput('');
hrInput.size(40, 20);
hrInput.position(210, 380);
alarmHr = (" ");
//Creates the Minute input button
minInput = createInput('');
minInput.size(40, 20);
minInput.position(270, 380);
alarmMin = (" ");
//Create the button to select AM or PM
ampmButton = createSelect();
ampmButton.position(330, 380);
ampmButton.option("AM");
ampmButton.option("PM");
//Create the button to choose song
songButton = createSelect();
songButton.position(400, 380);
songButton.option("Grass Skirt Chase")
songButton.option("On Fire");
songButton.option("Calming Guitar");
songButton.option("Rooster");
songButton.option("Build Up");
songButton.option("Still With You")
alarmDisplay = false;
score = 0;
console.log("Game off");
}
}
function alarmCheck(){
if ((minute() == alarmMin) && (hour() == alarmHr) && (alarmAMPM == isAMPM) && (executeAlarmOnce == false)) { //-Zach
console.log("Alarm")
alarmRun = true;
if (songButton.value() == ("On Fire")){
currentSong = alarmSound1;
} else if (songButton.value() == ("Grass Skirt Chase")){
currentSong = alarmSound2;
} else if (songButton.value() == ("Calming Guitar")){
currentSong = alarmSound3;
} else if (songButton.value() == ("Rooster")){
currentSong = alarmSound4;
} else if (songButton.value() == ("Build Up")){
currentSong = alarmSound5;
}else if (songButton.value() == ("Still With You")){
currentSong = alarmSound6;
}
currentSong.loop(0, 1, .2);
executeAlarmOnce = true; //-Zach
changeScene();
resizeCanvas(500, 500);
}
}
function alarm(){
let hr = hour();
if (hr > 11){
isAMPM = "PM";
} else {
isAMPM = "AM"
}
if (hr > 12){
hr = hr - 12;
}
if (hr < 10 && hr > 0){
hr = "0"+hr;
}
if (hr == 0){
hr = hr + 12
}
let min = minute();
if (min < 10 && min >= 0){
min = "0"+min;
}
let sec = second();
if (sec < 10 && sec >= 0){
sec = "0"+sec;
}
fill(0, 0, 100);
textSize(60);
text(hr + ":" + min + ":" + sec, 260, 200);
text(isAMPM, 450, 200);
textSize(17);
text("Hour", 225, 365);
text("Min", 285, 365);
textSize(25);
text(":", 255, 390);
textSize(20);
if ((alarmDisplay == true) && (alarmHr == 0) && (alarmAMPM == "AM")){
text('Alarm time ' + (alarmHr + 12) + ':' + alarmMin + " " + alarmAMPM, 300, 20);
} else if (alarmDisplay == true && alarmHr < 12){
text('Alarm time ' + alarmHr + ':' + alarmMin + " " + alarmAMPM, 300, 20);
} else if ((alarmDisplay == true) && (alarmHr > 12) && (alarmAMPM == "PM")) {
text('Alarm time ' + (alarmHr - 12) + ':' + alarmMin + " " + alarmAMPM, 300, 20);
} else if ((alarmDisplay == true) && (alarmHr == 12) && (alarmAMPM == "PM")){
text('Alarm time ' + (alarmHr) + ':' + alarmMin + " " + alarmAMPM, 300, 20);
}
else {
text('Hit enter to confirm your Alarm', 290, 20);
}
}
function changeScene(){
alarmScene = false;
removeElements();
alarmScene = false;
froggerScene = true;
removeElements();
}
function initialize(){
if ((froggerScene == true) && (initializeOnce == false) && (alarmWin == true)){
console.log("begin");
//Creates the Hour input button
hrInput = createInput('');
hrInput.size(40, 20);
hrInput.position(210, 380);
//Creates the Minute input button
minInput = createInput('');
minInput.size(40, 20);
minInput.position(270, 380);
//Create the button to select AM or PM
ampmButton = createSelect();
ampmButton.position(330, 380);
ampmButton.option("AM");
ampmButton.option("PM");
alarmDisplay = false;
executeAlarmOnce = false;
}
}
// FROGGER CODE
//////
//////
//////
//////
function moveCars() {
// Move the car
car1X += car1V;
car2X -= car2V;
car3X += car3V;
// Reset if it moves off screen
if (car1X > width){
car1X = 0;
car1Y = random(50, 150);
}
if (car2X < -60){
car2X = width;
car2Y = random(330, 370);
}
if (car3X > width){
car3X = 0;
car3Y = random(180, 300);
}
}
function drawCars() {
// Code for car 1
fill(0, globalS, globalB);
rect(car1X, car1Y, 40, 30);
// Code for car 2
fill(30, 10, 30);
rect(car2X, car2Y, 60, 30);
// Code for car 3
fill(250, globalS, globalB);
rect(car3X, car3Y, 30, 30);
// Code for additional cars
}
function checkCollisions() {
// If the frog collides with the car, reset the frog and subtract a life.
hit = collideRectCircle(car1X, car1Y, 40, 30, frogX, frogY, 20);
hit2 = collideRectCircle(car2X, car2Y, 60, 30, frogX, frogY, 20);
hit3 = collideRectCircle(car3X, car3Y, 30, 30, frogX, frogY, 20);
if (hit == true && !gameIsOver){
lives -= 1;
frogY = safeY + 40;
frogX = random(200, 300);
console.log("hit!");
}
if (hit2 == true && !gameIsOver){
lives -= 2;
frogY = safeY + 40;
frogX = random(200, 300);
console.log("hit!");
}
if (hit3 == true && !gameIsOver){
lives -= 1;
frogY = safeY + 40;
frogX = random(200, 300);
console.log("hit!");
}
}
function checkWin() {
// If the frog makes it into the yellow gold zone, increment the score
// and move the frog back down to the bottom.
if (frogY < 50){
frogY = safeY + 20;
frogX = random(200, 400);
score += 1;
}
}
function displayScores() {
textSize(12);
fill(0);
// Display Lives
// Display Score
text(`Score: ${score}`, 30, 30);
// Display game over message if the game is over
if (score >= 1){
currentSong.pause();
gameIsOver = true;
}
if (gameIsOver == true){
fill (37, 65, 93);
rect(0, 0, width, height);
textSize(40);
fill(255);
text("Good Morning!", 250, height/2);
textSize(20);
text("Press the Space Bar to set a new alarm", 250, 300);
alarmHr = (" ");
alarmMin = (" ");
alarmAMPM = (" ");
}
}
//Ensures that the player stays within bounds
function checkBoundaries() {
if (frogX > width) {
frogX -= 20;
}
if (frogX < 0) {
frogX += 20;
}
if (frogY > height) {
frogY -= 20;
}
}