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

snake

annotate main.py @ 96:3619305694ad

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