xxxxxxxxxx
60
let cricket;
let ladybug;
let fly;
let bee;
function setup() {
createCanvas(400, 400);
cricket = new Bug("green");
ladybug = new Bug ("red");
fly = new Bug ("grey")
bee = new Bug ("yellow")
print("cricket x:" + cricket.x);
print("ladybug x:" + ladybug.x);
print("fly x" + fly.x);
print("bee x" + bee.x)
}
function draw(){
background(255);
fly.move();
bee.move();
ladybug.move();
cricket.move();
cricket.display();
ladybug.display();
fly.display();
bee.display();
cricket.offscreen();
ladybug.offscreen();
fly.offscreen();
bee.offscreen();
}
class Bug {
constructor(tempC){
this.x = random(width);
this.y = random (height);
this.xSpeed = random (1,3);
this.c = tempC;
}
display(){
fill(this.c)
circle(this.x,this.y,20)
}
move(){
//this.x=this.x+1;
//this.x++
this.x = this.x + this.xSpeed;
}
offscreen(){
if(this.x > width || this.x < 0){
this.xSpeed = this.xSpeed * -1;
}
}
}