xxxxxxxxxx
114
let kids1;
let sceneNum = 0;
let sofa;
let table;
let bed;
let sofaImg = null;
let fridgeImg = null;
let furniture = [];
function preload() {
fridgeImg = loadImage('fridge.png');
sofaImg = loadImage('sofa.jpg');
}
function setup() {
createCanvas(400, 400);
kids1 = new Kids1();
// x value y value w h color value img
sofa = new Sofa(random(0,100), random(0, 100), 100, 30, color(random(255)), fridgeImg);
table = new Sofa(random(200,300), random(200, 300), 50, 50, color(random(255)), sofaImg);
fridge = new Sofa(random(100,200), random(300, 400), 30, 80, color(random(255)), sofaImg);
furniture.push(sofa);
furniture.push(table);
furniture.push(fridge);
let randomHasPhone = int( random( 1 , furniture.length ) );
console.log(randomHasPhone)
for( let i = 0; i < furniture.length; i++ ){
if( randomHasPhone === (i+1) ){
furniture[i].hasPhone = true;
}
}
}
function draw() {
background(220);
kids1.body();
kids1.move();
for( let i = 0; i < furniture.length; i++ ){
furniture[i].body();
}
}
class Kids1 {
constructor() {
this.x = width / 2;
this.y = height - 50;
this.w = 30;
this.h = 30;
this.c = color(0, 255, 0);
}
body() {
fill(this.c);
ellipse(this.x, this.y, this.w, this.h);
}
move() {
if (keyIsDown(38)) {
this.y -= 3;
}
if (keyIsDown(40)) {
this.y += 3;
}
if (keyIsDown(37)) {
this.x -= 3;
}
if (keyIsDown(39)) {
this.x += 3;
}
}
}
class Sofa {
constructor(x, y, w=50, h=35, c, img, hasPhone = false) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.c = c;
this.img = img;
this.hasPhone = hasPhone; // boolean
}
body() {
fill(this.c);
if( this.img ){
image(this.img, this.x, this.y, this.w, this.h);
}
this.checkCollision();
}
checkCollision() {
if (kids1.x + kids1.w / 2 > this.x && kids1.x < this.x + this.w && kids1.y + kids1.h / 2 > this.y && kids1.y < this.y + this.h) {
console.log('bumped!');
if( this.hasPhone === true ){
text('here is the phone 🎉', 10, 30)
}else {
text('here is no phone :(', 10, 30)
}
}
}
}