Guest User

early-grok-3

a guest
Feb 18th, 2025
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | Source Code | 0 0
  1. import pygame
  2. import math
  3. import numpy as np
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Window settings
  9. WIDTH = 800
  10. HEIGHT = 800
  11. CENTER = (WIDTH // 2, HEIGHT // 2)
  12. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  13. pygame.display.set_caption("Bouncing Ball in Spinning Hexagon")
  14.  
  15. # Colors
  16. WHITE = (255, 255, 255)
  17. BLACK = (0, 0, 0)
  18. RED = (255, 0, 0)
  19. BLUE = (0, 0, 255)
  20.  
  21. # Ball properties
  22. ball_radius = 15
  23. ball_pos = np.array([float(CENTER[0]), float(CENTER[1])])
  24. ball_velocity = np.array([5.0, -10.0])  # Initial velocity (x, y)
  25. gravity = 0.3
  26. friction = 0.99  # Air resistance
  27. elasticity = 0.8  # Energy loss on bounce
  28.  
  29. # Hexagon properties
  30. hex_radius = 200
  31. hex_angle = 0  # Rotation angle in radians
  32. hex_rotation_speed = 0.02  # Radians per frame
  33. hex_vertices = []
  34.  
  35. # Calculate initial hexagon vertices
  36. def calculate_hex_vertices():
  37.     vertices = []
  38.     for i in range(6):
  39.         angle = hex_angle + (i * math.pi / 3)
  40.         x = CENTER[0] + hex_radius * math.cos(angle)
  41.         y = CENTER[1] + hex_radius * math.sin(angle)
  42.         vertices.append(np.array([x, y]))
  43.     return vertices
  44.  
  45. # Get line segment between two vertices
  46. def get_hex_sides(vertices):
  47.     sides = []
  48.     for i in range(6):
  49.         sides.append((vertices[i], vertices[(i + 1) % 6]))
  50.     return sides
  51.  
  52. # Vector operations
  53. def dot_product(v1, v2):
  54.     return np.dot(v1, v2)
  55.  
  56. def normalize(v):
  57.     return v / np.linalg.norm(v) if np.linalg.norm(v) > 0 else v
  58.  
  59. # Check collision between ball and line segment
  60. def check_collision(ball_pos, ball_radius, p1, p2):
  61.     # Vector from p1 to p2
  62.     line_vec = p2 - p1
  63.     # Vector from p1 to ball center
  64.     point_vec = ball_pos - p1
  65.    
  66.     line_len = np.linalg.norm(line_vec)
  67.     if line_len == 0:
  68.         return False, None
  69.    
  70.     line_unit = line_vec / line_len
  71.    
  72.     # Project point_vec onto line_unit
  73.     t = max(0, min(line_len, dot_product(point_vec, line_unit)))
  74.    
  75.     # Get closest point on line
  76.     projection = p1 + t * line_unit
  77.    
  78.     # Distance from ball center to closest point
  79.     distance = np.linalg.norm(ball_pos - projection)
  80.    
  81.     if distance <= ball_radius:
  82.         # Collision detected
  83.         normal = normalize(ball_pos - projection)
  84.         return True, normal
  85.     return False, None
  86.  
  87. # Handle collision response
  88. def handle_collision(ball_pos, ball_velocity, normal):
  89.     # Reflect velocity across normal
  90.     v_dot_n = dot_product(ball_velocity, normal)
  91.     reflected_velocity = ball_velocity - 2 * v_dot_n * normal
  92.     # Apply elasticity (energy loss)
  93.     return reflected_velocity * elasticity
  94.  
  95. # Game loop
  96. clock = pygame.time.Clock()
  97. running = True
  98.  
  99. while running:
  100.     # Handle events
  101.     for event in pygame.event.get():
  102.         if event.type == pygame.QUIT:
  103.             running = False
  104.  
  105.     # Update hexagon rotation
  106.     hex_angle += hex_rotation_speed
  107.     hex_vertices = calculate_hex_vertices()
  108.     hex_sides = get_hex_sides(hex_vertices)
  109.  
  110.     # Apply gravity and friction
  111.     ball_velocity[1] += gravity
  112.     ball_velocity *= friction
  113.  
  114.     # Update ball position
  115.     ball_pos += ball_velocity
  116.  
  117.     # Check for collisions with hexagon walls
  118.     for p1, p2 in hex_sides:
  119.         collided, normal = check_collision(ball_pos, ball_radius, p1, p2)
  120.         if collided:
  121.             ball_velocity = handle_collision(ball_pos, ball_velocity, normal)
  122.             # Push ball out of wall to prevent sticking
  123.             penetration = ball_radius - np.linalg.norm(ball_pos - (p1 + (p2 - p1) *
  124.                 dot_product(ball_pos - p1, p2 - p1) / np.linalg.norm(p2 - p1)**2))
  125.             if penetration > 0:
  126.                 ball_pos += normal * penetration
  127.  
  128.     # Clear screen
  129.     screen.fill(BLACK)
  130.  
  131.     # Draw hexagon
  132.     pygame.draw.polygon(screen, WHITE, [(v[0], v[1]) for v in hex_vertices], 2)
  133.  
  134.     # Draw ball
  135.     pygame.draw.circle(screen, RED, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
  136.  
  137.     # Update display
  138.     pygame.display.flip()
  139.     clock.tick(60)
  140.  
  141. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment