xxxxxxxxxx
166
let x
let y
let flyingObjects = [];
let sceneNum = 0;
let lives = 3;
let buttonClicked = 0
let playButton;
function setup() {
createCanvas(600, 400);
//initiate object
heroChar = new Hero();
//run through the object 6 times, and each time creates a new variable for array
for (let i = 0; i < 6; i++){
flyingObjects[i] = new FlyingObject();
}
//start function in setup so button doesnt show up over and over again
draw_scene0();
}
function draw() {
//dont put button on draw
if (sceneNum === 1) {
playButton.hide()
draw_scene1();
} else if (sceneNum ===2){
draw_scene2();
}
}
function draw_scene0() {
background(100,100,100);
playButton = createButton('LETS PLAY');
playButton.position(400,300);
playButton.mousePressed(changeScene)
}
function changeScene(){
sceneNum=1;
buttonClicked = 1
playButton.hide();
console.log(sceneNum,buttonClicked)
}
//game play page
function draw_scene1() {
background(220);
heroChar.show();
heroChar.move();
heroChar.checkCollision();
//run through the array, it will run 6 times
for (let i = 0; i < 6; i++){
flyingObjects[i].show();
flyingObjects[i].move();
}
}
//game over page
function draw_scene2() {
background('BLACK');
text('GAME OVER', height/2, width/2);
}
class FlyingObject {
constructor(){
this.x = random(width);
this.y = random(-50, 0);
this.color = 'WHITE';
this.size = 40;
}
show() {
fill(this.color)
rect(this.x,this.y, this.size, this.size);
// if (this.y<400){
// rect(this.x,this.y,40, 40);
// } else {
// this.y=0
// }
}
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 = 15;
this.x = this.radius;
this.y = this.radius;
}
show() {
ellipse(this.x,this.y,this.radius * 2);
}
move() {
// Right
if (keyIsDown(39)){
this.x=Math.min(this.x+10,width-this.radius);
}
// Left, choose the max number out of the two
if (keyIsDown(37)){
this.x=Math.max(this.x-10, this.radius);
}
// Up, choose the max number out of the two
if (keyIsDown(38)){
this.y=Math.max(this.y-10, this.radius);
}
// Down, choose the min number out of the two
if (keyIsDown(40)){
this.y=Math.min(this.y+10, height - this.radius);
}}
checkCollision(){
for (let i = 0; i < flyingObjects.length; i++) {
const object = flyingObjects[i];
// const objectTop = object.y - object.size/2;
// const objectBottom = object.y + object.size/2;
// const objectLeft = object.x - object.size/2;
// const objectRight = object.x + object.size/2;
// const heroLeft = this.x - this.radius;
// const heroRight = this.x + this.radius;
// const heroTop = this.y - this.radius;
// const heroBottom = this.y + this.radius;
if (
// (heroLeft >= objectLeft && heroLeft <= objectRight ||
// heroRight >= objectLeft && heroRight <= objectRight) &&
// (heroTop >= objectTop && heroTop <= objectBottom ||
// heroBottom >= objectTop && heroBottom <= objectBottom)
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;
}
}
}
}
// if (this.x<=flyingObjects.x){
// console.log(this.x,flyingObjects.x)
}
}