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

snake

annotate main.py @ 160:56e7d0bfd362

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