xxxxxxxxxx
let grapefruit = [];
let sushi = [];
let frames = 0;
let collided = false;
function setup() {
canvas = createCanvas(400, 400);
grapefruit = new Sprite(random(width), random(height), 300,300);
sushi = new Sprite(random(width), random(height),300, 300);
grapefruit.img = '0.jpg';
sushi.img = 'sushi.png';
sushi.collider = 'dynamic';
grapefruit.collider = 'dynamic';
grapefruit.scale = 0.2;
grapefruit.vel.x = random(-7, 7);
grapefruit.vel.y = random(-7, 7);
sushi.scale = 0.2;
sushi.vel.x = random(-7, 7);
sushi.vel.y = random(-7, 7);
}
function draw() {
background(250);
if(grapefruit.vel.x<0){
grapefruit.mirror.x = false;
}else grapefruit.mirror.x = true;
if(sushi.vel.x<0){
sushi.mirror.x = true;
}else sushi.mirror.x = false;
if(grapefruit.x<0 || grapefruit.x>width){
grapefruit.vel.x *= -1;
}
if(grapefruit.y<0 || grapefruit.y>height){
grapefruit.vel.y *= -1;
}
if(sushi.x<0 || sushi.x>width){
sushi.vel.x *= -1;
}
if(sushi.y<0 || sushi.y>height){
sushi.vel.y *= -1;
}
if(sushi.collides(grapefruit)){
grapefruit.img = "juicebox.gif"
sushi.img = "sushi-small.gif"
collided = true;
console.log("collision");
}
if(collided && frames == 200){
grapefruit.img = '0.jpg';
sushi.img = 'sushi.png';
collided = false;
frames=0;
}else if(collided) frames++;
}