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

snake

annotate main.py @ 155:382ab7b65331

added smth for snake restart, not working yet
author Alex Martynov
date Tue, 21 Dec 2010 18:11:27 +0300
parents 3c909a161978
children 7a4853ff834f
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."""
Alex@155 49 if self.step_id == 666:
martiran@29 50 self.step_id = 0
martiran@130 51 self.engine.snakes = [None, None, None, None]
Alex@155 52 self.engine.psnakes = [None, None, None, None]
martiran@29 53 pass
martiran@107 54 if self.step_id == 0:
me@150 55 file = tkfd.askopenfile(title="Open file")
me@150 56 if file == None:
me@150 57 return
martiran@29 58 snake = self.engine.create_snake(snake_number)
me@150 59 snake.load(file)
martiran@29 60 pass
Alex@103 61 self.engine.refill()
Alex@86 62 self.engine.redraw()
Alex@27 63 return
Alex@23 64
martiran@18 65 def run (self):
martiran@136 66 """Run the game with 150 ms step"""
Alex@155 67 if self.step_id == 666:
Alex@155 68 self.step_id = 0
Alex@155 69 for i, snake in enumerate(self.engine.psnakes):
Alex@155 70 self.engine.snakes[i] = snake
Alex@155 71 self.engine.create_snake(i)
Alex@97 72 if self.dead_snake_check() == False:
Alex@97 73 return
martiran@136 74 if self.step_id > 200:
martiran@122 75 self.end()
martiran@122 76 return
martiran@32 77 self.step_id = self.step_id+1
martiran@28 78 self.engine.step()
martiran@136 79 self.after_id = self.canvas.after(150, self.run)
martiran@28 80 return
martiran@28 81 def step (self):
martiran@136 82 """Do the next game step"""
Alex@97 83 if self.dead_snake_check() == False:
Alex@97 84 return
martiran@32 85 if self.step_id <= 200:
martiran@32 86 if self.after_id != None:
martiran@32 87 self.canvas.after_cancel(self.after_id)
martiran@32 88 pass
martiran@32 89 self.step_id = self.step_id+1
martiran@29 90 self.engine.step()
martiran@29 91 pass
martiran@29 92 else:
martiran@29 93 self.end()
martiran@29 94 pass
martiran@29 95 return
Alex@97 96
Alex@97 97 def dead_snake_check(self):
martiran@136 98 """Check the number of snakes alive.
martiran@136 99 End the game if alive snake number is less than two."""
Alex@97 100 dead_snakes = 0
Alex@97 101 for snake in self.engine.snakes:
Alex@97 102 if snake == None:
Alex@97 103 dead_snakes=dead_snakes+1
Alex@97 104 pass
Alex@97 105 if dead_snakes >= 3:
Alex@97 106 self.end()
Alex@97 107 return False
Alex@23 108
martiran@28 109 def end (self):
martiran@136 110 """End the game and raise the window that tels about it."""
Alex@60 111 if self.after_id != None:
Alex@60 112 self.canvas.after_cancel(self.after_id)
Alex@60 113 pass
Alex@153 114 self.step_id = 666
martiran@29 115 root = tk.Tk()
martiran@29 116 end_label = tk.Label(root, text="End")
martiran@29 117 end_label.pack()
martiran@29 118 root.mainloop()
martiran@18 119 pass
martiran@32 120
me@42 121 if __name__ == "__main__":
me@42 122 snake_batle = UI()
me@42 123 snake_batle.root.mainloop()