New Microsoft Word Document
New Microsoft Word Document
import math
import time
# Initialize pygame
pygame.init()
# Screen dimensions
# Colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GRAVITY_VALUES = {
"Earth": 9.8,
"Mars": 3.71,
"Moon": 1.625,
"Jupiter": 24.79,
"Pluto": 0.62,
# Pendulum parameters
# Timer
start_time = None
elapsed_time = 0
timer_active = False
# Swing times
swing_times = []
best_swing_time = None
# Damping factor
damping = 0.99
dragging = False
clock = pygame.time.Clock()
FPS = 60
# Fonts
# Gradient background
def draw_gradient_background():
for y in range(HEIGHT):
def draw_planet_buttons():
button_width = 120
button_height = 50
planets = list(GRAVITY_VALUES.keys())
x = 20
y = 50 + i * (button_height + 10)
def draw_reset_button():
button_width = 120
button_height = 50
x = 20
y = HEIGHT - 70
def draw_swing_times_table():
table_y = 20
# Table header
if best_swing_time:
def draw_wooden_rod():
rod_length = 60
rod_width = 10
# Main loop
running = True
while running:
screen.fill(WHITE)
draw_gradient_background()
planets = draw_planet_buttons()
draw_reset_button()
draw_swing_times_table()
draw_wooden_rod()
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x = 20
y = 50 + i * (50 + 10)
gravity = GRAVITY_VALUES[planet]
reset_x = 20
reset_y = HEIGHT - 70
if reset_x <= mouse_x <= reset_x + 120 and reset_y <= mouse_y <= reset_y + 50:
angle = 0
angular_velocity = 0
start_time = None
elapsed_time = 0
timer_active = False
swing_times.clear()
best_swing_time = None
dragging = True
angular_velocity = 0
start_time = time.time()
timer_active = True
if dragging:
dx = mouse_x - pivot[0]
dy = mouse_y - pivot[1]
if not dragging:
angular_velocity += angular_acceleration
angle += angular_velocity
angular_velocity *= damping
swing_times.append(elapsed_time)
best_swing_time = elapsed_time
timer_active = False
shadow_x = pendulum_pos[0]
shadow_y = HEIGHT - 50
# Display timer
pygame.display.flip()
clock.tick(FPS)
pygame.quit()