from Tkinter import *;
import random;
from math import sqrt;

def move_mitt_up(event):
	global mittx;
	global mitty;
	global w;
	global mitt;
	
	mitty += 10;
	w.coords(mitt, mittx, mitty, mittx+30, mitty+30);
	

def move_mitt_down(event):
	global mittx;
	global mitty;
	global w;
	global mitt;
	
	mitty -= 10;
	w.coords(mitt, mittx, mitty, mittx+30, mitty+30);
	

def move_ball():
	global ball;
	global w;
	global cx;
	global cy;
	global dx;
	global dy;
	global mittx, mitty;
	
	cx = cx+dx;
	cy = cy+dy;
	w.coords(ball, cx, cy, cx+10, cy+10);
	if ((cy<10) or (cy>590)):
		dy = -dy;
	
	if (sqrt((mittx-cx)*(mittx-cx)+(mitty-cy)*(mitty-cy))>15):
		if ((cx>0) and (cx<790)):
			w.after(30, move_ball);
	else:
		w.coords(ball, mittx, mitty, mittx+10, mitty+10);
			
root = Tk();
w = Canvas(root, width=800, height=600)
w.pack()

mittx = 760;
mitty = 300;
mitt = w.create_oval(mittx, mitty, mittx+30, mitty+30, fill="red");
root.bind("j", move_mitt_up);
root.bind("k", move_mitt_down);

cx = 10; 
cy = random.randint(0, 600);
ball = w.create_oval(cx, cy, cx+25, cy+25, fill="blue")
dx = random.randint(5, 10);
dy = random.randint(0, 10);

w.after(100, move_ball);
w.mainloop();