xxxxxxxxxx
359
let mode = 0;
let img;
let myFont;
let trees = [];
let foliages = [];
let pp;
let bears = [];
let mooses = [];
let sound;
let stopbutton;
let playbutton;
let lives = 7;
function preload()
{
sound = loadSound('music.mp3');
myFont = loadFont('Lobster-Regular.ttf');
}
// function mousePressed()
// {
// sound.play();
// }
function setup() {
createCanvas(400, 400);
frameRate(30);
sound.loop();
sound.stop();
img1 = loadImage('title.png');
imgMoose = loadImage('moose.png');
imgBear = loadImage('bears.png');
imgPlayer = loadImage('player.png')
imgWin = loadImage('win.png');
//scene
for (let i=0; i<20; i++){
trees[i] = new Tree(random(width), 50, color(255, random(255), 0));
foliages[i] = new Foliage(random(width), 110, color(random(255), 0, 255));
}
for (let i=0; i<3; i++){
let bearY = random(150, 250);
bears[i] = new Bear(random(width), bearY);
mooses[i] = new Moose(random(width), bearY);
}
//player
pp = new People();
}
function draw() {
background(220);
noStroke();
switch(mode){
case 0:
scene0();
break;
case 1:
scene1();
break;
case 2:
scene2();
break;
case 3:
scene3();
break;
default:
}
}
function keyPressed(){
if(key=='0'){
mode = 0;
}else if(key =='1'){
mode = 1;
}else if (key =='2'){
mode = 2;
}
else if (key == '3'){
mode = 3;
}
print(mode);
}
function scene0(){
clear();
// background(194, 239, 194);
image(img1, 0, 0);
fill(255, 102, 0);
textFont(myFont);
textSize(30);
textAlign(CENTER);
text('A Foliage Adventure', 250, 120);
playbutton = createButton('Play');
playbutton.position(180, 375);
playbutton.mousePressed(playsound);
}
function scene1(){
clear();
background(0);
//road
fill(128, 128, 128);
rect(0, 150, 400, 120);
fill(255,255,255);
rect(0, 200, 60, 10);
rect(120, 200, 60, 10);
rect(240, 200, 60, 10);
rect(360, 200, 60, 10);
//draw bear+moose
for (let i=0; i<3; i++){
bears[i].body();
bears[i].motion();
mooses[i].body();
mooses[i].motion();
}
//draw scene
for (let i=0; i<20; i++){
trees[i].body();
trees[i].motion();
foliages[i].body();
foliages[i].motion();
}
//draw player
pp.body();
pp.motion();
pp.home();
ppLives();
bears.forEach(bear => {
bear.body();
bear.motion();
let dists =dist(bear.x,bear.y,pp.x,pp.y);
if(dists<(pp.w+bear.w/2)){
text("bear!", 50, 25);
// lives--;
if (frameCount%20===0){
lives--;
}
}
});
mooses.forEach(moose => {
moose.body();
moose.motion();
let dists1 = dist(moose.x, moose.y, pp.x, pp.y);
if(dists1<(pp.w+moose.w/2)){
mode = 2;
}
})
}
function scene2(){
clear();
background(255, 173, 51);
image(imgWin, 0, 0);
fill(255);
textSize(30);
textAlign(CENTER);
text('You found it!', 300, 200);
stopbutton = createButton('Stop');
stopbutton.position(220, 375);
stopbutton.mousePressed(stopsound);
}
function scene3(){
clear();
background (255,0,0);
fill(255);
textSize(24);
textAlign(CENTER);
text('Oh NO... press 1 to try again!', 200, 200);
}
function ppLives(){
for (let i=0; i<lives; i++){
fill(255);
ellipse(i*20, height-10, 20);
}
if(lives == 0){
mode = 3;
}
}
//classes
//the character/player
class People {
constructor(){
this.x = 30;
this.y = 200;
this.w = 20;
this.h = 20;
this.c = color(0,0,0);
}
body(){
image(imgPlayer, this.x, this.y);
}
motion(){
if(keyIsDown(UP_ARROW)){
this.y-=2;
}
if(keyIsDown(DOWN_ARROW)){
this.y+=2;
}
if(keyIsDown(39)){
this.x+=2;
}
if(keyIsDown(40)){
this.x-=2;
}
}
home(){
if(this.x > 400){
// // scene++;
// this.x = 30;
// this.y = 200;
mode = 1;
}
}
}
class Bear {
constructor(x,y){
this.x = x;
this.y = y;
this.w = 30;
this.h = 20;
}
body(){
image(imgBear, this.x, this.y);
}
motion(){
this.x-=2;
if (this.x <0){
this.x = 400;
}
}
}
class Moose {
constructor (x, y){
this.x = x;
this.y = y;
this.w = 15;
this.h = 15;
}
body(){
image(imgMoose, this.x, this.y);
}
motion(){
this.x-=3;
if(this.x <100){
this.x = 400;
}
}
}
class Tree {
constructor(x, y, c){
this.x = x;
this.y = y;
this.w = 30;
this.h = 100;
this.c = c;
}
body(){
fill(this.c);
rect(this.x, this.y, this.w, this.h);
rect(this.x, this.y+ 200, this.w, this.h);
}
motion(){
this.x++;
if (this.x > width){
this.x = 0;
}
}
}
class Foliage {
constructor(x, y, c){
this.x = x;
this.y = y;
this.w = 30;
this.h = 40;
this.c = c;
}
body(){
fill(this.c);
rect(this.x, this.y, this.w, this.h);
rect(this.x, this.y+200, this.w, this.h);
}
motion(){
this.x++;
if (this.x > width){
this.x = 0;
}
}
}
function playsound(){
if(sound.isPlaying() == false)
{
sound.play();
mode = 1;
}
}
function stopsound()
{
if(sound.isPlaying() == true)
{
sound.pause();
}
}