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)


def move_mitt(event):
    global mitt
    global gameCanvas
    global mittx, mitty
    
    if event.char == "j":
        mitty = mitty+10
    elif event.char == "k":
        mitty = mitty-10
    gameCanvas.coords(mitt, mittx, mitty, mittx+MITT_SIZE, mitty+MITT_SIZE)


MITT_SIZE = 20
BALL_SIZE = 10
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)
mittx = gameCanvas.winfo_width()-30
mitty = gameCanvas.winfo_height()/2
mitt = gameCanvas.create_oval(mittx, mitty,
                              mittx+MITT_SIZE, mitty+MITT_SIZE, 
                              fill="red")

root.bind("k", move_mitt)
root.bind("j", move_mitt)

root.mainloop()
