snake
changeset 160:56e7d0bfd362
changed numeration of the snakes
edited documentation
changed button placement
added restart button
added UI.step_length
author | Alex Martynov |
---|---|
date | Wed, 22 Dec 2010 19:35:56 +0300 |
parents | 7a4853ff834f |
children | 8b348841afd2 |
files | engine.py main.py |
diffstat | 2 files changed, 44 insertions(+), 22 deletions(-) [+] |
line diff
1.1 --- a/engine.py Tue Dec 21 23:01:07 2010 +0300 1.2 +++ b/engine.py Wed Dec 22 19:35:56 2010 +0300 1.3 @@ -185,23 +185,22 @@ 1.4 f_w = self.field.w 1.5 for y in range(self.start_snake_length): 1.6 cells_id.insert(0,((f_w-1)/2, y+1)) 1.7 - for rot_num in range(snake_number - 1): 1.8 + for rot_num in range(snake_number): 1.9 for i, cell in enumerate(cells_id): 1.10 cells_id[i] = (min(f_h, f_w)-1-cell[1],cell[0]) 1.11 cells = [] 1.12 for cell in cells_id: 1.13 cells.append(self.field[cell]) 1.14 color_dic = { 1.15 - 1:'blue', 1.16 - 2:'green', 1.17 - 3:'yellow', 1.18 - 4:'red',} 1.19 + 0:'blue', 1.20 + 1:'green', 1.21 + 2:'yellow', 1.22 + 3:'red',} 1.23 if old_snake == None: 1.24 - self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number]) 1.25 + self.snakes[snake_number] = snake.Snake(cells, color_dic[snake_number]) 1.26 else: 1.27 old_snake.cells = cells 1.28 - self.psnakes[snake_number-1] = self.snakes[snake_number-1] 1.29 - return self.snakes[snake_number-1] 1.30 + return self.snakes[snake_number] 1.31 1.32 def refill(self): 1.33 """Refill the field cells types and snakes according to the actual
2.1 --- a/main.py Tue Dec 21 23:01:07 2010 +0300 2.2 +++ b/main.py Wed Dec 22 19:35:56 2010 +0300 2.3 @@ -6,7 +6,16 @@ 2.4 2.5 2.6 class UI(object): 2.7 - """User Interface:""" 2.8 + """User Interface: 2.9 + 2.10 + Atributes: 2.11 + 2.12 + - 'root' - root Window game placed at 2.13 + - 'engine' - engine of the game 2.14 + - 'canvas' - Widget field is pictured at 2.15 + - 'step_id' - current step of the game 2.16 + - 'after_id' - identificator of runing game process 2.17 + - 'step_legth' - fime of the step""" 2.18 def __init__ (self): 2.19 """Create Python Battle game window. 2.20 Initialyze engige of the game.""" 2.21 @@ -18,21 +27,24 @@ 2.22 self.step_id = 0 2.23 self.engine = engine.Engine(self.canvas) 2.24 self.after_id = None 2.25 + self.step_length = 150 2.26 return 2.27 2.28 def buttons_pack(self, root): 2.29 """Packing the buttons in root frame. 2.30 Definition of button functions.""" 2.31 buttons = tk.Frame(root) 2.32 - load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1)) 2.33 + load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0)) 2.34 load_1.grid(row=1, column=2, stick="news") 2.35 - load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2)) 2.36 + load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1)) 2.37 load_2.grid(row=2, column=3, stick="news") 2.38 run_b = tk.Button(buttons, text="Run", command=lambda: self.run()) 2.39 - run_b.grid(row=1, column=5, stick="news") 2.40 - load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3)) 2.41 + run_b.grid(row=2, column=2, stick="news") 2.42 + restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart()) 2.43 + restart_b.grid(row=1, column=5, stick="news") 2.44 + load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2)) 2.45 load_3.grid(row=3, column=2, stick="news") 2.46 - load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4)) 2.47 + load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3)) 2.48 load_4.grid(row=2, column=1, stick="news") 2.49 step_b = tk.Button(buttons, text="Step", command=lambda: self.step()) 2.50 step_b.grid(row=2, column=5, stick="news") 2.51 @@ -49,7 +61,6 @@ 2.52 if self.step_id == 666: 2.53 self.step_id = 0 2.54 self.engine.snakes = [None, None, None, None] 2.55 - self.engine.psnakes = [None, None, None, None] 2.56 pass 2.57 if self.step_id == 0: 2.58 file = tkfd.askopenfile(title="Open file") 2.59 @@ -63,12 +74,10 @@ 2.60 return 2.61 2.62 def run (self): 2.63 - """Run the game with 150 ms step""" 2.64 - if self.step_id == 666: 2.65 - self.step_id = 0 2.66 - for i, snake in enumerate(self.engine.psnakes): 2.67 - self.engine.snakes[i] = snake 2.68 - self.engine.create_snake(i, snake) 2.69 + """Run the game with 'step_length' ms step 2.70 + After the end of the game - restarts it with snakes survived in 2.71 + previous game""" 2.72 + self.restart() 2.73 if self.dead_snake_check() == False: 2.74 return 2.75 if self.step_id > 200: 2.76 @@ -76,8 +85,9 @@ 2.77 return 2.78 self.step_id = self.step_id+1 2.79 self.engine.step() 2.80 - self.after_id = self.canvas.after(150, self.run) 2.81 + self.after_id = self.canvas.after(self.step_length, self.run) 2.82 return 2.83 + 2.84 def step (self): 2.85 """Do the next game step""" 2.86 if self.dead_snake_check() == False: 2.87 @@ -105,6 +115,19 @@ 2.88 if dead_snakes >= 3: 2.89 self.end() 2.90 return False 2.91 + 2.92 + def restart(self): 2.93 + """"Restarts the game after the end of the game with snakes survived""" 2.94 + if self.step_id == 666: 2.95 + print "her" 2.96 + self.step_id = 0 2.97 + for i, snake in enumerate(self.engine.psnakes): 2.98 + if self.engine.psnakes[i] != None: 2.99 + self.engine.snakes[i] = snake 2.100 + self.engine.create_snake(i, snake) 2.101 + self.engine.refill() 2.102 + self.engine.redraw() 2.103 + self.engine.psnakes = self.engine.snakes 2.104 2.105 def end (self): 2.106 """End the game and raise the window that tels about it."""