Adding a button to change the time, date, and speed of the simulation in Pygame involves a bit more complexity. We’ll have to handle button clicks and adjust the orbital speeds of the planets accordingly. Pygame doesn't have built-in support for GUI elements like buttons, so we'll simulate one by drawing a rectangle and detecting clicks inside its boundaries.
This example builds upon the previous solar system simulation by adding a simple button that, when clicked, toggles the speed of the planet's orbit.
Note: For simplicity, this button will just increase or reset the speed of the animation. Implementing a full date and time change feature requires substantially more code to accurately reflect the positions of planets on a given date, involving astronomical calculations beyond the scope of this example.
First, make sure you have Pygame installed:
pip install pygame
Here's the updated script:
import pygame
import sys
import math
pygame.init()
# Screen setup
width, height = 900, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Solar System Simulation")
# Colors
black = (0, 0, 0)
yellow = (255, 255, 0) # Sun color
button_color = (100, 200, 150) # Button color
text_color = (255, 255, 255) # Text color
colors = [(139, 137, 137), (205, 190, 112), (0, 0, 255), (255, 69, 0),
(218, 165, 32), (184, 134, 11), (173, 216, 230), (0, 191, 255)]
# Font setup for the button text
font = pygame.font.SysFont(None, 36)
# Planet settings
num_planets = 8
orbit_radii = [50, 80, 110, 140, 200, 250, 300, 350]
base_orbit_speeds = [0.04, 0.03, 0.02, 0.015, 0.012, 0.01, 0.008, 0.006]
orbit_speeds = base_orbit_speeds[:]
planet_sizes = [4, 8, 8, 7, 15, 12, 10, 9]
time_multiplier = 1
def draw_button(screen, text, position, color):
button = pygame.Rect(position[0], position[1], 200, 50) # Create a rect for button
pygame.draw.rect(screen, color, button) # Draw the button on the screen
text_surf = font.render(text, True, text_color) # Create text surface
text_rect = text_surf.get_rect(center=(position[0]+100, position[1]+25)) # Center text in button
screen.blit(text_surf, text_rect) # Draw text
return button
def check_button_click(position, button_rect):
return button_rect.collidepoint(position)
# Button settings
button_text = "Toggle Speed"
button_position = (width - 220, 50) # Button position
button = None # Placeholder for button rect
# Run flag
running = True
# Clock to control the frame rate
clock = pygame.time.Clock()
# Main loop
while running:
mouse_click_position = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_click_position = pygame.mouse.get_pos()
screen.fill(black)
pygame.draw.circle(screen, yellow, (width // 2 - 100, height // 2), 20) # Sun
current_ticks = pygame.time.get_ticks() / 1000 * time_multiplier
for i in range(num_planets):
angle = current_ticks * orbit_speeds[i]
x = math.cos(angle) * orbit_radii[i] + width // 2 - 100
y = math.sin(angle) * orbit_radii[i] + height // 2
pygame.draw.circle(screen, colors[i], (int(x), int(y)), planet_sizes[i])
# Button Logic
button = draw_button(screen, button_text, button_position, button_color)
if mouse_click_position:
if check_button_click(mouse_click_position, button):
time_multiplier = 3 if time_multiplier == 1 else 1 # Toggle speed
orbit_speeds = [speed * time_multiplier for speed in base_orbit_speeds]
pygame.display.flip()
clock.tick(60) # 60 FPS
pygame.quit()
sys.exit()
This script extends the previous example by:
-
Adding a Button:
draw_button: A function to draw a button on the screen and display text on it.
check_button_click: Checks if the mouse click position is within the button's rectangle.
-
Toggling Speed:
- A single button is displayed that, when clicked, toggles the
time_multiplier variable between 1 and 3. This multiplier affects the orbit_speeds of the planets, causing them to orbit faster or reset to their original speed.
-
Interactive Elements:
- The script checks for mouse clicks and, if the click is on the button, toggles the speed of the planetary orbits.
This example demonstrates basic interactive elements in Pygame. For a full application, consider a more sophisticated UI framework or incorporating additional libraries that facilitate GUI development with Pygame.