This is python 3 and here is my code but it did not run it f
This is python 3, and here is my code, but it did not run it.
from tkinter import *
class Bouncingball(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title(\"Bouncing Ball\")
self.grid()
canvas_width = 800 #canvas width
canvas_height = 400 #canvas height
self.canvas = Canvas(width = canvas_width, height = canvas_height, bg = \"white\")
self.canvas.grid()
ball_diameter = 20 #draw a ball in pixels
top_left_x = 2
top_left_y = 2
self.canvas.create_oval(top_left_x, top_left_y, top_left_x + ball_diameter,
top_left_y + ball_diameter, fill = \"red\", tags = \"ball\")
horizontal_direction = \"east\"
vertical_direction = \"south\"
dx = dy = 2
while True:
if horizontal_direction == \"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
top_x = canvas_width - ball_diameter
horizontal_direction = \"west\" # change direction
self.canvas.move(\"ball\", dx, 0) # move ball horizontally dx pixels to the right/east
else: # i.e., horizontal_direction is \"west\"
top_x -= dx
if top_x <= 0: # ball has hit west wall
top_x = 0 # you may need to adjust this a little
horizontal_direction = \"east\" # change direction
self.canvas.move(\"ball\", -dx, 0) # move ball horizontally dx pixels to the left/westdef main():
def main():
Bouncingball().mainloop()
main()
here is question.
For this lab, you will develop a GUI application to to animate a bouncing ball. The 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.
Create a GUI application with an 800 pixels by 400 pixels
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
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
Use two variables to track the ball\'s horizonal and vertical direction. Initially, the ball moves east-south. So in my code, I used horizontal_direction and vertical_direction and set them as follows:
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. Hint: see how this was done in the example AnimationDemo.py on TRACE
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
Add code to the animation loop to take care of the ball\'s vertical movemont.
Solution
#Do not ever run a while loop to animate.
from tkinter import *
class Bouncingball(Frame):
def __init__(self):
Frame.__init__(self)
#master = Tk()
#self.master.title(\"Bouncing Ball\")
#self.grid()
self.canvas_width = 800 #canvas width
self.canvas_height = 400 #canvas height
self.canvas = Canvas(width = self.canvas_width, height = self.canvas_height, bg = \"white\")
self.canvas.grid()
self.ball_diameter = 20 #draw a ball in pixels
self.top_left_x = 2
self.top_left_y = 2
self.canvas.create_oval(self.top_left_x, self.top_left_y, self.top_left_x + self.ball_diameter,
self.top_left_y + self.ball_diameter, fill = \"red\", tags = \"ball\")
self.horizontal_direction = \"east\"
self.vertical_direction = \"south\"
self.dx = self.dy = 2
#while True:
def draw_loop():
if self.horizontal_direction == \"east\":
self.top_left_x += self.dx # dx is 2 because the ball moves 2 pixels horizontally every 15 milliseconds
if self.top_left_x >= self.canvas_width - self.ball_diameter: # ball has hit east wall
self.top_left_x = self.canvas_width - self.ball_diameter
self.horizontal_direction = \"west\" # change direction
self.canvas.move(\"ball\", self.dx, 0) # move ball horizontally dx pixels to the right/east
#self.canvas.create_oval(self.top_left_x, self.top_left_y, self.top_left_x + self.ball_diameter, self.top_left_y + self.ball_diameter, fill = \"red\", tags = \"ball\")
if self.horizontal_direction == \"west\": # i.e., horizontal_direction is \"west\"
self.top_left_x -= self.dx
if self.top_left_x <= 0: # ball has hit west wall
self.top_x = 0 # you may need to adjust this a little
self.horizontal_direction = \"east\" # change direction
self.canvas.move(\"ball\", -1.0*self.dx, 0) # move ball horizontally dx pixels to the left/westdef main():
#self.canvas.create_oval(self.top_left_x, self.top_left_y, self.top_left_x + self.ball_diameter, self.top_left_y + self.ball_diameter, fill = \"red\", tags = \"ball\")
if self.vertical_direction == \"north\":
self.top_left_y -= self.dy
if self.top_left_y <= 0: # ball has hit north wall
self.top_left_y = 0 # you may need to adjust this a little
self.vertical_direction = \"south\" # change direction
self.canvas.move(\"ball\", 0, -1.0*self.dy) # move ball horizontally dx pixels to the left/westdef main():
if self.vertical_direction == \"south\":
self.top_left_y += self.dy
if self.top_left_y >= self.canvas_height - self.ball_diameter: # ball has hit north wall
self.top_left_y = self.canvas_height - self.ball_diameter # you may need to adjust this a little
self.vertical_direction = \"north\" # change direction
self.canvas.move(\"ball\", 0, self.dy) # move ball horizontally dx pixels to the left/west
self.after(15, draw_loop)
draw_loop()
#def animate(self):
# print(1)
# draw_loop(self)
# self.after(100, animate)
#animate(self)
def main():
Bouncingball().mainloop()
main()


