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

snake

annotate main.py @ 153:3c909a161978

chnged number of end step_id
author Alex Martynov
date Tue, 21 Dec 2010 08:30:18 +0300
parents 42eeb876f42d
children 382ab7b65331
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@136 9 """User Interface:"""
martiran@19 10 def __init__ (self):
martiran@136 11 """Create Python Battle game window.
martiran@136 12 Initialyze engige of the game."""
martiran@29 13 self.root = tk.Tk()
martiran@29 14 self.root.title("Python Battle")
martiran@29 15 self.canvas = tk.Canvas(self.root, background = "black")
martiran@19 16 self.canvas.pack(side ="top", fill="both", expand="yes")
me@96 17 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
martiran@28 18 self.step_id = 0
martiran@19 19 self.engine = engine.Engine(self.canvas)
martiran@32 20 self.after_id = None
martiran@18 21 return
martiran@136 22
me@96 23 def buttons_pack(self, root):
martiran@136 24 """Packing the buttons in root frame.
martiran@136 25 Definition of button functions."""
me@96 26 buttons = tk.Frame(root)
me@96 27 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1))
martiran@132 28 load_1.grid(row=1, column=2, stick="news")
me@96 29 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2))
martiran@132 30 load_2.grid(row=2, column=3, stick="news")
me@96 31 run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
martiran@132 32 run_b.grid(row=1, column=5, stick="news")
me@96 33 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))
martiran@132 34 load_3.grid(row=3, column=2, stick="news")
me@96 35 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4))
martiran@132 36 load_4.grid(row=2, column=1, stick="news")
me@96 37 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
martiran@151 38 step_b.grid(row=2, column=5, stick="news")
martiran@151 39 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
martiran@151 40 end_b.grid(row=3, column=5, stick="news")
me@133 41 for column in range(1, 6):
me@133 42 buttons.grid_columnconfigure(column, weight=1)
me@96 43 return buttons
martiran@29 44
Alex@23 45 def load (self, snake_number):
martiran@136 46 """Ask for snake file loading.
martiran@136 47 Initialyzing snake and draw it on the field.
martiran@136 48 Return field back to default after end of the game."""
martiran@29 49 if self.step_id >= 200:
martiran@29 50 self.step_id = 0
martiran@130 51 self.engine.snakes = [None, None, None, None]
martiran@29 52 pass
martiran@107 53 if self.step_id == 0:
me@150 54 file = tkfd.askopenfile(title="Open file")
me@150 55 if file == None:
me@150 56 return
martiran@29 57 snake = self.engine.create_snake(snake_number)
me@150 58 snake.load(file)
martiran@29 59 pass
Alex@103 60 self.engine.refill()
Alex@86 61 self.engine.redraw()
Alex@27 62 return
Alex@23 63
martiran@18 64 def run (self):
martiran@136 65 """Run the game with 150 ms step"""
Alex@97 66 if self.dead_snake_check() == False:
Alex@97 67 return
martiran@136 68 if self.step_id > 200:
martiran@122 69 self.end()
martiran@122 70 return
martiran@32 71 self.step_id = self.step_id+1
martiran@28 72 self.engine.step()
martiran@136 73 self.after_id = self.canvas.after(150, self.run)
martiran@28 74 return
martiran@28 75 def step (self):
martiran@136 76 """Do the next game step"""
Alex@97 77 if self.dead_snake_check() == False:
Alex@97 78 return
martiran@32 79 if self.step_id <= 200:
martiran@32 80 if self.after_id != None:
martiran@32 81 self.canvas.after_cancel(self.after_id)
martiran@32 82 pass
martiran@32 83 self.step_id = self.step_id+1
martiran@29 84 self.engine.step()
martiran@29 85 pass
martiran@29 86 else:
martiran@29 87 self.end()
martiran@29 88 pass
martiran@29 89 return
Alex@97 90
Alex@97 91 def dead_snake_check(self):
martiran@136 92 """Check the number of snakes alive.
martiran@136 93 End the game if alive snake number is less than two."""
Alex@97 94 dead_snakes = 0
Alex@97 95 for snake in self.engine.snakes:
Alex@97 96 if snake == None:
Alex@97 97 dead_snakes=dead_snakes+1
Alex@97 98 pass
Alex@97 99 if dead_snakes >= 3:
Alex@97 100 self.end()
Alex@97 101 return False
Alex@23 102
martiran@28 103 def end (self):
martiran@136 104 """End the game and raise the window that tels about it."""
Alex@60 105 if self.after_id != None:
Alex@60 106 self.canvas.after_cancel(self.after_id)
Alex@60 107 pass
Alex@153 108 self.step_id = 666
martiran@29 109 root = tk.Tk()
martiran@29 110 end_label = tk.Label(root, text="End")
martiran@29 111 end_label.pack()
martiran@29 112 root.mainloop()
martiran@18 113 pass
martiran@32 114
me@42 115 if __name__ == "__main__":
me@42 116 snake_batle = UI()
me@42 117 snake_batle.root.mainloop()