Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created February 25, 2023 00:19
Show Gist options
  • Save stephengruppetta/0b7e400f6ee84da93bfad4b6e14c7d55 to your computer and use it in GitHub Desktop.
Save stephengruppetta/0b7e400f6ee84da93bfad4b6e14c7d55 to your computer and use it in GitHub Desktop.
# juggling_balls_game.py
import turtle
import time
import random
from juggling_balls import Ball
# Game parameters
WIDTH = 600
HEIGHT = 600
frame_rate = 60
batting_tolerance = 40
spawn_interval_range = (1, 5)
balls_lost_limit = 10
# Setup window
window = turtle.Screen()
window.setup(WIDTH, HEIGHT)
window.bgcolor(0.15, 0.15, 0.15)
window.tracer(0)
# Batting function
def bat_up(x, y):
for ball in balls:
if ball.distance(x, y) < batting_tolerance:
ball.bat_up()
window.onclick(bat_up)
balls = []
# Game loop
game_timer = time.time()
spawn_timer = time.time()
spawn_interval = 0
balls_lost = 0
while balls_lost < balls_lost_limit:
frame_start_time = time.time()
# Spawn new ball
if time.time() - spawn_timer > spawn_interval:
balls.append(Ball(WIDTH, HEIGHT))
spawn_interval = random.randint(*spawn_interval_range)
spawn_timer = time.time()
# Move balls
for ball in balls:
ball.move()
if ball.is_out_of_bounds():
window.update()
balls.remove(ball)
turtle.turtles().remove(ball)
balls_lost += 1
# Update window title
window.title(f"Time: {time.time() - game_timer:3.1f} | Balls lost: {balls_lost}")
# Limit frame rate
loop_time = time.time() - frame_start_time
if loop_time < 1 / frame_rate:
time.sleep(1 / frame_rate - loop_time)
window.update()
# Game over
final_time = time.time() - game_timer
# Hide balls
for ball in balls:
ball.hideturtle()
window.update()
# Show game over text
text = turtle.Turtle()
text.hideturtle()
text.color("white")
text.write(
f"Game Over | Time taken: {final_time:2.1f}",
align="center",
font=("Courier", 20, "normal")
)
turtle.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment