Question: By using python 3, develops GUI application to to animate a bouncing ball. Ball will start near the top left corner of an 800 by

By using python 3, develops GUI application to to animate a bouncing ball. Ball will start near the top left corner of an 800 by 400 canvas. The ball will move in a 45 degrees, where it moves 2 pixels horizontally and 2 pixels vertically every 15 milliseconds. When the ball hits any of of the four edges of the canvas, it will bounce in the opposite direction. The angle of reflection is always equal to the angle of incidence. So, if the ball is moving south-east and it hits, let us say, the bottom edge, it will bounce in the north-east direction. It will be easier to consider the east-west (ie., right-left) movement as independent from the north-south (i.e, up-down movement). Below, you are given code to deal with the east-west movement and need to add code to deal with the north-south movement.

To help you with this lab, write the code in stages as follows and always make sure your code is working before you move to the next stage. You may also need to draw things down on a piece of paper to help you visualize things.

1.Create a GUI application with an 800 pixels by 400 pixels

2.Create a small ball starting near the top left corner of the canvas. You can either draw a small circle, say with diameter 20 pixels, or use an image of a small ball. Identify the ball (i.e, give it a tags value) so that you can refer to it later

3.Use two variables, say top_x and top_y, to track the ball's location. This is needed to figure out when the ball hits an edge and, therefore, should change direction

4.Use two variables to track the ball's horizonal and vertical directions. Initially, the ball moves east-south. So in my code, I used horizontal_direction and vertical_direction and set them as follows:

horizontal_direction = "east" vertical_direction = "south" 

5.Insert code for the animation loop to move the ball 2 pixels down (i.e. south) and 2 pixels right (i.e., east) every 15 miliseconds. This code will be adjusted in steps 6 and 7.

6.Use the idea in the following code and insert appropriate code in the animation loop from the previous step to take care of the ball's horizontal movement

 # horizontal movement if horizontal_direction == "east": self.canvas.move("ball", dx, 0) # move ball horizontally dx pixels to the right/east top_x += dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds if top_x >= canvas_width - ball_diameter: # ball has hit east wall horizontal_direction = "west" # change direction else: # i.e., horizontal_direction is "west" self.canvas.move("ball", -dx, 0) # move ball horizontally dx pixels to the left/west top_x -= dx if top_x <= 0: # ball has hit west wall horizontal_direction = "east" # change direction 

Add code to the animation loop to take care of the ball's vertical movemont.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!