i am sorry i dont have the screentogif around, otherwise i’ll post a moving gif instead.
btw, here is the working code on day 1,
completely by c3.7+think.
import pygame
import sys
import math
# Initialize Pygame
pygame.init()
# Define constants
WIDTH, HEIGHT = 500, 500
SQUARE_SIZE = 300
BALL_RADIUS = 10
GRAVITY = 300 # pixels per second squared
ROTATION_SPEED = 60 # degrees per second
FPS = 60
# Create window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rotating Square with Bouncing Ball")
clock = pygame.time.Clock()
# Ball properties
ball_pos = [WIDTH // 2, HEIGHT // 2] # Start at center
ball_vel = [0, 0] # Initial velocity is zero
# Square properties
square_angle = 0 # Initial rotation angle
square_center = (WIDTH // 2, HEIGHT // 2)
def rotate_vector(vector, angle_degrees):
"""Rotate a vector by the given angle in degrees."""
angle_rad = math.radians(angle_degrees)
x, y = vector
# Apply rotation
rotated_x = x * math.cos(angle_rad) - y * math.sin(angle_rad)
rotated_y = x * math.sin(angle_rad) + y * math.cos(angle_rad)
return [rotated_x, rotated_y]
def get_square_vertices(angle):
"""Return the vertices of the square after rotation."""
half_size = SQUARE_SIZE // 2
# Start with unrotated square vertices relative to center
vertices = [
(-half_size, -half_size),
(half_size, -half_size),
(half_size, half_size),
(-half_size, half_size)
]
# Rotate vertices and translate to screen coordinates
rotated = []
for x, y in vertices:
rotated_point = rotate_vector([x, y], angle)
rotated.append((rotated_point[0] + square_center[0], rotated_point[1] + square_center[1]))
return rotated
def handle_collision(pos, vel, angle):
"""Handle collision between the ball and the square."""
# Translate to square's center
rel_pos = [pos[0] - square_center[0], pos[1] - square_center[1]]
# Rotate position and velocity to square's reference frame
rel_pos = rotate_vector(rel_pos, -angle)
rel_vel = rotate_vector(vel, -angle)
half_size = SQUARE_SIZE // 2
collision = False
# Check collision with axis-aligned square in rotated space
# Left wall
if rel_pos[0] - BALL_RADIUS < -half_size:
rel_pos[0] = -half_size + BALL_RADIUS
rel_vel[0] = -rel_vel[0] * 0.9 # Add some damping
collision = True
# Right wall
if rel_pos[0] + BALL_RADIUS > half_size:
rel_pos[0] = half_size - BALL_RADIUS
rel_vel[0] = -rel_vel[0] * 0.9
collision = True
# Top wall
if rel_pos[1] - BALL_RADIUS < -half_size:
rel_pos[1] = -half_size + BALL_RADIUS
rel_vel[1] = -rel_vel[1] * 0.9
collision = True
# Bottom wall
if rel_pos[1] + BALL_RADIUS > half_size:
rel_pos[1] = half_size - BALL_RADIUS
rel_vel[1] = -rel_vel[1] * 0.9
collision = True
# Rotate back to world space
rel_pos = rotate_vector(rel_pos, angle)
rel_vel = rotate_vector(rel_vel, angle)
# Translate back from square's center
new_pos = [rel_pos[0] + square_center[0], rel_pos[1] + square_center[1]]
return new_pos, rel_vel, collision
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Calculate time since last frame
dt = clock.tick(FPS) / 1000.0 # Convert to seconds
# Update square rotation
square_angle += ROTATION_SPEED * dt
if square_angle >= 360:
square_angle -= 360
# Apply gravity to ball in world coordinates
ball_vel[1] += GRAVITY * dt
# Update ball position
new_ball_pos = [ball_pos[0] + ball_vel[0] * dt, ball_pos[1] + ball_vel[1] * dt]
# Check for collision with rotated square
new_ball_pos, ball_vel, collision = handle_collision(new_ball_pos, ball_vel, square_angle)
ball_pos = new_ball_pos
# Draw everything
screen.fill((0, 0, 0)) # Black background
# Draw rotated square
square_vertices = get_square_vertices(square_angle)
pygame.draw.polygon(screen, (255, 255, 255), square_vertices)
# Draw ball
pygame.draw.circle(screen, (255, 0, 0), (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)
# Update display
pygame.display.flip()
pygame.quit()
sys.exit()
today’s code if you like
import pygame
import math
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 500, 500
SQUARE_SIZE = 300
BALL_RADIUS = 10
GRAVITY = 300 # pixels per second squared
FPS = 60
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rotating Square with Bouncing Ball")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Ball properties
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_velocity = [0, 0]
# Time variables
clock = pygame.time.Clock()
angle = 0 # Initial angle for rotation
delta_time = 0
# Main loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Calculate delta time
delta_time = clock.tick(FPS) / 1000.0 # seconds since last frame
# Update angle for rotation
angle += 60 * delta_time # Rotate 60 degrees per second
if angle >= 360:
angle -= 360
# Update ball position
ball_velocity[1] += GRAVITY * delta_time # Apply gravity
ball_pos[0] += ball_velocity[0] * delta_time
ball_pos[1] += ball_velocity[1] * delta_time
# Check for collision with walls
if ball_pos[0] - BALL_RADIUS < (WIDTH - SQUARE_SIZE) / 2 or ball_pos[0] + BALL_RADIUS > (WIDTH + SQUARE_SIZE) / 2:
ball_velocity[0] = -ball_velocity[0] # Reverse horizontal velocity
ball_pos[0] = max(BALL_RADIUS, min(ball_pos[0], WIDTH - BALL_RADIUS)) # Keep ball within bounds
if ball_pos[1] - BALL_RADIUS < (HEIGHT - SQUARE_SIZE) / 2 or ball_pos[1] + BALL_RADIUS > (HEIGHT + SQUARE_SIZE) / 2:
ball_velocity[1] = -ball_velocity[1] # Reverse vertical velocity
ball_pos[1] = max(BALL_RADIUS, min(ball_pos[1], HEIGHT - BALL_RADIUS)) # Keep ball within bounds
# Clear the screen
screen.fill(BLACK)
# Calculate the square's corners
half_size = SQUARE_SIZE / 2
center_x, center_y = WIDTH // 2, HEIGHT // 2
corners = [
(center_x + half_size * math.cos(math.radians(angle + 45)), center_y + half_size * math.sin(math.radians(angle + 45))),
(center_x + half_size * math.cos(math.radians(angle + 135)), center_y + half_size * math.sin(math.radians(angle + 135))),
(center_x + half_size * math.cos(math.radians(angle + 225)), center_y + half_size * math.sin(math.radians(angle + 225))),
(center_x + half_size * math.cos(math.radians(angle + 315)), center_y + half_size * math.sin(math.radians(angle + 315))),
]
# Draw the rotating square
pygame.draw.polygon(screen, WHITE, corners)
# Draw the ball
pygame.draw.circle(screen, RED, (int(ball_pos[0]), int(ball_pos[1])), BALL_RADIUS)
# Update the display
pygame.display.flip()
# Quit Pygame
pygame.quit()