here is my codepython3 I want to the ball starts eastsouth a
here is my code(python3). I want to the ball starts east-south, and 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.
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_x = 2
top_y = 2
self.canvas.create_oval(top_x, top_y, top_x + ball_diameter,
top_y + ball_diameter, fill = \"red\", tags = \"ball\")
horizontal_direction = \"east\"
vertical_direction = \"south\"
dx = dy = 2
while True:
#horizontal movement
if horizontal_direction == \"east\":
top_x += dx
if top_x >= canvas_width - ball_diameter:
top_x = canvas_width - ball_diameter
horizontal_direction = \"west\"
self.canvas.move(\"ball\", dx, 0)
else:
top_x -= dx
if top_x <= 0:
top_x = 0
horizontal_direction = \"east\"
self.canvas.move(\"ball\", -dx, 0)
self.canvas.after(15)
self.canvas.update()
while True:
#vertical movement
if vertical_direction == \"south\":
top_y += dy
if top_y >= canvas_height - ball_diameter:
top_y = canvas_height - ball_diameter
vertical_direction = \"north\"
self.canvas.move(\"ball\", dy, 0)
else:
top_y -= dy
if top_y <= 0:
top3_y = 0
vertical_direction = \"south\"
self.canvas.move(\"ball\", -dy, 0)
self.canvas.after(15)
self.canvas.update()
def main():
Bouncingball().mainloop()
main()
here is more detail(my homework)
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
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
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_left_x = 0
self.horizontal_direction = \"east\" # change direction
self.canvas.move(\"ball\", -1.0*self.dx, 0) # move ball horizontally dx pixels to the left/west
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
self.vertical_direction = \"south\" # change direction
self.canvas.move(\"ball\", 0, -1.0*self.dy) # move ball horizontally dy pixels to the north
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 south wall
self.top_left_y = self.canvas_height - self.ball_diameter
self.vertical_direction = \"north\" # change direction
self.canvas.move(\"ball\", 0, self.dy) # move ball horizontally dy pixels to the south
self.after(15, draw_loop)
draw_loop()
def main():
Bouncingball().mainloop()
main()


