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

snake

annotate main.py @ 186:73aed6bf1caf

engine.legal_moves() now return list of legal moves added UI.snake_move_check() - "passed" changed name for UI.dead_snake_check() -> UI.snake_dead_check()
author Alex Martynov
date Tue, 28 Dec 2010 16:47:32 +0300
parents 0cf4e42c75a1
children 954cc2774637
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@166 18 - 'step_legth' - length of the step (in ms)
Alex@166 19 - 'game_length' - number of the steps in one round of the game"""
martiran@19 20 def __init__ (self):
martiran@136 21 """Create Python Battle game window.
martiran@136 22 Initialyze engige of the game."""
martiran@29 23 self.root = tk.Tk()
martiran@29 24 self.root.title("Python Battle")
martiran@29 25 self.canvas = tk.Canvas(self.root, background = "black")
martiran@19 26 self.canvas.pack(side ="top", fill="both", expand="yes")
me@96 27 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
Alex@165 28 self.step_id = 0
martiran@19 29 self.engine = engine.Engine(self.canvas)
martiran@32 30 self.after_id = None
Alex@160 31 self.step_length = 150
Alex@166 32 self.game_length = 200
martiran@18 33 return
martiran@136 34
me@96 35 def buttons_pack(self, root):
martiran@136 36 """Packing the buttons in root frame.
Alex@169 37 Definition of button functions.
Alex@169 38
Alex@169 39 'Load' - ask for snake file load
Alex@169 40 'Run' - runs the game/next round. Next round starts with snakes survived in previous
Alex@169 41 'Step' - do the next dtep of the game
Alex@169 42 'End' - manual end of the game
Alex@169 43 'Restart" - restart the field with snakes of previous round"""
me@96 44 buttons = tk.Frame(root)
Alex@160 45 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
martiran@132 46 load_1.grid(row=1, column=2, stick="news")
Alex@160 47 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
martiran@132 48 load_2.grid(row=2, column=3, stick="news")
Alex@165 49 run_b = tk.Button(buttons, text="Run", command=lambda: self.start())
Alex@160 50 run_b.grid(row=2, column=2, stick="news")
Alex@167 51 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart(survived="no"))
Alex@160 52 restart_b.grid(row=1, column=5, stick="news")
Alex@160 53 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
martiran@132 54 load_3.grid(row=3, column=2, stick="news")
Alex@160 55 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
martiran@132 56 load_4.grid(row=2, column=1, stick="news")
me@96 57 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
martiran@151 58 step_b.grid(row=2, column=5, stick="news")
martiran@151 59 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
martiran@151 60 end_b.grid(row=3, column=5, stick="news")
me@133 61 for column in range(1, 6):
me@133 62 buttons.grid_columnconfigure(column, weight=1)
me@96 63 return buttons
martiran@29 64
Alex@23 65 def load (self, snake_number):
martiran@136 66 """Ask for snake file loading.
martiran@136 67 Initialyzing snake and draw it on the field.
Alex@164 68 Return field back to default (without snakes) after end of the game."""
Alex@168 69 if self.step_id == self.game_length + 666:
martiran@29 70 self.step_id = 0
martiran@130 71 self.engine.snakes = [None, None, None, None]
martiran@29 72 pass
martiran@107 73 if self.step_id == 0:
me@150 74 file = tkfd.askopenfile(title="Open file")
me@150 75 if file == None:
me@150 76 return
martiran@29 77 snake = self.engine.create_snake(snake_number)
me@150 78 snake.load(file)
martiran@29 79 pass
Alex@103 80 self.engine.refill()
Alex@86 81 self.engine.redraw()
Alex@27 82 return
Alex@23 83
Alex@165 84 def start (self):
Alex@165 85 """Init running of the game."""
Alex@175 86 if self.after_id != None:
Alex@175 87 return
Alex@166 88 if self.step_id == self.game_length + 666:
Alex@167 89 self.restart(survived="yes")
Alex@174 90 if self.step_id == 0:
Alex@174 91 self.engine.psnakes = self.engine.snakes[:]
Alex@165 92 self.run()
Alex@165 93
Alex@165 94 def run (self):
Alex@165 95 """Run the game with 'step_length' ms step
Alex@165 96 After the end of the game - restarts it with snakes survived in
Alex@165 97 previous game"""
Alex@168 98 if self.step_id > self.game_length:
Alex@168 99 self.end()
Alex@168 100 return
Alex@186 101 if self.snake_dead_check() == False:
Alex@172 102 return
martiran@32 103 self.step_id = self.step_id+1
martiran@28 104 self.engine.step()
Alex@160 105 self.after_id = self.canvas.after(self.step_length, self.run)
martiran@28 106 return
Alex@160 107
martiran@28 108 def step (self):
martiran@136 109 """Do the next game step"""
Alex@186 110 if self.snake_dead_check() == False:
Alex@97 111 return
Alex@173 112 if self.step_id == 0:
Alex@173 113 self.engine.psnakes = self.engine.snakes[:]
Alex@166 114 if self.step_id <= self.game_length:
Alex@175 115 self.run_cancel()
martiran@32 116 self.step_id = self.step_id+1
martiran@29 117 self.engine.step()
martiran@29 118 pass
martiran@29 119 else:
martiran@29 120 self.end()
martiran@29 121 pass
martiran@29 122 return
Alex@175 123
Alex@175 124 def run_cancel(self):
Alex@175 125 """Stops runnin of the game"""
Alex@175 126 if self.after_id != None:
Alex@175 127 self.canvas.after_cancel(self.after_id)
Alex@175 128 self.after_id = None
Alex@97 129
Alex@186 130 def snake_dead_check(self):
martiran@136 131 """Check the number of snakes alive.
martiran@136 132 End the game if alive snake number is less than two."""
Alex@97 133 dead_snakes = 0
Alex@97 134 for snake in self.engine.snakes:
Alex@97 135 if snake == None:
Alex@97 136 dead_snakes=dead_snakes+1
Alex@97 137 pass
Alex@97 138 if dead_snakes >= 3:
Alex@97 139 self.end()
Alex@97 140 return False
Alex@185 141
Alex@182 142 def snake_len_check(self):
Alex@182 143 """Get the snake with maximum length - the winer
Alex@182 144
Alex@182 145 Return:
Alex@182 146 winer - list of snake or snakes with max length
Alex@182 147 length - this maximum length"""
Alex@182 148 length=0
Alex@182 149 winer = []
Alex@182 150 for snake in self.engine.snakes:
Alex@182 151 if snake != None:
Alex@182 152 if len(snake.cells) > length:
Alex@182 153 length = len(snake.cells)
Alex@182 154 winer = [snake]
Alex@182 155 elif len(snake.cells) == length:
Alex@182 156 winer.append(snake)
Alex@182 157 return winer, length
Alex@186 158
Alex@186 159 def snake_move_check(self):
Alex@186 160 """Get possible movements of the snakes.
Alex@186 161 If all anakes cant move - return False."""
Alex@186 162 pass
Alex@160 163
Alex@167 164 def restart(self, survived):
Alex@167 165 """"Restarts snakes positions after the end of the game
Alex@167 166
Alex@167 167 Options:
Alex@167 168 survived = "yes" - restarts next round only with snakes survived in previous round
Alex@167 169 survived = "no" - restart next roun with all snakes played in previous round"""
Alex@167 170 if survived == "yes":
Alex@167 171 snake_set = self.engine.snakes
Alex@167 172 else:
Alex@167 173 snake_set = self.engine.psnakes
Alex@163 174 self.step_id = 0
Alex@167 175 for i, snake in enumerate(snake_set):
Alex@167 176 if snake_set[i] != None:
Alex@163 177 self.engine.snakes[i] = snake
Alex@163 178 self.engine.create_snake(i, snake)
Alex@163 179 self.engine.refill()
Alex@163 180 self.engine.redraw()
Alex@23 181
martiran@28 182 def end (self):
Alex@176 183 """End the round and raise the label that tels about it."""
Alex@175 184 self.run_cancel()
Alex@166 185 self.step_id = self.game_length + 666
Alex@185 186 (w, h), (x, y), _ = self.engine.field_geometry_calc()
Alex@181 187 self.engine.redraw()
Alex@185 188 self.canvas.create_text(x+w/2.0, y+h/2.0, text="End of the round", fill="white", font="bold")
Alex@182 189 winer, length = self.snake_len_check()
Alex@182 190 if len(winer) > 1:
Alex@185 191 self.canvas.create_text(x+w/2.0, y+h*2.0/3.0, text="Number of winers: %s" %(len(winer)), fill="white", font="bold")
Alex@182 192 else:
Alex@185 193 self.canvas.create_text(x+w/2.0, y+h*2.0/3.0, text="Winer: %s(%s)" %(winer[0].name, winer[0].color), fill="white", font="bold")
Alex@185 194 self.canvas.create_text(x+w/2.0, y+h*3.0/4.0, text="Total winer length: %s" %(length), fill="white", font="bold")
martiran@18 195 pass
martiran@32 196
me@42 197 if __name__ == "__main__":
me@42 198 snake_batle = UI()
me@42 199 snake_batle.root.mainloop()