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

snake

view main.py @ 174:55aeb245c503

fixed bug of lost previous snakes memory, while turning from run mode to step and back fixes #19
author Alex Martynov
date Fri, 24 Dec 2010 00:58:36 +0300
parents 008be193a0a3
children fde515c7a4c8
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.step_id == 0:
89 self.engine.psnakes = self.engine.snakes[:]
90 self.run()
92 def run (self):
93 """Run the game with 'step_length' ms step
94 After the end of the game - restarts it with snakes survived in
95 previous game"""
96 if self.step_id > self.game_length:
97 self.end()
98 return
99 if self.dead_snake_check() == False:
100 return
101 self.step_id = self.step_id+1
102 self.engine.step()
103 self.after_id = self.canvas.after(self.step_length, self.run)
104 return
106 def step (self):
107 """Do the next game step"""
108 if self.dead_snake_check() == False:
109 return
110 if self.step_id == 0:
111 self.engine.psnakes = self.engine.snakes[:]
112 if self.step_id <= self.game_length:
113 if self.after_id != None:
114 self.canvas.after_cancel(self.after_id)
115 pass
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 dead_snake_check(self):
125 """Check the number of snakes alive.
126 End the game if alive snake number is less than two."""
127 dead_snakes = 0
128 for snake in self.engine.snakes:
129 if snake == None:
130 dead_snakes=dead_snakes+1
131 pass
132 if dead_snakes >= 3:
133 self.end()
134 return False
136 def restart(self, survived):
137 """"Restarts snakes positions after the end of the game
139 Options:
140 survived = "yes" - restarts next round only with snakes survived in previous round
141 survived = "no" - restart next roun with all snakes played in previous round"""
142 if survived == "yes":
143 snake_set = self.engine.snakes
144 else:
145 snake_set = self.engine.psnakes
146 self.step_id = 0
147 for i, snake in enumerate(snake_set):
148 if snake_set[i] != None:
149 self.engine.snakes[i] = snake
150 self.engine.create_snake(i, snake)
151 self.engine.refill()
152 self.engine.redraw()
154 def end (self):
155 """End the game and raise the window that tels about it."""
156 if self.after_id != None:
157 self.canvas.after_cancel(self.after_id)
158 pass
159 self.step_id = self.game_length + 666
160 root = tk.Tk()
161 end_label = tk.Label(root, text="End")
162 end_label.pack()
163 root.mainloop()
164 pass
166 if __name__ == "__main__":
167 snake_batle = UI()
168 snake_batle.root.mainloop()