Make bouncing ball simulation in Python using pygame, add squish physics to ball,…
- User
Make bouncing ball simulation in Python using pygame, add squish physics to ball, provide full code
- Assistant
Make bouncing ball simulation in Python using pygame, add squish physics to ball, provide full code
Here is a complete Python script that creates a bouncing ball simulation using Pygame, incorporating squish physics to make the ball deform upon impact with the ground. The deformation (squish) effect is proportional to the impact speed, and it smoothly transitions back to the original shape as the ball moves upward.
import pygame
import sys
import math
# Initialize Pygame
pygame.init()
# Set up the screen dimensions and create a window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Bouncing Ball with Squish Physics")
# Clock to control the frame rate
clock = pygame.time.Clock()
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Ball properties
ball_radius = 30 # Original radius of the ball
ball_x = screen_width // 2 # Horizontal position (centered)
ball_y = ball_radius # Starting vertical position at the top
ball_vel_y = 0 # Initial vertical velocity
gravity = 0.5 # Acceleration due to gravity
damping = 0.8 # Energy loss factor upon bouncing (0 < damping < 1)
# Squish physics variables
squishing = False # Indicates if the ball is currently squishing
squish_counter = 0 # Counter for the squish animation frames
squish_duration = 20 # Total frames for the squish effect
max_squish = 0 # Maximum squish factor (determined by impact speed)
# Main game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Apply gravity to the vertical velocity
ball_vel_y += gravity
# Update the ball's vertical position
ball_y += ball_vel_y
# Collision detection with the ground
if ball_y + ball_radius >= screen_height:
ball_y = screen_height - ball_radius # Position the ball on top of the ground
ball_vel_y = -ball_vel_y * damping # Reverse velocity and apply damping
# Start the squish effect
squishing = True
squish_counter = 0
# Determine the maximum squish based on the impact speed
impact_speed = abs(ball_vel_y)
max_squish = min(impact_speed / 20, 0.5) # Limit max squish to 0.5
# Handle the squish animation
if squishing:
squish_counter += 1
progress = squish_counter / squish_duration # Progress of the squish animation (0 to 1)
if progress < 1.0:
# Calculate the squish factor using a sine wave for smoothness
squish_factor = max_squish * math.sin(math.pi * progress)
# Adjust the ball's width and height to simulate squishing
ball_width = ball_radius * 2 * (1 + squish_factor)
ball_height = ball_radius * 2 * (1 - squish_factor)
else:
# Squish animation is over; reset to original dimensions
squishing = False
ball_width = ball_radius * 2
ball_height = ball_radius * 2
else:
# No squishing; maintain original dimensions
ball_width = ball_radius * 2
ball_height = ball_radius * 2
# Clear the screen
screen.fill(WHITE)
# Draw the ball with the current dimensions
ball_rect = pygame.Rect(0, 0, ball_width, ball_height)
ball_rect.center = (int(ball_x), int(ball_y))
pygame.draw.ellipse(screen, BLACK, ball_rect)
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(60)
# Quit Pygame and exit the program
pygame.quit()
sys.exit()
Explanation:
Physics Simulation:
ball_vel_y) is influenced by gravity (gravity), simulating acceleration due to gravity.ball_y + ball_radius >= screen_height), its velocity is inverted and reduced by the damping factor to simulate energy loss.Squish Effect:
max_squish) is proportional to the impact speed, creating a more pronounced squish for harder impacts.progress varies from 0 to 1).Drawing the Ball:
pygame.draw.ellipse) with the current ball_width and ball_height.ball_rect rectangle is recalculated each frame to position the ellipse correctly based on the ball's current position and dimensions.How to Run the Program:
Install Pygame:
Ensure that you have Pygame installed. You can install it using pip:
pip install pygame
Save the Script:
Save the above code in a file named, for example, bouncing_ball.py.
Run the Script:
Run the script using Python:
python bouncing_ball.py
Enjoy the Simulation!
When you run the program, a window will open displaying a ball that falls under gravity, bounces off the ground, and squishes upon each impact. The squish effect will be more pronounced if you increase the gravity or the initial height, causing the ball to fall faster and hit the ground harder.
Feel free to experiment with the parameters like gravity, damping, squish_duration, and others to see how they affect the simulation.
Not yet included in a published release.