xxxxxxxxxx
76
let bot;
let something
function setup() {
createCanvas(400, 400);
bot = new Robot()
something = new Thing()
}
function draw() {
background(220);
bot.display()
bot.move()
something.display()
something.move()
something.collision()
}
function mousePressed(){
//click to drop box
if (something.held){
something.x-=20
something.held = false;
}
}
class Robot{
constructor(){
this.x = random(width)
this.y= random(height)
}
display(){
fill('blue')
circle(this.x,this.y,20)
}
move(){
if (keyIsDown("40")){
this.y++
}
if (keyIsDown("38")){
this.y--
}
if (keyIsDown("37")){
this.x--
}
if (keyIsDown("39")){
this.x++
}
}
}
class Thing{
constructor(){
this.x = random(width)
this.y=random(height)
this.held = false;
}
display(){
fill('red')
square(this.x,this.y,5)
}
collision(){
if (dist(this.x,this.y,bot.x,bot.y)<12){
this.held = true;
}
}
move(){
if (this.held){
this.x=bot.x
this.y=bot.y
}
}
}