xxxxxxxxxx
94
let stars = [];
let planets = [];
class Star {
constructor() {
this.x = random(width);
this.y = random(height);
this.size = random(1, 4);
this.brightness = random(100, 180);
}
update() {
this.brightness = random(100, 180);
}
display() {
fill(this.brightness);
noStroke();
ellipse(this.x, this.y, this.size);
}
}
class Planet {
constructor() {
this.x = random(width);
this.y = random(height);
this.size = random(20, 60);
this.color = color(random(50, 100), random(50, 100), random(100, 200));
this.orbitSpeed = random(0.005, 0.015);
this.angle = random(TWO_PI);
// the orbit radius depends on the size of the planet
this.orbitRadius = this.size*4;
}
update() {
// Update the planet's position along its orbit
this.angle += this.orbitSpeed;
this.x = width / 2 + cos(this.angle) * this.orbitRadius;
this.y = height / 2 + sin(this.angle) * this.orbitRadius;
}
display() {
fill(this.color);
noStroke();
ellipse(this.x, this.y, this.size);
}
drawOrbit() {
noFill();
stroke(100);
ellipse(width / 2, height / 2, this.orbitRadius * 2);
}
}
function drawSun(){
fill(random(230, 255),random(230, 255),0);
ellipse(width/2, height/2, 100);
}
function setup() {
createCanvas(700, 500);
//creating and storing the stars
for (let i = 0; i < 200; i++) {
stars.push(new Star());
}
//creating and storing the planets
for (let i = 0; i < 4; i++) {
planets.push(new Planet());
}
}
function draw() {
background(0);
//draw sun
drawSun();
// updating stars
for (let i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].display();
}
// updating planets
for (let i = 0; i < planets.length; i++) {
planets[i].update();
planets[i].display();
planets[i].drawOrbit();
}
}