Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/2f62804e21fc/main.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 06:49:26 2013
Кодировка:
snake: 2f62804e21fc main.py

snake

view main.py @ 57:2f62804e21fc

Added example snake
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 00:03:35 +0300
parents b2eaeeb74d87
children a3bb04e72924
line source
1 import Tkinter as tk
2 import tkFileDialog as tkfd
3 import engine
4 import snake
8 class UI(object):
9 def __init__ (self):
10 self.root = tk.Tk()
11 self.root.title("Python Battle")
12 self.canvas = tk.Canvas(self.root, background = "black")
13 self.canvas.pack(side ="top", fill="both", expand="yes")
14 buttons = tk.Frame(self.root)
15 buttons.pack(side ="bottom", fill="both", expand="yes")
16 self.buttons_pack(buttons)
17 self.step_id = 0
18 self.engine = engine.Engine(self.canvas)
19 self.after_id = None
20 return
21 def buttons_pack(self, frame):
22 load_1 = tk.Button(frame, text="Load 1", command=lambda: self.load(1))
23 load_1.pack(side="left", fill="both", expand = "yes")
24 load_2 = tk.Button(frame, text="Load 2", command=lambda: self.load(2))
25 load_2.pack(side="left", fill="both", expand = "yes")
26 run_b = tk.Button(frame, text="Run", command=lambda: self.run())
27 run_b.pack(side="left", fill="both", expand = "yes")
28 load_3 = tk.Button(frame, text="Load 3", command=lambda: self.load(3))
29 load_3.pack(side="right", fill="both", expand = "yes")
30 load_4 = tk.Button(frame, text="Load 4", command=lambda: self.load(4))
31 load_4.pack(side="right", fill="both", expand = "yes")
32 step_b = tk.Button(frame, text="Step", command=lambda: self.step())
33 step_b.pack(side="right", fill="both", expand = "yes")
34 return
36 def load (self, snake_number):
37 if self.step_id >= 200:
38 self.step_id = 0
39 pass
40 elif self.step_id == 0:
41 file_name = tkfd.askopenfilename(title="Open file")
42 snake = self.engine.create_snake(snake_number)
43 snake.load(file_name)
44 pass
45 else:
46 pass
47 return
49 def run (self):
50 self.step_id = self.step_id+1
51 self.engine.step()
52 self.after_id = self.canvas.after(300, self.run())
53 if self.step_id == 200:
54 self.canvas.after_cancel(self.after_id)
55 self.end()
56 pass
57 return
58 def step (self):
59 if self.step_id <= 200:
60 if self.after_id != None:
61 self.canvas.after_cancel(self.after_id)
62 pass
63 self.step_id = self.step_id+1
64 self.engine.step()
65 pass
66 else:
67 self.end()
68 pass
69 return
71 def end (self):
72 root = tk.Tk()
73 end_label = tk.Label(root, text="End")
74 end_label.pack()
75 root.mainloop()
76 pass
78 if __name__ == "__main__":
79 snake_batle = UI()
80 snake_batle.root.mainloop()