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

snake

view main.py @ 177:cd67dc65f599

Removed rules to keep near walls from e2
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Sun, 26 Dec 2010 12:12:44 +0300
parents 55aeb245c503
children e967a9b10fce
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.after_id != None:
87 return
88 if self.step_id == self.game_length + 666:
89 self.restart(survived="yes")
90 if self.step_id == 0:
91 self.engine.psnakes = self.engine.snakes[:]
92 self.run()
94 def run (self):
95 """Run the game with 'step_length' ms step
96 After the end of the game - restarts it with snakes survived in
97 previous game"""
98 if self.step_id > self.game_length:
99 self.end()
100 return
101 if self.dead_snake_check() == False:
102 return
103 self.step_id = self.step_id+1
104 self.engine.step()
105 self.after_id = self.canvas.after(self.step_length, self.run)
106 return
108 def step (self):
109 """Do the next game step"""
110 if self.dead_snake_check() == False:
111 return
112 if self.step_id == 0:
113 self.engine.psnakes = self.engine.snakes[:]
114 if self.step_id <= self.game_length:
115 self.run_cancel()
116 self.step_id = self.step_id+1
117 self.engine.step()
118 pass
119 else:
120 self.end()
121 pass
122 return
124 def run_cancel(self):
125 """Stops runnin of the game"""
126 if self.after_id != None:
127 self.canvas.after_cancel(self.after_id)
128 self.after_id = None
130 def dead_snake_check(self):
131 """Check the number of snakes alive.
132 End the game if alive snake number is less than two."""
133 dead_snakes = 0
134 for snake in self.engine.snakes:
135 if snake == None:
136 dead_snakes=dead_snakes+1
137 pass
138 if dead_snakes >= 3:
139 self.end()
140 return False
142 def restart(self, survived):
143 """"Restarts snakes positions after the end of the game
145 Options:
146 survived = "yes" - restarts next round only with snakes survived in previous round
147 survived = "no" - restart next roun with all snakes played in previous round"""
148 if survived == "yes":
149 snake_set = self.engine.snakes
150 else:
151 snake_set = self.engine.psnakes
152 self.step_id = 0
153 for i, snake in enumerate(snake_set):
154 if snake_set[i] != None:
155 self.engine.snakes[i] = snake
156 self.engine.create_snake(i, snake)
157 self.engine.refill()
158 self.engine.redraw()
160 def end (self):
161 """End the game and raise the window that tels about it."""
162 self.run_cancel()
163 self.step_id = self.game_length + 666
164 root = tk.Tk()
165 end_label = tk.Label(root, text="End")
166 end_label.pack()
167 root.mainloop()
168 pass
170 if __name__ == "__main__":
171 snake_batle = UI()
172 snake_batle.root.mainloop()