xxxxxxxxxx
61
let cricket;
let ladybug;
let grasshopper
let dragonfly
let tempC;
function setup() {
createCanvas(400, 400);
cricket = new Bug("green");
ladybug = new Bug("red");
grasshopper = new Bug ("lightgreen")
dragonfly= new Bug("lightblue")
print("cricket x;" + cricket.x);
print("ladybug x;" + ladybug.x);
print("grasshopper x;" + grasshopper.x);
print("dragonfly x;" + dragonfly.x);
}
function draw() {
background(220);
cricket.display();
ladybug.display();
grasshopper.display()
dragonfly.display()
grasshopper.move()
dragonfly.move()
grasshopper.checkOffScreen()
dragonfly.checkOffScreen()
}
class Bug {
constructor(tempC) {
this.x = random(width);
this.y = random(height);
this.c = tempC;
this.xSpeed=random(-3,3)
}
display() {
fill(this.c);
circle(this.x, this.y, 20);
}
move(){
this.x = this.x+this.xSpeed
}
checkOffScreen(){
if (this.x > 400 || this.x < 0){
this.xSpeed = this.xSpeed * -1
}
}
}