xxxxxxxxxx
78
let robot;
let box;
function setup() {
createCanvas(400, 400);
noStroke();
rectMode(CENTER);
robot = new Ball();
box = new Box();
}
function draw() {
background(220);
box.display();
box.moving();
robot.display();
robot.update();
}
class Ball {
constructor() {
this.position = createVector(random(width), random(height));
this.velocity = new createVector(random(-5, 5), random(-5, 5));
this.size = 20;
this.c = color(170);
}
update() {
//add the current speed to the position
this.position.add(this.velocity);
//check if offscreen
if (this.position.x < 0 || this.position.x > width) {
this.velocity.x = this.velocity.x * -1;
}
if (this.position.y < 0 || this.position.y > height) {
this.velocity.y = this.velocity.y * -1;
}
}
display() {
fill(this.c);
ellipse(this.position.x, this.position.y, this.size)
}
}
class Box {
constructor() {
this.position = createVector(width/2,height/2);
this.w = 20;
this.h = 25;
this.delivered = false;
}
display() {
fill(0);
rect(this.position.x,this.position.y, this.w, this.h);
}
moving() {
if(this.position.x > 350){
this.delivered = true;
print("delivered");
}
if (this.delivered == false){
if (this.position.dist(robot.position) < 50 ){
print("moving");
this.position = robot.position.copy();
}
}
}
}