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

snake

view main.py @ 80:2f56e186e75c

Fixed initial value for snake.Rule.direction
author Danya Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 01:08:29 +0300
parents 21a5779088e6
children 36ce9881e2c3
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.end()
55 pass
56 dead_snakes = 0
57 for snake in self.engine.snakes:
58 if snake == None:
59 dead_snakes=dead_snakes+1
60 pass
61 if dead_snakes >= 3:
62 self.end()
63 pass
64 return
65 def step (self):
66 if self.step_id <= 200:
67 if self.after_id != None:
68 self.canvas.after_cancel(self.after_id)
69 pass
70 self.step_id = self.step_id+1
71 self.engine.step()
72 pass
73 else:
74 self.end()
75 pass
76 return
78 def end (self):
79 if self.after_id != None:
80 self.canvas.after_cancel(self.after_id)
81 pass
82 root = tk.Tk()
83 end_label = tk.Label(root, text="End")
84 end_label.pack()
85 root.mainloop()
86 pass
88 if __name__ == "__main__":
89 snake_batle = UI()
90 snake_batle.root.mainloop()