Guest User

grok3-x

a guest
Feb 18th, 2025
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 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. # Set up the display
  9. WIDTH = 800
  10. HEIGHT = 600
  11. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  12. pygame.display.set_caption("Ball in Spinning Hexagon")
  13.  
  14. # Colors
  15. WHITE = (255, 255, 255)
  16. RED = (255, 0, 0)
  17. BLACK = (0, 0, 0)
  18.  
  19. # Ball properties
  20. ball_radius = 15
  21. ball_pos = np.array([WIDTH/2, HEIGHT/2], dtype=float)
  22. ball_vel = np.array([3.0, -5.0], dtype=float)
  23. GRAVITY = 0.2
  24. FRICTION = 0.99
  25.  
  26. # Hexagon properties
  27. hex_radius = 200
  28. hex_center = (WIDTH/2, HEIGHT/2)
  29. rotation_angle = 0
  30. rotation_speed = 0.02  # radians per frame
  31.  
  32. def get_hexagon_points(center, radius, angle):
  33.     """Calculate vertices of a rotating hexagon"""
  34.     points = []
  35.     for i in range(6):
  36.         vertex_angle = angle + i * math.pi / 3
  37.         x = center[0] + radius * math.cos(vertex_angle)
  38.         y = center[1] + radius * math.sin(vertex_angle)
  39.         points.append((x, y))
  40.     return points
  41.  
  42. def line_intersection(p1, p2, p3, p4):
  43.     """Find intersection point between two lines if it exists"""
  44.     x1, y1 = p1
  45.     x2, y2 = p2
  46.     x3, y3 = p3
  47.     x4, y4 = p4
  48.    
  49.     denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
  50.     if denom == 0:  # Lines are parallel
  51.         return None
  52.    
  53.     t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom
  54.     u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom
  55.    
  56.     if 0 <= t <= 1 and 0 <= u <= 1:
  57.         x = x1 + t * (x2 - x1)
  58.         y = y1 + t * (y2 - y1)
  59.         return (x, y)
  60.     return None
  61.  
  62. def reflect_velocity(vel, normal):
  63.     """Calculate reflection of velocity vector off a surface"""
  64.     normal = np.array(normal) / np.linalg.norm(normal)
  65.     return vel - 2 * np.dot(vel, normal) * normal
  66.  
  67. # Game loop
  68. clock = pygame.time.Clock()
  69. running = True
  70.  
  71. while running:
  72.     # Event handling
  73.     for event in pygame.event.get():
  74.         if event.type == pygame.QUIT:
  75.             running = False
  76.    
  77.     # Clear screen
  78.     screen.fill(BLACK)
  79.    
  80.     # Update rotation
  81.     rotation_angle += rotation_speed
  82.    
  83.     # Get current hexagon vertices
  84.     hex_points = get_hexagon_points(hex_center, hex_radius, rotation_angle)
  85.    
  86.     # Apply gravity and friction
  87.     ball_vel[1] += GRAVITY
  88.     ball_vel *= FRICTION
  89.    
  90.     # Store previous position
  91.     prev_pos = ball_pos.copy()
  92.    
  93.     # Update ball position
  94.     ball_pos += ball_vel
  95.    
  96.     # Check collisions with hexagon walls
  97.     for i in range(6):
  98.         p1 = hex_points[i]
  99.         p2 = hex_points[(i + 1) % 6]
  100.        
  101.         # Check if ball path intersects with wall
  102.         intersection = line_intersection(
  103.             (prev_pos[0], prev_pos[1]),
  104.             (ball_pos[0], ball_pos[1]),
  105.             p1, p2
  106.         )
  107.        
  108.         if intersection:
  109.             # Move ball back to intersection point
  110.             ball_pos = np.array(intersection)
  111.            
  112.             # Calculate wall normal
  113.             wall_vec = np.array([p2[0] - p1[0], p2[1] - p1[1]])
  114.             normal = np.array([-wall_vec[1], wall_vec[0]])
  115.            
  116.             # Reflect velocity
  117.             ball_vel = reflect_velocity(ball_vel, normal) * 0.8  # 0.8 is elasticity
  118.            
  119.             # Prevent sticking
  120.             ball_pos += ball_vel * 0.1
  121.             break
  122.    
  123.     # Draw hexagon
  124.     pygame.draw.polygon(screen, WHITE, hex_points, 2)
  125.    
  126.     # Draw ball
  127.     pygame.draw.circle(screen, RED, ball_pos.astype(int), ball_radius)
  128.    
  129.     # Update display
  130.     pygame.display.flip()
  131.    
  132.     # Control frame rate
  133.     clock.tick(60)
  134.  
  135. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment