Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/raw-rev/8b348841afd2
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 07:14:12 2012
Кодировка:

# HG changeset patch
# User Alex Martynov
# Date 1293035776 -10800
# Node ID 8b348841afd299c6649a3fb555d9f6b8575b81d4
# Parent 37d7d9c8d9758ae87d78c452ce96e487af6cd290# Parent 56e7d0bfd362330f136c2dc452adeff3b6b6aa7c
merge

diff -r 37d7d9c8d975 -r 8b348841afd2 engine.py
--- a/engine.py Wed Dec 22 00:06:21 2010 +0300
+++ b/engine.py Wed Dec 22 19:36:16 2010 +0300
@@ -185,23 +185,22 @@
f_w = self.field.w
for y in range(self.start_snake_length):
cells_id.insert(0,((f_w-1)/2, y+1))
- for rot_num in range(snake_number - 1):
+ for rot_num in range(snake_number):
for i, cell in enumerate(cells_id):
cells_id[i] = (min(f_h, f_w)-1-cell[1],cell[0])
cells = []
for cell in cells_id:
cells.append(self.field[cell])
color_dic = {
- 1:'blue',
- 2:'green',
- 3:'yellow',
- 4:'red',}
+ 0:'blue',
+ 1:'green',
+ 2:'yellow',
+ 3:'red',}
if old_snake == None:
- self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
+ self.snakes[snake_number] = snake.Snake(cells, color_dic[snake_number])
else:
old_snake.cells = cells
- self.psnakes[snake_number-1] = self.snakes[snake_number-1]
- return self.snakes[snake_number-1]
+ return self.snakes[snake_number]

def refill(self):
"""Refill the field cells types and snakes according to the actual
diff -r 37d7d9c8d975 -r 8b348841afd2 main.py
--- a/main.py Wed Dec 22 00:06:21 2010 +0300
+++ b/main.py Wed Dec 22 19:36:16 2010 +0300
@@ -6,7 +6,16 @@


class UI(object):
- """User Interface:"""
+ """User Interface:
+
+ Atributes:
+
+ - 'root' - root Window game placed at
+ - 'engine' - engine of the game
+ - 'canvas' - Widget field is pictured at
+ - 'step_id' - current step of the game
+ - 'after_id' - identificator of runing game process
+ - 'step_legth' - fime of the step"""
def __init__ (self):
"""Create Python Battle game window.
Initialyze engige of the game."""
@@ -18,21 +27,24 @@
self.step_id = 0
self.engine = engine.Engine(self.canvas)
self.after_id = None
+ self.step_length = 150
return

def buttons_pack(self, root):
"""Packing the buttons in root frame.
Definition of button functions."""
buttons = tk.Frame(root)
- load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1))
+ load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
load_1.grid(row=1, column=2, stick="news")
- load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2))
+ load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
load_2.grid(row=2, column=3, stick="news")
run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
- run_b.grid(row=1, column=5, stick="news")
- load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))
+ run_b.grid(row=2, column=2, stick="news")
+ restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart())
+ restart_b.grid(row=1, column=5, stick="news")
+ load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
load_3.grid(row=3, column=2, stick="news")
- load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4))
+ load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
load_4.grid(row=2, column=1, stick="news")
step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
step_b.grid(row=2, column=5, stick="news")
@@ -49,7 +61,6 @@
if self.step_id == 666:
self.step_id = 0
self.engine.snakes = [None, None, None, None]
- self.engine.psnakes = [None, None, None, None]
pass
if self.step_id == 0:
file = tkfd.askopenfile(title="Open file")
@@ -63,12 +74,10 @@
return

def run (self):
- """Run the game with 150 ms step"""
- if self.step_id == 666:
- self.step_id = 0
- for i, snake in enumerate(self.engine.psnakes):
- self.engine.snakes[i] = snake
- self.engine.create_snake(i, snake)
+ """Run the game with 'step_length' ms step
+ After the end of the game - restarts it with snakes survived in
+ previous game"""
+ self.restart()
if self.dead_snake_check() == False:
return
if self.step_id > 200:
@@ -76,8 +85,9 @@
return
self.step_id = self.step_id+1
self.engine.step()
- self.after_id = self.canvas.after(150, self.run)
+ self.after_id = self.canvas.after(self.step_length, self.run)
return
+
def step (self):
"""Do the next game step"""
if self.dead_snake_check() == False:
@@ -105,6 +115,19 @@
if dead_snakes >= 3:
self.end()
return False
+
+ def restart(self):
+ """"Restarts the game after the end of the game with snakes survived"""
+ if self.step_id == 666:
+ print "her"
+ self.step_id = 0
+ for i, snake in enumerate(self.engine.psnakes):
+ if self.engine.psnakes[i] != None:
+ self.engine.snakes[i] = snake
+ self.engine.create_snake(i, snake)
+ self.engine.refill()
+ self.engine.redraw()
+ self.engine.psnakes = self.engine.snakes

def end (self):
"""End the game and raise the window that tels about it."""