Advertisement
Guest User

Untitled

a guest
Jan 27th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. import pygame
  2. import math
  3. import sys
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Screen dimensions
  9. WIDTH, HEIGHT = 600, 600
  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. # Ball properties
  19. ball_radius = 20
  20. ball_pos = [WIDTH // 2, HEIGHT // 2]
  21. ball_velocity = [5, 5]
  22.  
  23. # Square properties
  24. square_size = 400
  25. square_angle = 0
  26. square_rotation_speed = 1 # Degrees per frame
  27.  
  28. # Clock for controlling frame rate
  29. clock = pygame.time.Clock()
  30.  
  31. def rotate_point(point, angle, center):
  32. """Rotate a point around a center by a given angle."""
  33. angle_rad = math.radians(angle)
  34. x = point[0] - center[0]
  35. y = point[1] - center[1]
  36. new_x = x * math.cos(angle_rad) - y * math.sin(angle_rad)
  37. new_y = x * math.sin(angle_rad) + y * math.cos(angle_rad)
  38. return [new_x + center[0], new_y + center[1]]
  39.  
  40. def get_rotated_square(center, size, angle):
  41. """Get the vertices of a rotated square."""
  42. half_size = size // 2
  43. corners = [
  44. [center[0] - half_size, center[1] - half_size],
  45. [center[0] + half_size, center[1] - half_size],
  46. [center[0] + half_size, center[1] + half_size],
  47. [center[0] - half_size, center[1] + half_size],
  48. ]
  49. return [rotate_point(corner, angle, center) for corner in corners]
  50.  
  51. def is_point_in_rotated_square(point, square_corners):
  52. """Check if a point is inside a rotated square."""
  53. def cross_product(a, b, c):
  54. return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
  55.  
  56. d1 = cross_product(square_corners[0], square_corners[1], point)
  57. d2 = cross_product(square_corners[1], square_corners[2], point)
  58. d3 = cross_product(square_corners[2], square_corners[3], point)
  59. d4 = cross_product(square_corners[3], square_corners[0], point)
  60.  
  61. return (d1 >= 0 and d2 >= 0 and d3 >= 0 and d4 >= 0) or (d1 <= 0 and d2 <= 0 and d3 <= 0 and d4 <= 0)
  62.  
  63. def bounce_ball(ball_pos, ball_velocity, square_corners):
  64. """Handle ball bouncing off the edges of the square."""
  65. if not is_point_in_rotated_square([ball_pos[0] + ball_velocity[0], ball_pos[1]], square_corners):
  66. ball_velocity[0] *= -1
  67. if not is_point_in_rotated_square([ball_pos[0], ball_pos[1] + ball_velocity[1]], square_corners):
  68. ball_velocity[1] *= -1
  69.  
  70. # Main loop
  71. running = True
  72. while running:
  73. for event in pygame.event.get():
  74. if event.type == pygame.QUIT:
  75. running = False
  76.  
  77. # Clear the screen
  78. screen.fill(BLACK)
  79.  
  80. # Rotate the square
  81. square_angle = (square_angle + square_rotation_speed) % 360
  82. square_corners = get_rotated_square([WIDTH // 2, HEIGHT // 2], square_size, square_angle)
  83.  
  84. # Draw the square
  85. pygame.draw.polygon(screen, WHITE, square_corners, 2)
  86.  
  87. # Update ball position and handle bouncing
  88. ball_pos[0] += ball_velocity[0]
  89. ball_pos[1] += ball_velocity[1]
  90. bounce_ball(ball_pos, ball_velocity, square_corners)
  91.  
  92. # Draw the ball
  93. pygame.draw.circle(screen, YELLOW, (int(ball_pos[0]), int(ball_pos[1])), ball_radius)
  94.  
  95. # Update the display
  96. pygame.display.flip()
  97.  
  98. # Cap the frame rate
  99. clock.tick(60)
  100.  
  101. # Quit Pygame
  102. pygame.quit()
  103. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement