xxxxxxxxxx
188
let planet1, planet2;
let angle = 0; // Initial angle of rotation
let radius = 150; // Radius of the orbit for planets
let starPositions = []; // Array to store star positions and sizes
let comets = []; // Array to store comet objects
function setup() {
createCanvas(600, 400);
// Create two planets using the Planet class
planet1 = new Planet(40, 'blue', 'white'); // First planet (larger)
planet2 = new Planet(20, '#FF9800', 'rgb(165,242,249)'); // Second planet (smaller)
// Generate stars and store them
generateStars(100);
}
function draw() {
background(0); // Black background for space
drawStars(); // Draw the static stars
fill(255, 0, 0);
strokeWeight(2);
stroke(255, 106, 0); // Deep orange for the sun's outline
circle(width / 2, height / 2, 90); // Sun shape at the center
// Update and draw the planets
planet1.update(angle, radius);
planet1.show();
planet2.update(angle + PI, radius); // The PI angle ensures planets are always opposite each other
planet2.show();
// Increment the angle for continuous rotation
angle += 0.01;
// Creating and managing the creation of comets: 1% chance each frame to spawn a new comet
if (random(1) < 0.01) {
comets.push(new Comet()); // Add a comet to the array
// 50% chance to add a second comet
if (random(1) < 0.5) {
comets.push(new Comet());
}
}
// Update and draw comets, and check for collisions with the sun or planets
for (let i = comets.length - 1; i >= 0; i--) {
comets[i].update(); // Move comet
comets[i].show(); // Display comet
// Check if the comet goes offscreen
if (comets[i].offscreen()) {
comets.splice(i, 1); // Remove the comet from the array
continue;
}
// Check if comet collides with the sun
if (collidesWithSun(comets[i])) {
comets.splice(i, 1); // Remove the comet if it hits the sun
continue;
}
// Check if comet collides with planet1
if (collidesWithPlanet(comets[i], planet1)) {
comets.splice(i, 1); // Remove the comet if it hits planet1
continue;
}
// Check if comet collides with planet2
if (collidesWithPlanet(comets[i], planet2)) {
comets.splice(i, 1); // Remove the comet if it hits planet2
continue;
}
}
}
// Function to generate stars and store their positions, size, and color
function generateStars(numStars) {
for (let i = 0; i < numStars; i++) {
let x = random(0, width); // Random x position
let y = random(0, height); // Random y position
let size = random(1, 3); // Random size
// Colors for stars (magenta, blue, yellow, and white)
let colr = [color('magenta'), color('blue'), color('yellow'), color(255)];
// Store each star's position, size, and color
starPositions.push({
x: x,
y: y,
size: size,
colr: random(colr)
});
}
}
// Function to draw stars from stored positions
function drawStars() {
for (let i = 0; i < starPositions.length; i++) {
let star = starPositions[i];
stroke(star.colr); // Set color for each star
strokeWeight(star.size); // Set size for each star
point(star.x, star.y); // Draw the star
}
}
// Class to create a planet
class Planet {
constructor(size, colr, strokecolor) {
this.size = size;
this.colr = color(colr); // Fill color
this.strokecolor = strokecolor; // Outline color
this.x = 0; // X position (calculated later)
this.y = 0; // Y position (calculated later)
}
// Update the planet's position based on the angle and radius
update(angle, radius) {
this.x = width / 2 + cos(angle) * radius; // X position
this.y = height / 2 + sin(angle) * radius; // Y position
}
// Show the planet
show() {
stroke(this.strokecolor); // Outline color
strokeWeight(2)
fill(this.colr); // Fill color
ellipse(this.x, this.y, this.size); // Draw the planet
}
}
// Class to create a comet
class Comet {
constructor() {
this.x = -50; // Start comet just outside the left edge
this.y = random(height); // Random Y position
this.size = random(5, 15); // Random size
this.speed = random(5, 10); // Random speed
}
// Update the comet's position
update() {
this.x += this.speed; // Move the comet to the right
}
// Show the comet and its tail
show() {
fill(255, 255, 0); // Comet color (yellow)
noStroke(); // No outline
ellipse(this.x, this.y, this.size); // Draw the comet
// Draw the comet's tail
stroke(255, 255, 0, 150); // Tail color (semi-transparent yellow)
strokeWeight(this.size / 3); // Tail width based on comet size
line(this.x - this.size * 2, this.y, this.x, this.y); // Tail line
}
// Check if the comet is offscreen
offscreen() {
return this.x > width + 50; // Returns true if the comet is off the right side
}
}
// Function to check if a comet collides with the sun
function collidesWithSun(comet) {
let sunX = width / 2;
let sunY = height / 2;
let sunRadius = 45; // Sun's radius (half of its diameter)
// Calculate distance between the comet and the sun
let distance = dist(comet.x, comet.y, sunX, sunY);
// Check if the distance is less than the combined radius of the sun and the comet
return distance < (sunRadius + comet.size / 2);
}
// Function to check if a comet collides with a planet
function collidesWithPlanet(comet, planet) {
// Calculate the distance between the comet and the planet
let distance = dist(comet.x, comet.y, planet.x, planet.y);
// Check if the distance is less than the combined radius of the planet and the comet
return distance < (planet.size / 2 + comet.size / 2);
}