Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Tkinter as tk
- import time
- tstep = 1
- blockwidth = 10
- class App(tk.Frame):
- def __init__(self, x=10, y=10, start_x=5, start_y=5, master=None, start_interactive=False):
- tk.Frame.__init__(self, master)
- self.grid()
- self.createWidgets(x, y)
- self.direction = {
- "x": 1, # 1 for right, -1 for left
- "y": -1 # 1 for down, -1 for up
- }
- self.colors = {
- "start": "#00ff0c",
- "middle": "#ffc600",
- "next": "#000",
- "end": "#ff0000"
- }
- (self.x, self.y, self.start_x, self.start_y) = (start_x, start_y, start_x, start_y)
- (self.bound_x, self.bound_y) = (x, y)
- if start_interactive:
- self.start_interactive()
- else:
- self.highlight(self.x, self.y, self.colors["start"])
- self.bounce_alot()
- def start_interactive(self):
- self.bigcanvas.bind("<Button-1>", self.clicked)
- def clicked(self, event):
- self.bigcanvas.unbind("<Button-1>")
- x = int(self.bigcanvas.canvasx(event.x, blockwidth) / blockwidth) - 1
- y = int(self.bigcanvas.canvasy(event.y, blockwidth) / blockwidth) - 1
- (self.x, self.y, self.start_x, self.start_y) = (x, y, x, y)
- self.highlight(self.start_x, self.start_y, self.colors["start"])
- self.bounce_alot()
- def createWidgets(self, width, height):
- # Create rows and columns of boxes
- self.bigcanvas = tk.Canvas(width=width*blockwidth+1, height=height*blockwidth+1, background="#DDD")
- self.rectangles = []
- for x in xrange(width):
- u = []
- for y in xrange(height):
- u.append(
- self.bigcanvas.create_rectangle(
- x*blockwidth+1, y*blockwidth+1, (x+1)*blockwidth+1, (y+1)*blockwidth+1, fill="#FFF"
- )
- )
- self.rectangles.append(u)
- self.bigcanvas.grid()
- def highlight(self, x, y, color):
- self.bigcanvas.itemconfigure(self.rectangles[x][y], fill=color)
- def in_corner(self):
- return self.on_wall() and self.on_ceiling()
- def on_wall(self):
- return ((self.x == 0) or (self.x == self.bound_x - 1))
- def on_ceiling(self):
- return ((self.y == 0) or (self.y == self.bound_y - 1))
- def fix_direction(self):
- # we've hit a wall or side, but not a corner. Calculate new direction.
- # If we've hit a wall, inverse X.
- # If we've hit the ceiling, inverse Y.
- if self.on_wall():
- self.direction["x"] *= -1
- return
- if self.on_ceiling():
- self.direction["y"] *= -1
- def next_step(self):
- # print "x: %s, y: %s" % (self.x, self.y)
- if not (self.x == self.start_x and self.y == self.start_y):
- self.highlight(self.x, self.y, self.colors["middle"])
- self.x += self.direction["x"]
- self.y += self.direction["y"]
- if not (self.x == self.start_x and self.y == self.start_y):
- self.highlight(self.x, self.y, self.colors["next"])
- def bounce(self):
- if not self.in_corner():
- if not self.on_wall() and not self.on_ceiling():
- self.next_step()
- self.after(tstep, self.bounce)
- else:
- # print "hit! ",
- if not self.in_corner():
- self.fix_direction()
- self.next_step()
- self.after(tstep, self.bounce)
- else:
- # print "in corner!"
- self.highlight(self.x, self.y, self.colors["end"])
- def bounce_alot(self):
- self.after(tstep, self.bounce)
- root = App(80, 64, 50, 32, start_interactive=True)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement