Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/c89777ba44ec/main.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 19:02:18 2014
Кодировка:
snake: main.py annotate

snake

annotate main.py @ 97:c89777ba44ec

main.UI.dead_snakes_check()
author Alex Martynov
date Mon, 20 Dec 2010 02:26:31 +0300
parents c0d500d241ff
children 4436cf8ba30c
rev   line source
martiran@18 1 import Tkinter as tk
Alex@23 2 import tkFileDialog as tkfd
martiran@20 3 import engine
martiran@32 4 import snake
martiran@18 5
martiran@18 6
martiran@18 7
martiran@18 8 class UI(object):
martiran@19 9 def __init__ (self):
martiran@29 10 self.root = tk.Tk()
martiran@29 11 self.root.title("Python Battle")
martiran@29 12 self.canvas = tk.Canvas(self.root, background = "black")
martiran@19 13 self.canvas.pack(side ="top", fill="both", expand="yes")
martiran@29 14 buttons = tk.Frame(self.root)
martiran@20 15 buttons.pack(side ="bottom", fill="both", expand="yes")
martiran@20 16 self.buttons_pack(buttons)
martiran@28 17 self.step_id = 0
martiran@19 18 self.engine = engine.Engine(self.canvas)
martiran@32 19 self.after_id = None
martiran@18 20 return
martiran@20 21 def buttons_pack(self, frame):
martiran@32 22 load_1 = tk.Button(frame, text="Load 1", command=lambda: self.load(1))
martiran@32 23 load_1.pack(side="left", fill="both", expand = "yes")
martiran@32 24 load_2 = tk.Button(frame, text="Load 2", command=lambda: self.load(2))
martiran@32 25 load_2.pack(side="left", fill="both", expand = "yes")
martiran@28 26 run_b = tk.Button(frame, text="Run", command=lambda: self.run())
martiran@32 27 run_b.pack(side="left", fill="both", expand = "yes")
martiran@32 28 load_3 = tk.Button(frame, text="Load 3", command=lambda: self.load(3))
martiran@32 29 load_3.pack(side="right", fill="both", expand = "yes")
martiran@32 30 load_4 = tk.Button(frame, text="Load 4", command=lambda: self.load(4))
martiran@32 31 load_4.pack(side="right", fill="both", expand = "yes")
martiran@28 32 step_b = tk.Button(frame, text="Step", command=lambda: self.step())
martiran@32 33 step_b.pack(side="right", fill="both", expand = "yes")
martiran@20 34 return
martiran@29 35
Alex@23 36 def load (self, snake_number):
martiran@29 37 if self.step_id >= 200:
martiran@29 38 self.step_id = 0
martiran@29 39 pass
martiran@29 40 elif self.step_id == 0:
martiran@29 41 file_name = tkfd.askopenfilename(title="Open file")
martiran@29 42 snake = self.engine.create_snake(snake_number)
Alex@78 43 snake.load(open(file_name, "r"))
martiran@29 44 pass
martiran@29 45 else:
martiran@29 46 pass
Alex@86 47 self.engine.redraw()
Alex@27 48 return
Alex@23 49
martiran@18 50 def run (self):
Alex@97 51 if self.dead_snake_check() == False:
Alex@97 52 return
martiran@32 53 self.step_id = self.step_id+1
martiran@28 54 self.engine.step()
martiran@28 55 self.after_id = self.canvas.after(300, self.run())
martiran@28 56 if self.step_id == 200:
Alex@60 57 self.end()
Alex@60 58 pass
martiran@28 59 return
martiran@28 60 def step (self):
Alex@97 61 if self.dead_snake_check() == False:
Alex@97 62 return
martiran@32 63 if self.step_id <= 200:
martiran@32 64 if self.after_id != None:
martiran@32 65 self.canvas.after_cancel(self.after_id)
martiran@32 66 pass
martiran@32 67 self.step_id = self.step_id+1
martiran@29 68 self.engine.step()
martiran@29 69 pass
martiran@29 70 else:
martiran@29 71 self.end()
martiran@29 72 pass
martiran@29 73 return
Alex@97 74
Alex@97 75 def dead_snake_check(self):
Alex@97 76 dead_snakes = 0
Alex@97 77 for snake in self.engine.snakes:
Alex@97 78 if snake == None:
Alex@97 79 dead_snakes=dead_snakes+1
Alex@97 80 pass
Alex@97 81 if dead_snakes >= 3:
Alex@97 82 self.end()
Alex@97 83 return False
Alex@23 84
martiran@28 85 def end (self):
Alex@60 86 if self.after_id != None:
Alex@60 87 self.canvas.after_cancel(self.after_id)
Alex@60 88 pass
martiran@29 89 root = tk.Tk()
martiran@29 90 end_label = tk.Label(root, text="End")
martiran@29 91 end_label.pack()
martiran@29 92 root.mainloop()
martiran@18 93 pass
martiran@32 94
me@42 95 if __name__ == "__main__":
me@42 96 snake_batle = UI()
me@42 97 snake_batle.root.mainloop()