from Tkinter import *
import random

def move():
    global ball
    global gameCanvas
    global x, y, dx, dy
    global BALL_SIZE
    global WAIT_TIME
    
    x = x + dx
    y = y + dy
    if y < 0:
        y = 0
        dy = -dy
    elif y > gameCanvas.winfo_height() - BALL_SIZE:
        y = gameCanvas.winfo_height() - BALL_SIZE
        dy = -dy
    gameCanvas.coords(ball, x, y, x+BALL_SIZE, y+BALL_SIZE)
    gameCanvas.after(WAIT_TIME, move)


# constants are usually written all in upper case
BALL_SIZE = 20
WAIT_TIME = 100
root = Tk()
gameCanvas = Canvas(root, width=400, height=400)
gameCanvas.grid(column=0,row=0)
gameCanvas.wait_visibility()

x = 0
y = random.randint(0,gameCanvas.winfo_height()-BALL_SIZE)
dx = 2
dy = random.randint(-5,5)
ball = gameCanvas.create_oval(x, y, x+BALL_SIZE, y+BALL_SIZE, fill="blue")

gameCanvas.after(WAIT_TIME, move)
	
root.mainloop()
