Created
February 25, 2023 00:18
-
-
Save stephengruppetta/ed183da98071d42509b52d56215dacc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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