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

snake

view main.py @ 129:cfe29cb793eb

Snake may be filled even if empty
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 16:21:00 +0300
parents 9486211ec334
children 95f0ab48310a
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 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
15 self.step_id = 0
16 self.engine = engine.Engine(self.canvas)
17 self.after_id = None
18 return
19 def buttons_pack(self, root):
20 buttons = tk.Frame(root)
21 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1))
22 load_1.pack(side="left", fill="both", expand = "yes")
23 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2))
24 load_2.pack(side="left", fill="both", expand = "yes")
25 run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
26 run_b.pack(side="left", fill="both", expand = "yes")
27 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))
28 load_3.pack(side="right", fill="both", expand = "yes")
29 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4))
30 load_4.pack(side="right", fill="both", expand = "yes")
31 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
32 step_b.pack(side="right", fill="both", expand = "yes")
33 return buttons
35 def load (self, snake_number):
36 if self.step_id >= 200:
37 self.step_id = 0
38 pass
39 if self.step_id == 0:
40 file_name = tkfd.askopenfilename(title="Open file")
41 snake = self.engine.create_snake(snake_number)
42 snake.load(open(file_name, "r"))
43 pass
44 self.engine.refill()
45 self.engine.redraw()
46 return
48 def run (self):
49 if self.dead_snake_check() == False:
50 return
51 if self.step_id >= 200:
52 self.end()
53 return
54 self.step_id = self.step_id+1
55 self.engine.step()
56 self.after_id = self.canvas.after(300, self.run)
57 return
58 def step (self):
59 if self.dead_snake_check() == False:
60 return
61 if self.step_id <= 200:
62 if self.after_id != None:
63 self.canvas.after_cancel(self.after_id)
64 pass
65 self.step_id = self.step_id+1
66 self.engine.step()
67 pass
68 else:
69 self.end()
70 pass
71 return
73 def dead_snake_check(self):
74 dead_snakes = 0
75 for snake in self.engine.snakes:
76 if snake == None:
77 dead_snakes=dead_snakes+1
78 pass
79 if dead_snakes >= 3:
80 self.end()
81 return False
83 def end (self):
84 if self.after_id != None:
85 self.canvas.after_cancel(self.after_id)
86 pass
87 root = tk.Tk()
88 end_label = tk.Label(root, text="End")
89 end_label.pack()
90 root.mainloop()
91 pass
93 if __name__ == "__main__":
94 snake_batle = UI()
95 snake_batle.root.mainloop()