Advertisement
Guest User

Untitled

a guest
Jan 27th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 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 with Speed Controls")
  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) / math.sqrt(2)) # Critical calculation , patch #1: adjust square size to random window size
  21. half_square = square_size // 2
  22. rotation_angle = 0
  23. rotation_speed = 0.01
  24.  
  25. # Ball parameters
  26. ball_radius = 20
  27. ball_pos = [square_center[0], square_center[1]]
  28. ball_vel = [3.0, 3.0] # Use floats for smoother acceleration, ball velocity/speed
  29.  
  30. # Main loop
  31. clock = pygame.time.Clock()
  32. running = True
  33.  
  34. while running:
  35. # Handle events
  36. for event in pygame.event.get():
  37. if event.type == QUIT:
  38. running = False
  39.  
  40. # Get pressed keys state
  41. keys = pygame.key.get_pressed()
  42.  
  43. # Handle square rotation controls, #patch #2
  44. if keys[K_LEFT]:
  45. rotation_speed = -abs(rotation_speed) #Counter clock wise rotation
  46. if keys[K_RIGHT]:
  47. rotation_speed = abs(rotation_speed) #Clock wise rotation
  48. if keys[K_UP]:
  49. rotation_speed *= 1.02 # Smoother acceleration
  50. if keys[K_DOWN]:
  51. rotation_speed *= 0.98 # Smoother deceleration
  52.  
  53. # Handle ball speed controls
  54. if keys[K_a] or keys[K_s]:
  55. current_speed = math.hypot(ball_vel[0], ball_vel[1])
  56. if current_speed > 0:
  57. direction = (ball_vel[0] / current_speed, ball_vel[1] / current_speed)
  58. else:
  59. direction = (1, 0)
  60.  
  61. if keys[K_a]: # Accelerate
  62. current_speed += 0.5 # More noticeable per-frame change
  63. if keys[K_s]: # Decelerate
  64. current_speed = max(current_speed - 0.5, 0.5) # Minimum speed
  65.  
  66. ball_vel[0] = direction[0] * current_speed
  67. ball_vel[1] = direction[1] * current_speed
  68.  
  69. # Update rotation angle
  70. rotation_angle += rotation_speed
  71.  
  72.  
  73. # Move the ball
  74. ball_pos[0] += ball_vel[0]
  75. ball_pos[1] += ball_vel[1]
  76.  
  77. # Collision detection and response
  78. dx = ball_pos[0] - square_center[0]
  79. dy = ball_pos[1] - square_center[1]
  80.  
  81. cos_theta = math.cos(rotation_angle)
  82. sin_theta = math.sin(rotation_angle)
  83.  
  84. # Transform to local coordinates
  85. local_x = dx * cos_theta + dy * sin_theta
  86. local_y = -dx * sin_theta + dy * cos_theta
  87.  
  88. # Adjusted half size considering ball radius
  89. adjusted_half = half_square - ball_radius
  90. collision = False
  91.  
  92. # X-axis collision check
  93. if local_x > adjusted_half:
  94. local_x = adjusted_half
  95. collision = True
  96. elif local_x < -adjusted_half:
  97. local_x = -adjusted_half
  98. collision = True
  99.  
  100. # Y-axis collision check
  101. if local_y > adjusted_half:
  102. local_y = adjusted_half
  103. collision = True
  104. elif local_y < -adjusted_half:
  105. local_y = -adjusted_half
  106. collision = True
  107.  
  108. if collision:
  109. # Transform velocity to local coordinates
  110. vx_local = ball_vel[0] * cos_theta + ball_vel[1] * sin_theta
  111. vy_local = -ball_vel[0] * sin_theta + ball_vel[1] * cos_theta
  112.  
  113. # Reverse velocity components if collision occurred
  114. if abs(local_x) == adjusted_half:
  115. vx_local *= -1
  116. if abs(local_y) == adjusted_half:
  117. vy_local *= -1
  118.  
  119. # Transform velocity back to global coordinates
  120. ball_vel[0] = vx_local * cos_theta - vy_local * sin_theta
  121. ball_vel[1] = vx_local * sin_theta + vy_local * cos_theta
  122.  
  123. # Update position to stay within bounds
  124. dx_new = local_x * cos_theta - local_y * sin_theta
  125. dy_new = local_x * sin_theta + local_y * cos_theta
  126. ball_pos[0] = square_center[0] + dx_new
  127. ball_pos[1] = square_center[1] + dy_new
  128.  
  129. # Drawing
  130. screen.fill(BLACK)
  131.  
  132. # Draw rotating square
  133. half = square_size // 2
  134. corners = [
  135. (-half, -half),
  136. (-half, half),
  137. (half, half),
  138. (half, -half)
  139. ]
  140. rotated_corners = []
  141. for x, y in corners:
  142. rot_x = x * cos_theta - y * sin_theta
  143. rot_y = x * sin_theta + y * cos_theta
  144. rotated_corners.append((square_center[0] + rot_x, square_center[1] + rot_y))
  145. pygame.draw.polygon(screen, WHITE, rotated_corners, 2)
  146.  
  147. # Draw ball
  148. pygame.draw.circle(screen, YELLOW, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
  149.  
  150. pygame.display.flip()
  151. clock.tick(60)
  152.  
  153. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement