xxxxxxxxxx
79
let stars =[];
let rockets=[];
function setup() {
createCanvas(600, 700);
for (let i = 0; i < 100; i++) {
stars[i] = new Star(random(width), random(height), random(1, 5));
}
rocket = new Rocket(width / 2, height, 5);
// Add mouse click event listener
mouseClicked = function() {
let x = mouseX;
let y = mouseY;
let rocket = new Rocket(x, y, 5);
rockets.push(rocket);
}
}
function draw() {
background(1,22,64);
for (let star of stars) {
star.show();
}
for (let i = 0; i < rockets.length; i++) {
rockets[i].update();
rockets[i].show();
}
}
//Class making the randomized stars in the background
class Star {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
}
show() {
stroke(255);
strokeWeight(this.size);
point(this.x, this.y);
}
}
//Class making the rocket
class Rocket {
constructor(x, y, velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
}
update() {
this.y -= this.velocity;
}
//Body of rocket
show() {
fill(255);
// body
ellipse(this.x, this.y + 25, 20, 50);
// nose
fill(200,0,0)
triangle(this.x, this.y-30, this.x - 10, this.y , this.x + 10, this.y );
// windows
fill(0);
ellipse(this.x - 5, this.y + 10, 5, 5);
ellipse(this.x + 5, this.y + 10, 5, 5);
// flames
fill(255, 0, 0);
triangle(this.x - 10, this.y + 45, this.x, this.y + 75, this.x + 10, this.y + 45);
}
}