Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/raw-annotate/163fc2378113/main.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 11 13:52:03 2011
Кодировка:

martiran@18: import Tkinter as tk
Alex@23: import tkFileDialog as tkfd
martiran@20: import engine
martiran@32: import snake
martiran@18:
martiran@18:
martiran@18:
martiran@18: class UI(object):
Alex@160: """User Interface:
Alex@160:
Alex@160: Atributes:
Alex@160:
Alex@160: - 'root' - root Window game placed at
Alex@160: - 'engine' - engine of the game
Alex@160: - 'canvas' - Widget field is pictured at
Alex@160: - 'step_id' - current step of the game
Alex@160: - 'after_id' - identificator of runing game process
Alex@166: - 'step_legth' - length of the step (in ms)
Alex@166: - 'game_length' - number of the steps in one round of the game"""
martiran@19: def __init__ (self):
martiran@136: """Create Python Battle game window.
martiran@136: Initialyze engige of the game."""
martiran@29: self.root = tk.Tk()
martiran@29: self.root.title("Python Battle")
martiran@29: self.canvas = tk.Canvas(self.root, background = "black")
martiran@19: self.canvas.pack(side ="top", fill="both", expand="yes")
me@96: self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
Alex@165: self.step_id = 0
martiran@19: self.engine = engine.Engine(self.canvas)
martiran@32: self.after_id = None
Alex@160: self.step_length = 150
Alex@166: self.game_length = 200
martiran@18: return
martiran@136:
me@96: def buttons_pack(self, root):
martiran@136: """Packing the buttons in root frame.
Alex@169: Definition of button functions.
Alex@169:
Alex@169: 'Load' - ask for snake file load
Alex@169: 'Run' - runs the game/next round. Next round starts with snakes survived in previous
Alex@169: 'Step' - do the next dtep of the game
Alex@169: 'End' - manual end of the game
Alex@169: 'Restart" - restart the field with snakes of previous round"""
me@96: buttons = tk.Frame(root)
Alex@160: load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
martiran@132: load_1.grid(row=1, column=2, stick="news")
Alex@160: load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
martiran@132: load_2.grid(row=2, column=3, stick="news")
Alex@165: run_b = tk.Button(buttons, text="Run", command=lambda: self.start())
Alex@160: run_b.grid(row=2, column=2, stick="news")
Alex@167: restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart(survived="no"))
Alex@160: restart_b.grid(row=1, column=5, stick="news")
Alex@160: load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
martiran@132: load_3.grid(row=3, column=2, stick="news")
Alex@160: load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
martiran@132: load_4.grid(row=2, column=1, stick="news")
me@96: step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
martiran@151: step_b.grid(row=2, column=5, stick="news")
martiran@151: end_b = tk.Button(buttons, text="End", command=lambda: self.end())
martiran@151: end_b.grid(row=3, column=5, stick="news")
me@133: for column in range(1, 6):
me@133: buttons.grid_columnconfigure(column, weight=1)
me@96: return buttons
martiran@29:
Alex@23: def load (self, snake_number):
martiran@136: """Ask for snake file loading.
martiran@136: Initialyzing snake and draw it on the field.
Alex@164: Return field back to default (without snakes) after end of the game."""
Alex@168: if self.step_id == self.game_length + 666:
martiran@29: self.step_id = 0
martiran@130: self.engine.snakes = [None, None, None, None]
martiran@29: pass
martiran@107: if self.step_id == 0:
me@150: file = tkfd.askopenfile(title="Open file")
me@150: if file == None:
me@150: return
martiran@29: snake = self.engine.create_snake(snake_number)
me@150: snake.load(file)
martiran@29: pass
Alex@103: self.engine.refill()
Alex@86: self.engine.redraw()
Alex@27: return
Alex@23:
Alex@165: def start (self):
Alex@165: """Init running of the game."""
Alex@166: if self.step_id == self.game_length + 666:
Alex@167: self.restart(survived="yes")
Alex@97: if self.dead_snake_check() == False:
Alex@97: return
Alex@165: self.engine.psnakes = self.engine.snakes[:]
Alex@165: self.run()
Alex@165:
Alex@165: def run (self):
Alex@165: """Run the game with 'step_length' ms step
Alex@165: After the end of the game - restarts it with snakes survived in
Alex@165: previous game"""
Alex@168: if self.step_id > self.game_length:
Alex@168: self.end()
Alex@168: return
martiran@32: self.step_id = self.step_id+1
martiran@28: self.engine.step()
Alex@160: self.after_id = self.canvas.after(self.step_length, self.run)
martiran@28: return
Alex@160:
martiran@28: def step (self):
martiran@136: """Do the next game step"""
Alex@97: if self.dead_snake_check() == False:
Alex@97: return
Alex@166: if self.step_id <= self.game_length:
martiran@32: if self.after_id != None:
martiran@32: self.canvas.after_cancel(self.after_id)
martiran@32: pass
martiran@32: self.step_id = self.step_id+1
martiran@29: self.engine.step()
martiran@29: pass
martiran@29: else:
martiran@29: self.end()
martiran@29: pass
martiran@29: return
Alex@97:
Alex@97: def dead_snake_check(self):
martiran@136: """Check the number of snakes alive.
martiran@136: End the game if alive snake number is less than two."""
Alex@97: dead_snakes = 0
Alex@97: for snake in self.engine.snakes:
Alex@97: if snake == None:
Alex@97: dead_snakes=dead_snakes+1
Alex@97: pass
Alex@97: if dead_snakes >= 3:
Alex@97: self.end()
Alex@97: return False
Alex@160:
Alex@167: def restart(self, survived):
Alex@167: """"Restarts snakes positions after the end of the game
Alex@167:
Alex@167: Options:
Alex@167: survived = "yes" - restarts next round only with snakes survived in previous round
Alex@167: survived = "no" - restart next roun with all snakes played in previous round"""
Alex@167: if survived == "yes":
Alex@167: snake_set = self.engine.snakes
Alex@167: else:
Alex@167: snake_set = self.engine.psnakes
Alex@163: self.step_id = 0
Alex@167: for i, snake in enumerate(snake_set):
Alex@167: if snake_set[i] != None:
Alex@163: self.engine.snakes[i] = snake
Alex@163: self.engine.create_snake(i, snake)
Alex@163: self.engine.refill()
Alex@163: self.engine.redraw()
Alex@23:
martiran@28: def end (self):
martiran@136: """End the game and raise the window that tels about it."""
Alex@60: if self.after_id != None:
Alex@60: self.canvas.after_cancel(self.after_id)
Alex@60: pass
Alex@166: self.step_id = self.game_length + 666
martiran@29: root = tk.Tk()
martiran@29: end_label = tk.Label(root, text="End")
martiran@29: end_label.pack()
martiran@29: root.mainloop()
martiran@18: pass
martiran@32:
me@42: if __name__ == "__main__":
me@42: snake_batle = UI()
me@42: snake_batle.root.mainloop()