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.
Solution
from tkinter import *
import time
import random
wid = 800
hgt = 500
tk = Tk()
#preparing canvas.
canvas = Canvas(tk, width=wid, height=hgt, bg=\"black\")
tk.title(\"Draw ball\")
canvas.pack()
clrs = [\'red\', \'green\', \'blue\', \'orange\', \'yellow\', \'cyan\', \'magenta\',
\'dodgerblue\', \'turquoise\', \'grey\', \'gold\', \'pink\']
class Bouncing_ball:
def __init__(slf):
slf.size = random.randrange(200, 400)
color = random.choice(clrs)
slf.shape = canvas.create_rectangle(0, 0, slf.size, slf.size, fill=color)
slf.speedx = random.randrange(1, 10)
slf.speedy = random.randrange(1, 10)
def update(slf):
canvas.move(slf.shape, slf.speedx, slf.speedy)
pos = canvas.coords(slf.shape)
if pos[2] >= wid or pos[0] <= 0:
slf.speedx *= -1
if pos[3] >= hgt or pos[1] <= 0:
slf.speedy *= -1
bal_lst = []
for i in range(100):
bal_lst.append(Bouncing_ball())
while True:
for ball in bal_lst:
ball.update()
tk.update()
time.sleep(0.01)

