from Tkinter import *   #Import the graphics library
#Depending on which version of python it might be "tkinter" or "Tkinter"
import random
import time
window_width=800    #How many pixels tall
window_height=600   #How many pixels wide
class Ball:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10, 10, 20, 20, fill=color)
        self.canvas.move(self.id, window_width/2, window_height/2)
        self.old_x=0
        self.old_y=0
        self.canvas_height = self.canvas.winfo_height()
    def locate(self,new_x,new_y, show_path=False):
        self.canvas.coords(self.id, new_x, window_height-new_y, new_x+10, window_height-new_y-10)
        if show_path:
          canvas.create_line(self.old_x, window_height-self.old_y, new_x, window_height-new_y)
        self.old_x=new_x
        self.old_y=new_y
class Square:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(10, 10, 20, 20, fill=color)
        self.canvas.move(self.id, 245, 100)
        self.canvas_height = self.canvas.winfo_height()
        self.old_x=0
        self.old_y=0
    def locate(self,new_x,new_y, show_path=False):
        self.canvas.coords(self.id, new_x, window_height-new_y, new_x+10, window_height-new_y-10)
        if show_path:
          canvas.create_line(self.old_x, window_height-self.old_y, new_x, window_height-new_y)
        self.old_x=new_x
        self.old_y=new_y
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=window_width, height=window_height, bd=0, highlightthickness=0)
canvas.pack()
tk.update()
G = 0.05     #Universal gravitation Constant, but since this is a game, we have a weird number.  Units are Newtone pixel^2 / kg^2
BIG_Q = Ball(canvas, 'black')
BIG_Q_m = 10000.0    #Black BIG_Q mass, in kg
BIG_Q_x = window_width/2.0
BIG_Q_y = window_height/2.0
small_q = Square(canvas,'blue')
small_q_m = 1000.0    #Mass, in kg
small_q_x = 100.0
small_q_y = window_height*0.5
small_q_vx = 0.0    #pixels/loop_time   X component of the small_q's velocity in computer units
small_q_vy = -1 #pixels/loop_time   Y component of the small_q's velocity in computer units
BIG_Q_vx = 0.0
BIG_Q_vy = 0.1
keep_looping = True
show_path = False
while keep_looping:
  small_q_to_BIG_Q_d_vector_x = BIG_Q_x - small_q_x   #X-component of vector pointing from small_q to black BIG_Q
  small_q_to_BIG_Q_d_vector_y = BIG_Q_y - small_q_y   #Y-component of vector pointing from small_q to black BIG_Q
  distance=(small_q_to_BIG_Q_d_vector_x**2 + small_q_to_BIG_Q_d_vector_y**2)**0.5
  #print "d=",distance,
  #We now make a unit vector pointing from the small_q to the black BIG_Q
  unit_vector_x = small_q_to_BIG_Q_d_vector_x / distance
  unit_vector_y = small_q_to_BIG_Q_d_vector_y / distance
  F_small_q = G * small_q_m * BIG_Q_m / distance**2
  #Acceleration goes here
  small_q_ax = unit_vector_x * F_small_q / small_q_m
  small_q_ay = unit_vector_y * F_small_q / small_q_m
  BIG_Q_ax = -unit_vector_x * F_small_q / BIG_Q_m
  BIG_Q_ay = -unit_vector_y * F_small_q / BIG_Q_m
  #Now update the velocity of the small_q using v' = v + at.  But t is one loop time.
  small_q_vx = small_q_vx + 0.5*small_q_ax     #High accuracy fix
  small_q_vy = small_q_vy + 0.5*small_q_ay     #High accuracy fix
  BIG_Q_vx = BIG_Q_vx + 0.5*BIG_Q_ax     #High accuracy fix
  BIG_Q_vy = BIG_Q_vy + 0.5*BIG_Q_ay     #High accuracy fix
  #We now compute the new x,y location of the small_q using x' = x + vt + 0.5 a t^2.  But t is one loop time.
  small_q_x = small_q_x + small_q_vx + 0.5 * small_q_ax       #...times 1 squared
  small_q_y = small_q_y + small_q_vy + 0.5 * small_q_ay       #...times 1 squared
  BIG_Q_x = BIG_Q_x + BIG_Q_vx + 0.5 * BIG_Q_ax       #...times 1 squared
  BIG_Q_y = BIG_Q_y + BIG_Q_vy + 0.5 * BIG_Q_ay       #...times 1 squared
  #Now update the velocity of the small_q using v' = v + at.  But t is one loop time.
  small_q_vx = small_q_vx + 0.5*small_q_ax       #High accuracy fix
  small_q_vy = small_q_vy + 0.5*small_q_ay       #High accuracy fix
  BIG_Q_vx = BIG_Q_vx + 0.5*BIG_Q_ax       #High accuracy fix
  BIG_Q_vy = BIG_Q_vy + 0.5*BIG_Q_ay       #High accuracy fix
  small_q.locate(small_q_x,small_q_y,show_path)         #Moves small_q to location (x,y)
  BIG_Q.locate(BIG_Q_x,BIG_Q_y,show_path)         #Moves BIG_Q to location (x,y)
  show_path= not show_path      #If this is true, you will see a trail behind the small_q.
  tk.update_idletasks()    #This line and the next force a refresh of the window
  tk.update()
  time.sleep(0.01) #This is the loop time in seconds.  Make it bigger to slow down the action

a=raw_input("Press Enter key to continue")    
    