xxxxxxxxxx
58
/* this sketch currently draws a single ship that doesn't move. Fix the sketch to have an array of 40 ships that all move across the canvas and get reset when they move across the edge of the screen.*/
myShip = [];
function setup() {
createCanvas(800, 800);
noStroke();
for(let i = 0; i<40; i++)
{
myShip[i] = new Ship(random(0, width), random(0, height));
}
}
function draw() {
background(255);
for(let i = 0; i< myShip.length; i++)
{
myShip[i].display();
myShip[i].reset();
myShip[i].move();
}
}
class Ship {
constructor(_x, _y) {
this.x = _x;
this.y = _y;
this.speed = random(1,5);
this.c = color(random(0, 255), random(0, 255), random(0, 255), 40);
this.vel = (random(1, 3), 0);
}
move() {
if(this.x<= 800)
{
this.x += this.speed;
}
}
display() {
fill(this.c);
ellipse(this.x, this.y, 20, 20);
ellipse(this.x, this.y + 20, 40, 20);
}
reset() {
if (this.x >= 800 ) {
this.x = 0;
}
}
}