Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/4eec473f445b/main.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 06:21:01 2013
Кодировка:
snake: 4eec473f445b main.py

snake

view main.py @ 169:4eec473f445b

added button work explanation in documentation.
author Alex Martynov
date Thu, 23 Dec 2010 20:37:13 +0300
parents 0f7df983d610
children 4a3ae22734b0
line source
1 import Tkinter as tk
2 import tkFileDialog as tkfd
3 import engine
4 import snake
8 class UI(object):
9 """User Interface:
11 Atributes:
13 - 'root' - root Window game placed at
14 - 'engine' - engine of the game
15 - 'canvas' - Widget field is pictured at
16 - 'step_id' - current step of the game
17 - 'after_id' - identificator of runing game process
18 - 'step_legth' - length of the step (in ms)
19 - 'game_length' - number of the steps in one round of the game"""
20 def __init__ (self):
21 """Create Python Battle game window.
22 Initialyze engige of the game."""
23 self.root = tk.Tk()
24 self.root.title("Python Battle")
25 self.canvas = tk.Canvas(self.root, background = "black")
26 self.canvas.pack(side ="top", fill="both", expand="yes")
27 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
28 self.step_id = 0
29 self.engine = engine.Engine(self.canvas)
30 self.after_id = None
31 self.step_length = 150
32 self.game_length = 200
33 return
35 def buttons_pack(self, root):
36 """Packing the buttons in root frame.
37 Definition of button functions.
39 'Load' - ask for snake file load
40 'Run' - runs the game/next round. Next round starts with snakes survived in previous
41 'Step' - do the next dtep of the game
42 'End' - manual end of the game
43 'Restart" - restart the field with snakes of previous round"""
44 buttons = tk.Frame(root)
45 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
46 load_1.grid(row=1, column=2, stick="news")
47 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
48 load_2.grid(row=2, column=3, stick="news")
49 run_b = tk.Button(buttons, text="Run", command=lambda: self.start())
50 run_b.grid(row=2, column=2, stick="news")
51 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart(survived="no"))
52 restart_b.grid(row=1, column=5, stick="news")
53 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
54 load_3.grid(row=3, column=2, stick="news")
55 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
56 load_4.grid(row=2, column=1, stick="news")
57 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
58 step_b.grid(row=2, column=5, stick="news")
59 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
60 end_b.grid(row=3, column=5, stick="news")
61 for column in range(1, 6):
62 buttons.grid_columnconfigure(column, weight=1)
63 return buttons
65 def load (self, snake_number):
66 """Ask for snake file loading.
67 Initialyzing snake and draw it on the field.
68 Return field back to default (without snakes) after end of the game."""
69 if self.step_id == self.game_length + 666:
70 self.step_id = 0
71 self.engine.snakes = [None, None, None, None]
72 pass
73 if self.step_id == 0:
74 file = tkfd.askopenfile(title="Open file")
75 if file == None:
76 return
77 snake = self.engine.create_snake(snake_number)
78 snake.load(file)
79 pass
80 self.engine.refill()
81 self.engine.redraw()
82 return
84 def start (self):
85 """Init running of the game."""
86 if self.step_id == self.game_length + 666:
87 self.restart(survived="yes")
88 if self.dead_snake_check() == False:
89 return
90 self.engine.psnakes = self.engine.snakes[:]
91 self.run()
93 def run (self):
94 """Run the game with 'step_length' ms step
95 After the end of the game - restarts it with snakes survived in
96 previous game"""
97 if self.step_id > self.game_length:
98 self.end()
99 return
100 self.step_id = self.step_id+1
101 self.engine.step()
102 self.after_id = self.canvas.after(self.step_length, self.run)
103 return
105 def step (self):
106 """Do the next game step"""
107 if self.dead_snake_check() == False:
108 return
109 if self.step_id <= self.game_length:
110 if self.after_id != None:
111 self.canvas.after_cancel(self.after_id)
112 pass
113 self.step_id = self.step_id+1
114 self.engine.step()
115 pass
116 else:
117 self.end()
118 pass
119 return
121 def dead_snake_check(self):
122 """Check the number of snakes alive.
123 End the game if alive snake number is less than two."""
124 dead_snakes = 0
125 for snake in self.engine.snakes:
126 if snake == None:
127 dead_snakes=dead_snakes+1
128 pass
129 if dead_snakes >= 3:
130 self.end()
131 return False
133 def restart(self, survived):
134 """"Restarts snakes positions after the end of the game
136 Options:
137 survived = "yes" - restarts next round only with snakes survived in previous round
138 survived = "no" - restart next roun with all snakes played in previous round"""
139 if survived == "yes":
140 snake_set = self.engine.snakes
141 else:
142 snake_set = self.engine.psnakes
143 self.step_id = 0
144 for i, snake in enumerate(snake_set):
145 if snake_set[i] != None:
146 self.engine.snakes[i] = snake
147 self.engine.create_snake(i, snake)
148 self.engine.refill()
149 self.engine.redraw()
151 def end (self):
152 """End the game and raise the window that tels about it."""
153 if self.after_id != None:
154 self.canvas.after_cancel(self.after_id)
155 pass
156 self.step_id = self.game_length + 666
157 root = tk.Tk()
158 end_label = tk.Label(root, text="End")
159 end_label.pack()
160 root.mainloop()
161 pass
163 if __name__ == "__main__":
164 snake_batle = UI()
165 snake_batle.root.mainloop()