Skip to content

Instantly share code, notes, and snippets.

@BjoernSchilberg
Forked from stephengruppetta/juggling_balls.py
Created March 2, 2023 18:54
Show Gist options
  • Save BjoernSchilberg/3cdf3ca26d0dce069cd0ffad49827554 to your computer and use it in GitHub Desktop.
Save BjoernSchilberg/3cdf3ca26d0dce069cd0ffad49827554 to your computer and use it in GitHub Desktop.
# juggling_balls.py
import random
import turtle
class Ball(turtle.Turtle):
MAX_VELOCITY = 5
GRAVITY = 0.07
BAT_VELOCITY_CHANGE = 8
def __init__(self, width, height):
super().__init__()
self.width = width
self.height = height
self.shape("circle")
self.color(
random.random(),
random.random(),
random.random(),
)
self.penup()
self.setposition(
random.randint(-self.width // 2, self.width // 2),
random.randint(-self.height // 2, self.height // 2),
)
self.setheading(90)
self.velocity = random.randint(1, self.MAX_VELOCITY)
def move(self):
self.forward(self.velocity)
self.fall()
def fall(self):
self.velocity -= self.GRAVITY
def is_out_of_bounds(self):
if self.ycor() < -self.height // 2:
self.hideturtle()
return True
return False
def bat_up(self):
self.velocity += self.BAT_VELOCITY_CHANGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment