How to bounce the ball forever around the screen

phamtony33 at gmail.com phamtony33 at gmail.com
Thu Dec 3 23:19:23 EST 2015


from Tkinter import *
window = Tk()
canvas = Canvas(window, width=500, height=500, background="green")
canvas.pack()

def move_ball(speed_x, speed_y):
	box = canvas.bbox("ball")
	x1 = box[0]
	y1 = box[1]
	x2 = box[2]
	y2 = box[3]
	
	if x1 <= 0:
		speed_x = 0
		speed_y = 0
		
	canvas.move("ball", speed_x, speed_y)
	canvas.after(30, move_ball, speed_x, speed_y)
	
canvas.create_oval(225, 225, 275, 275, fill="blue", tags="ball")

move_ball(-10, 7)

mainloop()

 where in the code should i change to make the ball bounce around forever. is it the x and y?



More information about the Python-list mailing list