Advertisement
Guest User

Untitled

a guest
Feb 25th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import math
  4.  
  5. # Initialize pygame
  6. pygame.init()
  7.  
  8. # Screen dimensions
  9. WIDTH, HEIGHT = 800, 800
  10. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  11. pygame.display.set_caption("Bouncing Ball in Rotating Square")
  12.  
  13. # Colors
  14. BLACK = (0, 0, 0)
  15. YELLOW = (255, 255, 0)
  16. BLUE = (0, 0, 255)
  17.  
  18. # Ball properties
  19. ball_radius = 20
  20. ball_pos = [WIDTH // 2, HEIGHT // 2]
  21. ball_velocity = [4, 3]
  22.  
  23. # Square properties
  24. square_size = 400
  25. square_center = [WIDTH // 2, HEIGHT // 2]
  26. square_angle = 0
  27. rotation_speed = 0.5 # degrees per frame
  28.  
  29. # Clock for controlling frame rate
  30. clock = pygame.time.Clock()
  31.  
  32. def rotate_point(point, center, angle_degrees):
  33. """Rotate a point around a center by a given angle in degrees."""
  34. angle_radians = math.radians(angle_degrees)
  35.  
  36. # Translate point to origin
  37. x = point[0] - center[0]
  38. y = point[1] - center[1]
  39.  
  40. # Rotate point
  41. x_rotated = x * math.cos(angle_radians) - y * math.sin(angle_radians)
  42. y_rotated = x * math.sin(angle_radians) + y * math.cos(angle_radians)
  43.  
  44. # Translate point back
  45. return [x_rotated + center[0], y_rotated + center[1]]
  46.  
  47. def get_square_vertices(center, size, angle):
  48. """Get the four vertices of the square after rotation."""
  49. half_size = size / 2
  50. vertices = [
  51. [center[0] - half_size, center[1] - half_size], # Top-left
  52. [center[0] + half_size, center[1] - half_size], # Top-right
  53. [center[0] + half_size, center[1] + half_size], # Bottom-right
  54. [center[0] - half_size, center[1] + half_size] # Bottom-left
  55. ]
  56.  
  57. # Rotate each vertex
  58. rotated_vertices = [rotate_point(vertex, center, angle) for vertex in vertices]
  59. return rotated_vertices
  60.  
  61. def is_ball_inside_square(ball_pos, vertices):
  62. """Check if the ball is inside the square using the point-in-polygon algorithm."""
  63. # Transform to local coordinates (un-rotate)
  64. local_pos = rotate_point(ball_pos, square_center, -square_angle)
  65.  
  66. # In local coordinates, we can just check against axis-aligned square
  67. half_size = square_size / 2
  68. return (abs(local_pos[0] - square_center[0]) < half_size - ball_radius and
  69. abs(local_pos[1] - square_center[1]) < half_size - ball_radius)
  70.  
  71. def handle_collision(ball_pos, ball_velocity, vertices):
  72. """Handle collision between ball and square edges."""
  73. # Transform to local coordinates (un-rotate)
  74. local_pos = rotate_point(ball_pos, square_center, -square_angle)
  75. local_vel = rotate_point([ball_velocity[0] + square_center[0],
  76. ball_velocity[1] + square_center[1]],
  77. square_center, -square_angle)
  78. local_vel = [local_vel[0] - square_center[0], local_vel[1] - square_center[1]]
  79.  
  80. half_size = square_size / 2
  81. collision = False
  82.  
  83. # Check collision with each edge in local coordinates
  84. if local_pos[0] - ball_radius < square_center[0] - half_size: # Left edge
  85. local_vel[0] = abs(local_vel[0])
  86. local_pos[0] = square_center[0] - half_size + ball_radius
  87. collision = True
  88. elif local_pos[0] + ball_radius > square_center[0] + half_size: # Right edge
  89. local_vel[0] = -abs(local_vel[0])
  90. local_pos[0] = square_center[0] + half_size - ball_radius
  91. collision = True
  92.  
  93. if local_pos[1] - ball_radius < square_center[1] - half_size: # Top edge
  94. local_vel[1] = abs(local_vel[1])
  95. local_pos[1] = square_center[1] - half_size + ball_radius
  96. collision = True
  97. elif local_pos[1] + ball_radius > square_center[1] + half_size: # Bottom edge
  98. local_vel[1] = -abs(local_vel[1])
  99. local_pos[1] = square_center[1] + half_size - ball_radius
  100. collision = True
  101.  
  102. if collision:
  103. # Transform back to global coordinates
  104. new_pos = rotate_point(local_pos, square_center, square_angle)
  105. new_vel = rotate_point([local_vel[0] + square_center[0],
  106. local_vel[1] + square_center[1]],
  107. square_center, square_angle)
  108. new_vel = [new_vel[0] - square_center[0], new_vel[1] - square_center[1]]
  109.  
  110. return new_pos, new_vel, True
  111.  
  112. return ball_pos, ball_velocity, False
  113.  
  114. # Main game loop
  115. running = True
  116. while running:
  117. for event in pygame.event.get():
  118. if event.type == pygame.QUIT:
  119. running = False
  120.  
  121. # Clear the screen
  122. screen.fill(BLACK)
  123.  
  124. # Update square rotation
  125. square_angle += rotation_speed
  126. if square_angle >= 360:
  127. square_angle -= 360
  128.  
  129. # Get square vertices
  130. vertices = get_square_vertices(square_center, square_size, square_angle)
  131.  
  132. # Update ball position
  133. ball_pos[0] += ball_velocity[0]
  134. ball_pos[1] += ball_velocity[1]
  135.  
  136. # Handle collision
  137. ball_pos, ball_velocity, collision = handle_collision(ball_pos, ball_velocity, vertices)
  138.  
  139. # Draw the square
  140. pygame.draw.polygon(screen, BLUE, vertices, 2)
  141.  
  142. # Draw the ball
  143. pygame.draw.circle(screen, YELLOW, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
  144.  
  145. # Update the display
  146. pygame.display.flip()
  147.  
  148. # Cap the frame rate
  149. clock.tick(60)
  150.  
  151. pygame.quit()
  152. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement