xxxxxxxxxx
58
let x = 50; // Initial x position of the moon
let y = 50; // Initial y position of the moon
let speedX = 2; // Speed of the moon along the x-axis
let speedY = 1; // Speed of the moon along the y-axis
let diameter = 80; // Initial diameter of the moon
let moonColor; // Color of the moon
function setup() {
createCanvas(600, 400);
moonColor = color(255, 240, 200); // Set moon color to a pale yellow
}
function draw() {
drawStarryBackground(); // Draw starry sky background
// Draw the moon
drawMoon(x, y, diameter, moonColor);
// Move the moon
x += speedX;
y += speedY;
// Check for collisions with the walls
if (x <= diameter / 2 || x >= width - diameter / 2) {
speedX *= -1; // Reverse the x direction
diameter += 2; // Increase the diameter more slowly
moonColor = color(random(200, 255), random(200, 255), random(150, 200)); // Change moon color
}
if (y <= diameter / 2 || y >= height - diameter / 2) {
speedY *= -1; // Reverse the y direction
diameter += 2; // Increase the diameter more slowly
moonColor = color(random(200, 255), random(200, 255), random(150, 200)); // Change moon color
}
}
function drawStarryBackground() {
background(0); // Set background to black
// Draw stars
randomSeed(4); // Set random seed for consistent randomness
for (let i = 0; i < 1000; i++) {
let x = random(width);
let y = random(height);
let size = random(1, 4);
fill(255);
noStroke();
ellipse(x, y, size, size);
}
}
function drawMoon(x, y, diameter, moonColor) {
fill(moonColor);
noStroke();
ellipse(x, y, diameter, diameter);
}