- Pythonic Bounce
- Posts
- Create Own Bouncing Ball Code with ChatGPT + Code
Create Own Bouncing Ball Code with ChatGPT + Code
Hey Creators! 👋
Ever wanted to create those satisfying bouncing ball videos that everyone’s obsessed with? Today, we’re showing you how to code your own simulation with ChatGPT, then capture, edit, and post it on TikTok, YouTube Shorts, and Instagram Reels! 🌐 From coding basics to hitting those monetization milestones, we’ve got you covered. Let’s dive in!
Step 1: Generate the Code with ChatGPT 💻✨
You don’t need to be a coding whiz to create a simulation like this! Here’s how to let ChatGPT do the heavy lifting:
Ask ChatGPT: Open ChatGPT and type something like, “I want to create a bouncing ball simulation using Pygame. Can you help with the code?” 🎢 Add fun features like color transitions, sound effects, or gravity effects!
Customize the Code: ChatGPT can help you add:
Gravity & Physics 🌍: For realistic bounces.
Color Transitions 🌈: Give each bounce a unique hue!
Velocity Boost ⚡: For higher-energy bounces as the ball moves!
Randomized Sound Effects 🎶: Every bounce triggers a musical note!
Save the Code: Copy ChatGPT’s code, paste it into an editor like VS Code, and save it as a
.pyfile. Done! ✅
Step 2: Run & Screen Record with OBS Studio 🎥
Ready to capture the action? Here’s how to run and record your simulation!
Run the Code: Open a terminal and type
python yourfile.py. Watch your ball bounce around! 🔴⚪🟢Capture with OBS: In OBS Studio, create a Display Capture for the simulation window, set your frame rate, and hit Start Recording. Get ready to go viral! 🌐
Step 3: Edit Like a Pro in CapCut ✂️📲
Time to make it pop!
Upload Your Video: Open CapCut, start a New Project 📂, and import your recording.
Trim & Focus: Cut out everything except the sim—make sure it’s only the ball in action! ⚽
Add Text & Effects: Text like “Can you guess how many times it’ll bounce? 👀” will keep viewers hooked.
Save & Export: Export in vertical format for TikTok, YouTube Shorts, and Instagram Reels. 🚀
Step 4: Post & Monetize 🤑
Ready to hit post? Here’s how to make sure your simulation stands out:
TikTok: Use engaging captions and trending sounds to boost visibility! 🎶
YouTube Shorts: Add keywords and tags to reach the right audience! 📈
Instagram Reels: Play with trending hashtags like #satisfying or #viralvideo. 🌟
Bonus: Keep posting consistently, and you’ll reach monetization requirements in no time—turning views into $$! 💸
Free Code!!! ⬇️
import pygame
import math
import random
bounces = 0
# Initialize pygame
pygame.init()
pygame.mixer.set_num_channels(500) # Increase the number of available channels
# Screen dimensions
WIDTH, HEIGHT = 1080, 1200
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Circle Physics Simulation")
# Colors of the rainbow
RAINBOW_COLORS = [(255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 128, 0), (0, 255, 0),
(0, 255, 255), (0, 0, 255), (75, 0, 130), (148, 0, 211)] # Add more colors as needed
# Ball settings
initial_ball_radius = 450
ball_radius = initial_ball_radius
ball_center = [WIDTH // 2, HEIGHT // 2]
ball_velocity = [6, 7] # Initial velocity
ball_mass = 5
elasticity = 1.0 # Elasticity factor (no energy lost in collision)
velocity_increase_factor = 1.0142 # Increase factor for velocity after each bounce
# Circle settings
circle_center = [WIDTH // 2, HEIGHT // 2]
circle_radius = min(WIDTH, HEIGHT) // 2 - 50 # Adjusted to be 50px from the edge
# Gravity
gravity = 0.7
# Trace settings
traces = []
max_traces = 20 # Maximum number of traces allowed
traces_per_frame = 7 # Number of traces added per frame
trace_opacity_decay = 3 # Opacity decay factor for the traces
# Color transition settings
transition_speed = 0.003 # Adjust the speed of color transition
note_sequence = [
'C4', 'C4', 'C4', 'C4', 'C4', 'Cs4', 'F4', 'C4', 'C4', 'C4', 'Cs4', 'F4', 'C4', 'C4',
'C4', 'Cs4', 'F4', 'G4', 'G4', 'As4', 'Gs4', 'G4', 'F4', 'F4', 'As4', 'Gs4', 'G4', 'F4',
'F4', 'C4', 'Cs4', 'F4', 'C4', 'C4', 'C4', 'Cs4', 'F4', 'C4', 'C4']
# Load sounds
sounds = [pygame.mixer.Sound(f"tokyo/{note}.wav") for note in note_sequence]
next_note_index = 0 # Index to keep track of the next note to play
# Keep track of the channels
channels = [pygame.mixer.Channel(i) for i in range(pygame.mixer.get_num_channels())]
next_channel_index = 0 # Index to keep track of the next channel to use
# List to keep track of additional bouncing balls
bouncing_balls = []
# List to store collision points
collision_points = []
# Clock to control the frame rate
clock = pygame.time.Clock()
# Font settings
font = pygame.font.Font(None, 36)
# Function to smoothly transition between colors
def transition_color(phase, speed):
color_index = phase * (len(RAINBOW_COLORS) - 1)
color1 = RAINBOW_COLORS[int(color_index) % len(RAINBOW_COLORS)]
color2 = RAINBOW_COLORS[(int(color_index) + 1) % len(RAINBOW_COLORS)]
factor = color_index % 1
new_color = [int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3)]
return new_color
# Main game loop
running = True
color_phase = 0
bounce_counter = 0
last_bounce_time = pygame.time.get_ticks() # Initialize last bounce time
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Apply gravity
ball_velocity[1] += gravity
# Update ball position
ball_center[0] += ball_velocity[0]
ball_center[1] += ball_velocity[1]
# Check for collision with walls
if ball_center[0] <= ball_radius:
ball_center[0] = ball_radius
ball_velocity[0] *= -elasticity # Reverse direction
ball_velocity[0] *= velocity_increase_factor # Increase velocity upon collision
bounce_counter += 1
elif ball_center[0] >= WIDTH - ball_radius:
ball_center[0] = WIDTH - ball_radius
ball_velocity[0] *= -elasticity # Reverse direction
ball_velocity[0] *= velocity_increase_factor # Increase velocity upon collision
bounce_counter += 1
if ball_center[1] <= ball_radius:
ball_center[1] = ball_radius
ball_velocity[1] *= -elasticity # Reverse direction
ball_velocity[1] *= velocity_increase_factor # Increase velocity upon collision
bounce_counter += 1
elif ball_center[1] >= HEIGHT - ball_radius:
ball_center[1] = HEIGHT - ball_radius
ball_velocity[1] *= -elasticity # Reverse direction
ball_velocity[1] *= velocity_increase_factor # Increase velocity upon collision
bounce_counter += 1
# Check for collision with circle
distance_from_center = (ball_center[0] - circle_center[0]) ** 2 + (ball_center[1] - circle_center[1]) ** 2
if distance_from_center >= (circle_radius - ball_radius) ** 2:
# Calculate normal vector
bounces += 1
normal = [ball_center[0] - circle_center[0], ball_center[1] - circle_center[1]]
length = math.sqrt(normal[0] ** 2 + normal[1] ** 2)
normal = [n / length for n in normal]
# Calculate dot product
dot_product = ball_velocity[0] * normal[0] + ball_velocity[1] * normal[1]
# Calculate reflection
reflection = [ball_velocity[i] - 2 * dot_product * normal[i] for i in range(2)]
# Update velocity with reflection
ball_velocity[0] = reflection[0]
ball_velocity[1] = reflection[1]
# Increase ball size by 3% each bounce, up to a maximum of 250px radius
ball_radius *= 0.990
# Ensure ball doesn't get stuck on the edge of the circle
angle = math.atan2(ball_center[1] - circle_center[1], ball_center[0] - circle_center[0])
offset = 2 # Offset to move ball away from the edge
ball_center[0] = circle_center[0] + (circle_radius - ball_radius - offset) * math.cos(angle)
ball_center[1] = circle_center[1] + (circle_radius - ball_radius - offset) * math.sin(angle)
# Store collision point
collision_points.append((circle_center[0] + (circle_radius * math.cos(angle)),
circle_center[1] + (circle_radius * math.sin(angle))))
# Play sound on a separate channel
channel = channels[next_channel_index]
if not channel.get_busy():
channel.play(sounds[next_note_index])
next_channel_index = (next_channel_index + 1) % len(channels)
next_note_index = (next_note_index + 1) % len(note_sequence)
# Increase velocity upon collision with circle, unless radius exceeds 250px
ball_velocity[0] *= velocity_increase_factor
ball_velocity[1] *= velocity_increase_factor
# Increment bounce counter
bounce_counter += 1
# Transition ball color smoothly
ball_color = transition_color(color_phase, transition_speed)
# Add multiple traces if inside the circle perimeter
for _ in range(traces_per_frame):
trace_color_with_alpha = tuple(list(ball_color) + [random.randint(50, 200)]) # Trace color matches ball with varying alpha
max_trace_radius = min(ball_radius, circle_radius) # Maximum allowed trace radius
trace_radius = random.uniform(0.5, 1.5) * max_trace_radius # Vary trace size within limits
traces.append({'position': ball_center.copy(), 'color': trace_color_with_alpha, 'radius': min(trace_radius, max_trace_radius)})
# Limit the number of traces
if len(traces) > max_traces:
traces.pop(0) # Remove the oldest trace
# Transition circle outline color smoothly
circle_outline_color = transition_color(color_phase, transition_speed)
# Draw the small circle within the main circle
screen.fill((0, 0, 0)) # Fill the screen with black background
pygame.draw.circle(screen, (0, 0, 0), circle_center, circle_radius) # Black inner color
pygame.draw.circle(screen, circle_outline_color, circle_center, circle_radius + 20, 20) # Circle outline with smooth color transition
# Draw traces
for trace in traces:
trace_alpha = max(0, trace['color'][3] - trace_opacity_decay) # Decrease alpha
trace_alpha *= 0.2 # Reduce opacity to 70%
trace_color_with_alpha = tuple(list(trace['color'][:3]) + [int(trace_alpha)]) # Update color with new alpha
trace_position = (int(trace['position'][0]), int(trace['position'][1]))
trace_radius = int(trace['radius'])
# Draw white outline
pygame.draw.circle(screen, (255, 255, 255), trace_position, trace_radius + 2)
# Draw trace
pygame.draw.circle(screen, trace_color_with_alpha, trace_position, trace_radius)
# Draw lines from collision points to the current ball position
for point in collision_points:
pygame.draw.line(screen, (ball_color), point, ball_center, 2)
pygame.draw.circle(screen, ball_color, ball_center, int(ball_radius))
pygame.draw.circle(screen, (255, 255, 255), ball_center, int(ball_radius), 2) # Bouncing ball outline with smooth color transition
font = pygame.font.Font(None, 36)
text = font.render(f"Bounces: {bounces}", True, (255, 255, 255))
screen.blit(text, (WIDTH/2-65, HEIGHT-50))
pygame.display.flip()
clock.tick(120)
# Update color phase
color_phase += transition_speed
pygame.quit()