Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- # Initialize Pygame
- pygame.init()
- # Window dimensions (4 inches at 96 DPI)
- WINDOW_SIZE = 384 # 4 inches * 96 DPI
- window = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
- pygame.display.set_caption("Rotating Triangle with Ball")
- # Colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- # Triangle dimensions (2 inches at 96 DPI)
- TRI_SIZE = 192 # 2 inches * 96 DPI
- TRI_CENTER = (WINDOW_SIZE // 2, WINDOW_SIZE // 2)
- # Ball dimensions (1/8 inch at 96 DPI)
- BALL_RADIUS = 12 # (1/8 inch) * 96 DPI / 2
- BALL_SPEED = 5 # Configurable speed
- # Triangle vertices (equilateral)
- vertices = [
- (TRI_CENTER[0], TRI_CENTER[1] - TRI_SIZE),
- (TRI_CENTER[0] - TRI_SIZE / 2, TRI_CENTER[1] + TRI_SIZE / 2),
- (TRI_CENTER[0] + TRI_SIZE / 2, TRI_CENTER[1] + TRI_SIZE / 2)
- ]
- # Ball properties
- ball_pos = [TRI_CENTER[0], TRI_CENTER[1]]
- ball_vel = [BALL_SPEED, BALL_SPEED]
- # Rotation properties
- rotation_angle = 0
- rotation_speed = 6 # Degrees per second
- # Clock for timing
- clock = pygame.time.Clock()
- # Game loop
- running = True
- while running:
- # Handle events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Calculate time delta
- dt = clock.tick(60) / 1000.0 # Convert to seconds
- # Update rotation angle
- rotation_angle += rotation_speed * dt
- if rotation_angle >= 360:
- rotation_angle -= 360
- # Rotate triangle vertices
- rotated_vertices = []
- for vertex in vertices:
- # Translate to origin
- x, y = vertex[0] - TRI_CENTER[0], vertex[1] - TRI_CENTER[1]
- # Rotate
- new_x = x * math.cos(math.radians(rotation_angle)) + y * math.sin(math.radians(rotation_angle))
- new_y = -x * math.sin(math.radians(rotation_angle)) + y * math.cos(math.radians(rotation_angle))
- # Translate back
- new_x += TRI_CENTER[0]
- new_y += TRI_CENTER[1]
- rotated_vertices.append((new_x, new_y))
- # Update ball position
- ball_pos[0] += ball_vel[0] * dt
- ball_pos[1] += ball_vel[1] * dt
- # Check collision with triangle edges
- for i in range(3):
- p1 = rotated_vertices[i]
- p2 = rotated_vertices[(i+1) % 3]
- # Line equation: Ax + By + C = 0
- A = p2[1] - p1[1]
- B = p1[0] - p2[0]
- C = p2[0] * p1[1] - p1[0] * p2[1]
- # Distance from ball to line
- distance = (A * ball_pos[0] + B * ball_pos[1] + C) / math.sqrt(A**2 + B**2)
- if abs(distance) < BALL_RADIUS:
- # Calculate reflection
- normal = (A, B)
- norm_length = math.sqrt(A**2 + B**2)
- if norm_length == 0:
- continue
- normal = (A / norm_length, B / norm_length)
- dot_product = ball_vel[0] * normal[0] + ball_vel[1] * normal[1]
- ball_vel[0] -= 2 * dot_product * normal[0]
- ball_vel[1] -= 2 * dot_product * normal[1]
- # Adjust position to prevent sticking
- ball_pos[0] -= normal[0] * (abs(distance) - BALL_RADIUS)
- ball_pos[1] -= normal[1] * (abs(distance) - BALL_RADIUS)
- # Keep ball within window (just in case)
- if ball_pos[0] < 0 or ball_pos[0] > WINDOW_SIZE:
- ball_vel[0] *= -1
- if ball_pos[1] < 0 or ball_pos[1] > WINDOW_SIZE:
- ball_vel[1] *= -1
- # Clear screen
- window.fill(BLACK)
- # Draw rotated triangle
- pygame.draw.lines(window, WHITE, True, rotated_vertices, 2)
- # Draw ball
- pygame.draw.circle(window, RED, (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)
- # Update display
- pygame.display.flip()
- # Quit Pygame
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement