''' graphics3.py Jeff Ondich, 11 October 2018 Started in Pascal on 1/25/95 Ported to C++, Java, and Python Adapted to Python 3 by Jed Yang in 2016 1. Try it. 2. How can you slow down or speed up the motion of the ball? 3. I can speed up the animation, to some point, by changing the value in the time.sleep() function. However, at some point, the screen flashes and the animation no longer looks good. Is there something you can change in the step() method to speed up the animation? 4. Try to get the ball to bounce from side to side instead of up and down. 5. Can you get the colors to change gradually from red to blue and back instead of abruptly? ''' import time from graphics import * class BouncingBall: def __init__(self, x, y, radius, window): self.window = window self.going_up = False self.down_color = color_rgb(200, 10, 60) self.up_color = color_rgb(60, 60, 200) self.circle = Circle(Point(x, y), radius) self.circle.setFill(self.down_color) self.circle.draw(window) def step(self): # Move the ball if self.going_up: dy = -4 else: dy = 4 self.circle.move(0, dy) # Does the ball need to bounce? height = self.window.getHeight() width = self.window.getWidth() x = self.circle.getCenter().getX() y = self.circle.getCenter().getY() if y + dy >= height - self.circle.getRadius(): self.going_up = True self.circle.setFill(self.up_color) elif y + dy <= self.circle.getRadius(): self.going_up = False self.circle.setFill(self.down_color) def main(): # First, create a window with a white background. window_width = 650 window_height = 500 window = GraphWin('Bounce', window_width, window_height) window.setBackground(color_rgb(255, 255, 255)) # Create a BouncingBall object with the specified position and size, # and associate it with the window created above. ball = BouncingBall(window_width / 2, 50, 50, window) # Until the user kills the program with a Ctrl+C, move the ball # one step, wait for a bit, move the ball another step, wait, etc. print('Type Ctrl-C in the terminal window to make the animation stop.') while True: ball.step() time.sleep(0.02) main()