Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- import numpy as np
- # Initialize Pygame
- pygame.init()
- # Set up the display
- WIDTH = 800
- HEIGHT = 600
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Ball in Spinning Hexagon")
- # Colors
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- BLACK = (0, 0, 0)
- # Ball properties
- ball_radius = 15
- ball_pos = np.array([WIDTH/2, HEIGHT/2], dtype=float)
- ball_vel = np.array([3.0, -5.0], dtype=float)
- GRAVITY = 0.2
- FRICTION = 0.99
- # Hexagon properties
- hex_radius = 200
- hex_center = (WIDTH/2, HEIGHT/2)
- rotation_angle = 0
- rotation_speed = 0.02 # radians per frame
- def get_hexagon_points(center, radius, angle):
- """Calculate vertices of a rotating hexagon"""
- points = []
- for i in range(6):
- vertex_angle = angle + i * math.pi / 3
- x = center[0] + radius * math.cos(vertex_angle)
- y = center[1] + radius * math.sin(vertex_angle)
- points.append((x, y))
- return points
- def line_intersection(p1, p2, p3, p4):
- """Find intersection point between two lines if it exists"""
- x1, y1 = p1
- x2, y2 = p2
- x3, y3 = p3
- x4, y4 = p4
- denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
- if denom == 0: # Lines are parallel
- return None
- t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom
- u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom
- if 0 <= t <= 1 and 0 <= u <= 1:
- x = x1 + t * (x2 - x1)
- y = y1 + t * (y2 - y1)
- return (x, y)
- return None
- def reflect_velocity(vel, normal):
- """Calculate reflection of velocity vector off a surface"""
- normal = np.array(normal) / np.linalg.norm(normal)
- return vel - 2 * np.dot(vel, normal) * normal
- # Game loop
- clock = pygame.time.Clock()
- running = True
- while running:
- # Event handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Clear screen
- screen.fill(BLACK)
- # Update rotation
- rotation_angle += rotation_speed
- # Get current hexagon vertices
- hex_points = get_hexagon_points(hex_center, hex_radius, rotation_angle)
- # Apply gravity and friction
- ball_vel[1] += GRAVITY
- ball_vel *= FRICTION
- # Store previous position
- prev_pos = ball_pos.copy()
- # Update ball position
- ball_pos += ball_vel
- # Check collisions with hexagon walls
- for i in range(6):
- p1 = hex_points[i]
- p2 = hex_points[(i + 1) % 6]
- # Check if ball path intersects with wall
- intersection = line_intersection(
- (prev_pos[0], prev_pos[1]),
- (ball_pos[0], ball_pos[1]),
- p1, p2
- )
- if intersection:
- # Move ball back to intersection point
- ball_pos = np.array(intersection)
- # Calculate wall normal
- wall_vec = np.array([p2[0] - p1[0], p2[1] - p1[1]])
- normal = np.array([-wall_vec[1], wall_vec[0]])
- # Reflect velocity
- ball_vel = reflect_velocity(ball_vel, normal) * 0.8 # 0.8 is elasticity
- # Prevent sticking
- ball_pos += ball_vel * 0.1
- break
- # Draw hexagon
- pygame.draw.polygon(screen, WHITE, hex_points, 2)
- # Draw ball
- pygame.draw.circle(screen, RED, ball_pos.astype(int), ball_radius)
- # Update display
- pygame.display.flip()
- # Control frame rate
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment