Advertisement
Guest User

Untitled

a guest
Jan 27th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import pygame
  2. import math
  3. from pygame.locals import *
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Set up display
  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. YELLOW = (255, 255, 0)
  15. BLACK = (0, 0, 0)
  16. WHITE = (255, 255, 255)
  17.  
  18. # Square parameters
  19. square_center = (width//2, height//2)
  20. square_size = int(min(width, height) * 0.7071) # Max size to fit rotated square (≈800/√2)
  21. half_square = square_size // 2
  22. rotation_angle = 0 # in radians
  23. rotation_speed = 0.01 # radians per frame
  24.  
  25. # Ball parameters
  26. ball_radius = 20
  27. ball_pos = [square_center[0], square_center[1]]
  28. ball_vel = [3, 3] # Initial velocity
  29.  
  30. # Main loop
  31. clock = pygame.time.Clock()
  32. running = True
  33.  
  34. while running:
  35. for event in pygame.event.get():
  36. if event.type == QUIT:
  37. running = False
  38.  
  39. # Update rotation angle
  40. rotation_angle += rotation_speed
  41.  
  42. # Move the ball
  43. ball_pos[0] += ball_vel[0]
  44. ball_pos[1] += ball_vel[1]
  45.  
  46. # Collision detection and response
  47. dx = ball_pos[0] - square_center[0]
  48. dy = ball_pos[1] - square_center[1]
  49.  
  50. cos_theta = math.cos(rotation_angle)
  51. sin_theta = math.sin(rotation_angle)
  52.  
  53. # Transform to local coordinates
  54. local_x = dx * cos_theta + dy * sin_theta
  55. local_y = -dx * sin_theta + dy * cos_theta
  56.  
  57. # Adjusted half size considering ball radius
  58. adjusted_half = half_square - ball_radius
  59. collision = False
  60.  
  61. # X-axis collision check
  62. if local_x > adjusted_half:
  63. local_x = adjusted_half
  64. collision = True
  65. elif local_x < -adjusted_half:
  66. local_x = -adjusted_half
  67. collision = True
  68.  
  69. # Y-axis collision check
  70. if local_y > adjusted_half:
  71. local_y = adjusted_half
  72. collision = True
  73. elif local_y < -adjusted_half:
  74. local_y = -adjusted_half
  75. collision = True
  76.  
  77. if collision:
  78. # Transform velocity to local coordinates
  79. vx_local = ball_vel[0] * cos_theta + ball_vel[1] * sin_theta
  80. vy_local = -ball_vel[0] * sin_theta + ball_vel[1] * cos_theta
  81.  
  82. # Reverse velocity components if collision occurred
  83. if abs(local_x) == adjusted_half:
  84. vx_local *= -1
  85. if abs(local_y) == adjusted_half:
  86. vy_local *= -1
  87.  
  88. # Transform velocity back to global coordinates
  89. ball_vel[0] = vx_local * cos_theta - vy_local * sin_theta
  90. ball_vel[1] = vx_local * sin_theta + vy_local * cos_theta
  91.  
  92. # Update position to stay within bounds
  93. dx_new = local_x * cos_theta - local_y * sin_theta
  94. dy_new = local_x * sin_theta + local_y * cos_theta
  95. ball_pos[0] = square_center[0] + dx_new
  96. ball_pos[1] = square_center[1] + dy_new
  97.  
  98. # Drawing
  99. screen.fill(BLACK)
  100.  
  101. # Draw rotating square
  102. half = square_size // 2
  103. corners = [
  104. (-half, -half),
  105. (-half, half),
  106. (half, half),
  107. (half, -half)
  108. ]
  109. rotated_corners = []
  110. for x, y in corners:
  111. rot_x = x * cos_theta - y * sin_theta
  112. rot_y = x * sin_theta + y * cos_theta
  113. rotated_corners.append((square_center[0] + rot_x, square_center[1] + rot_y))
  114. pygame.draw.polygon(screen, WHITE, rotated_corners, 2)
  115.  
  116. # Draw ball
  117. pygame.draw.circle(screen, YELLOW, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
  118.  
  119. pygame.display.flip()
  120. clock.tick(60)
  121.  
  122. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement