Guest User

Untitled

a guest
May 27th, 2010
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Tkinter as tk
  2. import time
  3.  
  4. tstep = 1
  5. blockwidth = 10
  6.  
  7. class App(tk.Frame):
  8.     def __init__(self, x=10, y=10, start_x=5, start_y=5, master=None, start_interactive=False):
  9.         tk.Frame.__init__(self, master)
  10.         self.grid()
  11.         self.createWidgets(x, y)
  12.         self.direction = {
  13.             "x": 1, # 1 for right, -1 for left
  14.             "y": -1  # 1 for down, -1 for up
  15.         }
  16.         self.colors = {
  17.             "start": "#00ff0c",
  18.             "middle": "#ffc600",
  19.             "next": "#000",
  20.             "end": "#ff0000"
  21.         }
  22.         (self.x, self.y, self.start_x, self.start_y) = (start_x, start_y, start_x, start_y)
  23.         (self.bound_x, self.bound_y) = (x, y)
  24.         if start_interactive:
  25.             self.start_interactive()
  26.         else:
  27.             self.highlight(self.x, self.y, self.colors["start"])
  28.             self.bounce_alot()
  29.     def start_interactive(self):
  30.         self.bigcanvas.bind("<Button-1>", self.clicked)
  31.     def clicked(self, event):
  32.         self.bigcanvas.unbind("<Button-1>")
  33.         x = int(self.bigcanvas.canvasx(event.x, blockwidth) / blockwidth) - 1
  34.         y = int(self.bigcanvas.canvasy(event.y, blockwidth) / blockwidth) - 1
  35.         (self.x, self.y, self.start_x, self.start_y) = (x, y, x, y)
  36.         self.highlight(self.start_x, self.start_y, self.colors["start"])
  37.         self.bounce_alot()
  38.     def createWidgets(self, width, height):
  39.         # Create rows and columns of boxes
  40.         self.bigcanvas = tk.Canvas(width=width*blockwidth+1, height=height*blockwidth+1, background="#DDD")
  41.         self.rectangles = []
  42.        
  43.         for x in xrange(width):
  44.             u = []
  45.             for y in xrange(height):
  46.                 u.append(
  47.                     self.bigcanvas.create_rectangle(
  48.                         x*blockwidth+1, y*blockwidth+1, (x+1)*blockwidth+1, (y+1)*blockwidth+1, fill="#FFF"
  49.                     )
  50.                 )
  51.                
  52.             self.rectangles.append(u)
  53.            
  54.         self.bigcanvas.grid()
  55.     def highlight(self, x, y, color):
  56.         self.bigcanvas.itemconfigure(self.rectangles[x][y], fill=color)
  57.        
  58.     def in_corner(self):
  59.         return self.on_wall() and self.on_ceiling()
  60.        
  61.     def on_wall(self):
  62.         return ((self.x == 0) or (self.x == self.bound_x - 1))
  63.        
  64.     def on_ceiling(self):
  65.         return ((self.y == 0) or (self.y == self.bound_y - 1))
  66.        
  67.     def fix_direction(self):
  68.         # we've hit a wall or side, but not a corner. Calculate new direction.
  69.         # If we've hit a wall, inverse X.
  70.         # If we've hit the ceiling, inverse Y.
  71.         if self.on_wall():
  72.             self.direction["x"] *= -1
  73.             return
  74.         if self.on_ceiling():
  75.             self.direction["y"] *= -1
  76.            
  77.     def next_step(self):
  78.         # print "x: %s, y: %s" % (self.x, self.y)
  79.         if not (self.x == self.start_x and self.y == self.start_y):
  80.             self.highlight(self.x, self.y, self.colors["middle"])
  81.         self.x += self.direction["x"]
  82.         self.y += self.direction["y"]
  83.         if not (self.x == self.start_x and self.y == self.start_y):
  84.             self.highlight(self.x, self.y, self.colors["next"])
  85.     def bounce(self):
  86.         if not self.in_corner():
  87.             if not self.on_wall() and not self.on_ceiling():
  88.                 self.next_step()
  89.                 self.after(tstep, self.bounce)
  90.             else:
  91.                 # print "hit! ",
  92.                 if not self.in_corner():
  93.                     self.fix_direction()
  94.                     self.next_step()
  95.                     self.after(tstep, self.bounce)
  96.         else:
  97.             # print "in corner!"
  98.             self.highlight(self.x, self.y, self.colors["end"])
  99.     def bounce_alot(self):
  100.         self.after(tstep, self.bounce)
  101.        
  102. root = App(80, 64, 50, 32, start_interactive=True)
  103. root.mainloop()
RAW Paste Data