import pygame
import math
import sys
# Initialize pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
GRAVITY = 0.5
FRICTION = 0.99
BOUNCE_DAMPING = 0.8
HEXAGON_RADIUS = 200
BALL_RADIUS = 15
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncing Ball in Spinning Hexagon")
clock = pygame.time.Clock()
# Ball properties
ball_pos = [0, -HEXAGON_RADIUS // 2] # Start near top
ball_vel = [3, 0]
# Hexagon properties
hexagon_angle = 0
rotation_speed = 1 # degrees per frame
def rotate_point(point, angle):
"""Rotate a point around the origin by angle degrees."""
rad = math.radians(angle)
x, y = point
new_x = x * math.cos(rad) - y * math.sin(rad)
new_y = x * math.sin(rad) + y * math.cos(rad)
return [new_x, new_y]
def get_hexagon_vertices(radius, angle):
"""Get the vertices of a hexagon centered at origin with given radius and rotation."""
vertices = []
for i in range(6):
# Calculate angle for each vertex (60 degrees apart)
vertex_angle = math.radians(60 * i + angle)
x = radius * math.cos(vertex_angle)
y = radius * math.sin(vertex_angle)
vertices.append([x, y])
return vertices
def point_to_line_distance(point, line_start, line_end):
"""Calculate distance from point to line segment."""
# Line vector
line_vec = [line_end[0] - line_start[0], line_end[1] - line_start[1]]
# Point to line start vector
point_vec = [point[0] - line_start[0], point[1] - line_start[1]]
# Line length squared
line_len_sq = line_vec[0]**2 + line_vec[1]**2
if line_len_sq == 0:
return math.sqrt(point_vec[0]**2 + point_vec[1]**2)
# Projection scalar
t = max(0, min(1, (point_vec[0] * line_vec[0] + point_vec[1] * line_vec[1]) / line_len_sq))
# Projection point
projection = [line_start[0] + t * line_vec[0], line_start[1] + t * line_vec[1]]
# Distance
dx = point[0] - projection[0]
dy = point[1] - projection[1]
return math.sqrt(dx**2 + dy**2), projection
def get_line_normal(line_start, line_end):
"""Get the normal vector of a line segment."""
dx = line_end[0] - line_start[0]
dy = line_end[1] - line_start[1]
length = math.sqrt(dx**2 + dy**2)
if length == 0:
return [0, -1]
# Perpendicular vector (rotated 90 degrees counterclockwise)
normal = [-dy / length, dx / length]
return normal
# Main game loop
running = True
while running:
screen.fill(BLACK)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update hexagon rotation
hexagon_angle += rotation_speed
# Get current hexagon vertices
vertices = get_hexagon_vertices(HEXAGON_RADIUS, hexagon_angle)
# Apply gravity to ball
ball_vel[1] += GRAVITY
# Apply friction
ball_vel[0] *= FRICTION
ball_vel[1] *= FRICTION
# Update ball position
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# Check collision with hexagon edges
for i in range(6):
start = vertices[i]
end = vertices[(i + 1) % 6]
# Check distance from ball to this edge
distance, closest_point = point_to_line_distance(ball_pos, start, end)
if distance < BALL_RADIUS:
# Collision detected
# Get the normal vector of the edge
normal = get_line_normal(start, end)
# Move ball outside the hexagon
overlap = BALL_RADIUS - distance
ball_pos[0] += normal[0] * overlap
ball_pos[1] += normal[1] * overlap
# Reflect velocity
dot_product = ball_vel[0] * normal[0] + ball_vel[1] * normal[1]
ball_vel[0] -= 2 * dot_product * normal[0]
ball_vel[1] -= 2 * dot_product * normal[1]
# Apply bounce damping
ball_vel[0] *= BOUNCE_DAMPING
ball_vel[1] *= BOUNCE_DAMPING
# Draw hexagon
screen_vertices = []
for vertex in vertices:
screen_x = vertex[0] + WIDTH // 2
screen_y = vertex[1] + HEIGHT // 2
screen_vertices.append([screen_x, screen_y])
pygame.draw.polygon(screen, WHITE, screen_vertices, 2)
# Draw ball
ball_screen_x = int(ball_pos[0] + WIDTH // 2)
ball_screen_y = int(ball_pos[1] + HEIGHT // 2)
pygame.draw.circle(screen, RED, (ball_screen_x, ball_screen_y), BALL_RADIUS)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()