Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2025
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Window dimensions (4 inches at 96 DPI)
  8. WINDOW_SIZE = 384  # 4 inches * 96 DPI
  9. window = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
  10. pygame.display.set_caption("Rotating Triangle with Ball")
  11.  
  12. # Colors
  13. BLACK = (0, 0, 0)
  14. WHITE = (255, 255, 255)
  15. RED = (255, 0, 0)
  16.  
  17. # Triangle dimensions (2 inches at 96 DPI)
  18. TRI_SIZE = 192  # 2 inches * 96 DPI
  19. TRI_CENTER = (WINDOW_SIZE // 2, WINDOW_SIZE // 2)
  20.  
  21. # Ball dimensions (1/8 inch at 96 DPI)
  22. BALL_RADIUS = 12  # (1/8 inch) * 96 DPI / 2
  23. BALL_SPEED = 5  # Configurable speed
  24.  
  25. # Triangle vertices (equilateral)
  26. vertices = [
  27.     (TRI_CENTER[0], TRI_CENTER[1] - TRI_SIZE),
  28.     (TRI_CENTER[0] - TRI_SIZE / 2, TRI_CENTER[1] + TRI_SIZE / 2),
  29.     (TRI_CENTER[0] + TRI_SIZE / 2, TRI_CENTER[1] + TRI_SIZE / 2)
  30. ]
  31.  
  32. # Ball properties
  33. ball_pos = [TRI_CENTER[0], TRI_CENTER[1]]
  34. ball_vel = [BALL_SPEED, BALL_SPEED]
  35.  
  36. # Rotation properties
  37. rotation_angle = 0
  38. rotation_speed = 6  # Degrees per second
  39.  
  40. # Clock for timing
  41. clock = pygame.time.Clock()
  42.  
  43. # Game loop
  44. running = True
  45. while running:
  46.     # Handle events
  47.     for event in pygame.event.get():
  48.         if event.type == pygame.QUIT:
  49.             running = False
  50.  
  51.     # Calculate time delta
  52.     dt = clock.tick(60) / 1000.0  # Convert to seconds
  53.  
  54.     # Update rotation angle
  55.     rotation_angle += rotation_speed * dt
  56.     if rotation_angle >= 360:
  57.         rotation_angle -= 360
  58.  
  59.     # Rotate triangle vertices
  60.     rotated_vertices = []
  61.     for vertex in vertices:
  62.         # Translate to origin
  63.         x, y = vertex[0] - TRI_CENTER[0], vertex[1] - TRI_CENTER[1]
  64.         # Rotate
  65.         new_x = x * math.cos(math.radians(rotation_angle)) + y * math.sin(math.radians(rotation_angle))
  66.         new_y = -x * math.sin(math.radians(rotation_angle)) + y * math.cos(math.radians(rotation_angle))
  67.         # Translate back
  68.         new_x += TRI_CENTER[0]
  69.         new_y += TRI_CENTER[1]
  70.         rotated_vertices.append((new_x, new_y))
  71.  
  72.     # Update ball position
  73.     ball_pos[0] += ball_vel[0] * dt
  74.     ball_pos[1] += ball_vel[1] * dt
  75.  
  76.     # Check collision with triangle edges
  77.     for i in range(3):
  78.         p1 = rotated_vertices[i]
  79.         p2 = rotated_vertices[(i+1) % 3]
  80.  
  81.         # Line equation: Ax + By + C = 0
  82.         A = p2[1] - p1[1]
  83.         B = p1[0] - p2[0]
  84.         C = p2[0] * p1[1] - p1[0] * p2[1]
  85.  
  86.         # Distance from ball to line
  87.         distance = (A * ball_pos[0] + B * ball_pos[1] + C) / math.sqrt(A**2 + B**2)
  88.         if abs(distance) < BALL_RADIUS:
  89.             # Calculate reflection
  90.             normal = (A, B)
  91.             norm_length = math.sqrt(A**2 + B**2)
  92.             if norm_length == 0:
  93.                 continue
  94.             normal = (A / norm_length, B / norm_length)
  95.             dot_product = ball_vel[0] * normal[0] + ball_vel[1] * normal[1]
  96.             ball_vel[0] -= 2 * dot_product * normal[0]
  97.             ball_vel[1] -= 2 * dot_product * normal[1]
  98.  
  99.             # Adjust position to prevent sticking
  100.             ball_pos[0] -= normal[0] * (abs(distance) - BALL_RADIUS)
  101.             ball_pos[1] -= normal[1] * (abs(distance) - BALL_RADIUS)
  102.  
  103.     # Keep ball within window (just in case)
  104.     if ball_pos[0] < 0 or ball_pos[0] > WINDOW_SIZE:
  105.         ball_vel[0] *= -1
  106.     if ball_pos[1] < 0 or ball_pos[1] > WINDOW_SIZE:
  107.         ball_vel[1] *= -1
  108.  
  109.     # Clear screen
  110.     window.fill(BLACK)
  111.  
  112.     # Draw rotated triangle
  113.     pygame.draw.lines(window, WHITE, True, rotated_vertices, 2)
  114.  
  115.     # Draw ball
  116.     pygame.draw.circle(window, RED, (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)
  117.  
  118.     # Update display
  119.     pygame.display.flip()
  120.  
  121. # Quit Pygame
  122. pygame.quit()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement