o simulate the setting sun shining through the clouds and changing color over time using Python, we can use the Pygame library which allows us to create graphical simulations. Here is a sample code:
pythonCopy codeimport pygame
import random
pygame.init()
# Define the screen size
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Sunset Simulation")
# Define the colors
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)
PINK = (255, 192, 203)
PURPLE = (128, 0, 128)
BLUE = (0, 0, 255)
# Define the cloud and sun properties
clouds = []
cloud_width = 100
cloud_height = 50
cloud_speed = 2
sun_x = 300
sun_y = 100
sun_size = 50
sun_color = YELLOW
sun_radius = int(sun_size / 2)
sun_gradient = [YELLOW, ORANGE, RED, PINK, PURPLE, BLUE]
gradient_index = 0
gradient_direction = 1
# Define the function to create a new cloud
def create_cloud():
cloud_x = random.randint(-cloud_width, SCREEN_WIDTH - cloud_width)
cloud_y = random.randint(0, SCREEN_HEIGHT / 2)
cloud = pygame.Rect(cloud_x, cloud_y, cloud_width, cloud_height)
clouds.append(cloud)
# Create the initial clouds
for i in range(5):
create_cloud()
# Define the main loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the sun color gradient
if gradient_index == len(sun_gradient) - 1:
gradient_direction = -1
elif gradient_index == 0:
gradient_direction = 1
gradient_index += gradient_direction
# Draw the sky
if gradient_direction == 1:
sky_color = sun_gradient[gradient_index]
else:
sky_color = sun_gradient[gradient_index + 1]
screen.fill(sky_color)
# Draw the clouds
for cloud in clouds:
pygame.draw.ellipse(screen, WHITE, cloud)
cloud.move_ip(cloud_speed, 0)
if cloud.right > SCREEN_WIDTH:
cloud.left = 0
# Draw the sun
pygame.draw.circle(screen, sun_color, (sun_x, sun_y), sun_radius)
# Update the sun color
sun_color = sun_gradient[gradient_index]
# Update the display
pygame.display.update()
pygame.quit()
This program uses pygame to create a screen and simulate a sunset. It defines the properties of the clouds and the sun, and uses a color gradient to change the color of the sun over time. The clouds move across the screen, and the sun gradually changes color as it sets.