xxxxxxxxxx
244
let x
let y
let flyingObjects = [];
let sceneNum = 0;
let lives = 3;
let playButton;
let replayButton;
let timer = 30;
let girlOffset = 0;
let heroShape = 0
let randomImg = []
function preload(){
bg0_image=loadImage('images/Bg_0.png');
bg1_image=loadImage('images/bg_1.png');
bg2_image=loadImage('images/Bg_2.png');
nini1_image=loadImage('images/Nini_1.png');
nini2_image=loadImage('images/Nini_2.png');
flyingObj1 = loadImage('images/flyingobj1.png')
flyingObj2 =loadImage('images/flyingobj2.png')
welcome = loadImage('images/welc_word.png');
success = loadImage ('images/succes_word.png');
mininini=loadImage('images/ninimini.png')
}
function setup() {
createCanvas(600, 400);
randomImg =[flyingObj1,flyingObj2];
//initiate object
heroChar = new Hero();
timerClock = new Timer();
//run through the object 6 times, and each time creates a new variable for array. i%2 defines odd and even number, choosing flying obj1 or flyingobj2
for (let i = 0; i < 8; i++){
flyingObjects[i] = new FlyingObject(randomImg[i % 2]);
}
//start button in setup so button doesnt show up over and over again
playButton = createButton('LETS PLAY');
playButton.position(400,300);
playButton.mousePressed(changeScene);
replayButton = createButton('Retry')
replayButton.position(400,300);
replayButton.mousePressed(retryScene);
replayButton.hide();
}
function draw() {
//dont put button on draw
girlOffset = random(-1,1);
switch (sceneNum){
//welcome page
case 0:
draw_scene0(girlOffset);
break;
//game page
case 1:
playButton.hide();
draw_scene1();
break;
// lose
case 2:
clear();
draw_scene2();
replayButton.show();
break;
// you win
case 3:
clear();
draw_scene3(girlOffset);
replayButton.show();
break;
}
}
function draw_scene0(girlOffset) {
image(bg0_image,0,0)
image(nini1_image,girlOffset,girlOffset);
image(welcome,0,80);
}
function changeScene(){
timer = 30;
lives =3 ;
sceneNum=1;
}
//game play page
function draw_scene1() {
image(bg1_image,0,0);
timerClock.countDown();
heroChar.show();
heroChar.move();
heroChar.checkCollision();
heroChar.livesLeft();
//run through the array, it will run 6 times
for (let i = 0; i < 8; i++){
flyingObjects[i].show();
flyingObjects[i].move();
}
}
//game over page
function draw_scene2() {
image(bg2_image,0,0);
}
function retryScene(){
timer = 30;
lives =3 ;
sceneNum=1
replayButton.hide()
}
function draw_scene3(girlOffset){
image(bg0_image,0,0);
image(success,0,0);
image(nini2_image,girlOffset,girlOffset)
}
class FlyingObject {
constructor(picture){
this.picture = picture;
this.x = random(width);
this.y = random(-50, 0);
this.color = 'WHITE';
this.size = 40;
}
show() {
fill(this.color)
image(this.picture,this.x,this.y,this.size,this.size);
// rect(this.x,this.y, this.size, this.size);
}
move(){
//to move y axis, do y+3
this.y=this.y+3;
if (this.y > height){
// the randoms make sure that your shape shows randomly
this.x = random(width);
this.y = random(-50, 0);
this.color = 'WHITE';
}
// this.x = random(this.x-3, this.x+3);
this.x += random(-3, 3);
}}
class Hero {
constructor(){
this.radius = 20;
this.x = 300
this.y = 350
}
show() {
image(mininini,this.x,this.y);
}
move() {
//making sure that the hero does not go beyond the w/h of the canvas
// Right
if (keyIsDown(39)){
this.x=Math.min(this.x+10,width-40);
}
// Left, choose the max number out of the two
if (keyIsDown(37)){
this.x=Math.max(this.x-10, 3);
}
// Up, choose the max number out of the two
if (keyIsDown(38)){
this.y=Math.max(this.y-10, 3);
}
// Down, choose the min number out of the two
if (keyIsDown(40)){
this.y=Math.min(this.y+10, height -40);
}}
checkCollision(){
for (let i = 0; i < flyingObjects.length; i++) {
// declare constant by looping through all flying objects because there's 6 objects total
const object = flyingObjects[i];
if (
// find distance between hero and rectangle, must be smaller than the radius and size of the two objects
dist(object.x, object.y, this.x, this.y) <= this.radius + object.size/2
) {
if (object.color !== 'RED') {
object.color = 'RED';
lives--;
// console.log('lost life');
if (lives <= 0) {
sceneNum = 2;
}
}
}
}
}
livesLeft () {
push();
fill(0);
text('Lives',20,380);
// loop the number of life, lives determine the amount of circle that will show. For each life, you'll loop through the life num
for (let lifeNum = 0; lifeNum < lives; lifeNum++) {
// circle(60+lifeNum*20, 375, 10);
image(mininini,50+lifeNum*15,370,mininini.width*0.3, mininini.height*0.3)
}(pop);
}
}
class Timer{
constructor(){}
countDown(){
push();
fill(0);
text('Time Left'+' '+timer,500,375);
if (frameCount %60 == 0 && timer >0 && lives >0){
timer --
} else if (timer ===0 && lives >0){
sceneNum = 3;
}
pop();
}
}