xxxxxxxxxx
396
let cars = [];
let coins = [];
let frog1;
let carsNum = 10;
let sceneNum = 0;
let frogLives = 3;
let signs = [];
let d;
let froggy;
let signny;
let coinny;
let moussy;
let bgsign;
let bgmouse;
let bgcoin;
let coinfinish = false;
let bg0;
let bg4;
let music;
function preload() {
froggy = loadImage("assets/frog.png");
signny = loadImage("assets/signs.png");
bgsign = loadImage("assets/bg-sign.png");
coinny = loadImage("assets/coin.gif");
moussy = loadImage("assets/mouse.gif");
bgmouse = loadImage("assets/bg-mouse.png");
bgcoin = loadImage("assets/bg-coin.png");
bg0 = loadImage("assets/bg0.gif");
bg4 = loadImage("assets/bg4.gif");
music = loadSound("assets/music.mp3");
}
function setup() {
createCanvas(600, 400);
music.loop();
for (let i = 0; i < carsNum; i++) {
cars[i] = new Car(random(width), random(height - 100), 100);
}
for (let n = 0; n < 5; n++) {
signs[n] = new Sign();
}
for (let m = 0; m < 8; m++) {
coins[m] = new Coin();
}
frog1 = new Frog();
// scene1 = new Scene();
}
function draw() {
switch (sceneNum) {
// ----------------------------CASE 0-----------------------------------
case 0:
push();
scale(0.24);
image(bg0, 0, 0);
pop();
fill(150);
text("Made in Dry", 270, 390);
push();
fill(255);
textStyle(BOLD);
text("PRESS ANYWHERE TO START!", 210, 300);
pop();
// scene1.changescene();
console.log("scene 0 num: " + sceneNum);
break;
// ----------------------------CASE 1-----------------------------------
case 1:
background(220);
push();
scale(0.24);
image(bgmouse, 0, 0);
pop();
for (let i = 0; i < carsNum; i++) {
cars[i].body();
cars[i].move();
cars[i].checkCollision();
}
frog1.body();
frog1.move();
frog1.home();
console.log("scene 1");
currentFrogLives();
fill(255);
text("Watch out the mice!", 10, 390);
push();
textStyle(BOLD);
fill(255);
text("Across the road to the top!", 190, 20);
pop();
break;
// ----------------------------CASE 2-----------------------------------
case 2:
background(120);
push();
scale(0.24);
image(bgsign, 0, 0);
pop();
for (let n = 0; n < signs.length; n++) {
signs[n].show();
signs[n].checkCollision();
}
frog1.body();
frog1.move();
frog1.home();
frog1.checkbox();
console.log("scene 2");
currentFrogLives();
push();
fill(34);
textStyle(BOLD);
text("Find the Path entrance!", 240, 20);
pop();
break;
// ----------------------------CASE 3-----------------------------------
case 3:
background(220);
push();
scale(0.24);
image(bgcoin, 0, 0);
pop();
for (let m = 0; m < coins.length; m++) {
d = dist(frog1.x, frog1.y, coins[m].x, coins[m].y);
if (d < 20) {
coins.splice(m, 1);
} else {
fill(255);
}
if (coins[m] != null) {
coins[m].show();
}
print(coins.length);
if (coins.length < 1) {
coinfinish = true;
}
}
frog1.body();
frog1.move();
frog1.home();
push();
console.log("scene 3");
text("Your coins are dropping off!", 10, 376);
text("Pick them up and buy ticket!", 10, 393);
pop();
break;
// ----------------------------CASE 4-----------------------------------
case 4:
// scene1.changescene();
push();
scale(0.24);
image(bg4, 0, 0);
pop();
fill(255);
textStyle(BOLD);
text("PRESS ANYWHERE TO REPLAY!", 210, 300);
console.log("scene 4");
break;
}
}
function currentFrogLives() {
for (let i = 0; i < frogLives; i++) {
//ellipse(i*20, height-10, 20);
}
}
class Frog {
constructor() {
this.x = width / 2;
this.y = height - 50;
this.w = 40;
this.h = 40;
this.c = color(0, 255, 0);
}
body() {
// fill(this.c);
// ellipse(this.x, this.y, this.w, this.h);
push();
imageMode(CENTER);
image(froggy, this.x, this.y, froggy.width * 0.25, froggy.height * 0.25);
pop();
}
move() {
if (this.y > 380) {
this.y = 380;
}
if (this.x > 580) {
this.x = 580;
}
if (this.x < 20) {
this.x = 20;
}
if (keyIsDown(38)) {
this.y -= 3;
}
if (keyIsDown(40)) {
this.y += 3;
}
if (keyIsDown(39)) {
this.x += 3;
}
if (keyIsDown(37)) {
this.x -= 3;
}
}
checkbox() {
if (this.y > 289 && this.y < 400 && this.x > 231 && this.x < 550) {
textSize(13);
fill(255);
//text("Downtown & Brooklyn",189,217);
}
}
home() {
if (sceneNum < 3) {
if (this.y < 20) {
sceneNum++;
this.y = height - 30;
}
} else if (coinfinish == true) {
sceneNum++;
for (let m = 0; m < 8; m++) {
append(coins, new Coin());
}
coinfinish = false;
} else if (sceneNum > 4) {
sceneNum = 1;
}
// if (sceneNum == 1) {
// this.x = width / 2;
// this.y = height - 50;
}
}
class Car {
constructor(x, y, c) {
// a special method that creates the car object
this.x = x;
this.y = y;
this.w = 40;
this.h = 25;
this.c = c;
}
body() {
// fill(this.c);
// rect(this.x-50, this.y, this.w, this.h);
push();
imageMode(CENTER);
image(moussy, this.x, this.y, moussy.width * 0.25, moussy.height * 0.25);
pop();
}
move() {
this.x++;
if (this.x > width + 50) {
this.x = 0;
}
}
checkCollision() {
if (
frog1.x + frog1.w / 2 > this.x &&
frog1.x < this.x + this.w &&
frog1.y + frog1.h / 2 > this.y &&
frog1.y < this.y + this.h
) {
console.log("You Lose!");
text("OUCH!", frog1.x - 30, frog1.y - 20);
frog1.y = height - 50; // reset frog pos
frogLives--;
}
}
}
class Sign {
constructor(x, y, w = 100, h = 40, c = 100) {
this.x = random(100, width - 100);
this.y = random(50, height - 100);
this.w = w;
this.h = h;
this.c = c;
}
show() {
// fill(255);
// rect(this.x, this.y, this.w, this.h);
image(signny, this.x, this.y, signny.width * 0.25, froggy.height * 0.25);
}
checkCollision() {
if (
frog1.x + frog1.w / 2 > this.x &&
frog1.x < this.x + this.w &&
frog1.y + frog1.h / 2 > this.y &&
frog1.y < this.y + this.h
) {
console.log("bumped!");
push();
fill(0);
text("This is not the Path!", 10, 390);
pop();
// frog1.y = height - 50; // reset frog pos
}
}
}
class Coin {
constructor() {
this.x = random(20, width - 20);
this.y = random(70, height - 40);
this.w = 20;
}
show() {
// ellipse(this.x, this.y, this.w);
push();
imageMode(CENTER);
image(coinny, this.x, this.y, coinny.width * 0.25, coinny.height * 0.25);
pop();
}
}
function mousePressed() {
if (sceneNum === 4) {
sceneNum = 0;
frog1 = new Frog()
} else if (sceneNum === 0) {
sceneNum = 1;
}
}
// class Scene {
// changescene() {
// if (mouseIsPressed) {
// console.log("+++++++++++++++++++++++++++++ " + sceneNum)
// if (sceneNum === 4) {
// sceneNum = 0;
// } else {
// sceneNum++;
// }
// }
// // if (sceneNum == 5) {
// // console.log("-----------------------------")
// // sceneNum = 0;
// // }
// }
// }