Changeset 160:56e7d0bfd362
Legend:
- Unmodified
- Added
- Removed
-
engine.py
r156 r160 ˆà 186 186 for y in range(self.start_snake_length):ˆà 187 187 cells_id.insert(0,((f_w-1)/2, y+1))ˆà 188 ˆà for rot_num in range(snake_number - 1):ˆàˆà 188 for rot_num in range(snake_number):ˆà 189 189 for i, cell in enumerate(cells_id):ˆà 190 190 cells_id[i] = (min(f_h, f_w)-1-cell[1],cell[0])ˆà òÀæ òÀæ ˆà 193 193 cells.append(self.field[cell])ˆà 194 194 color_dic = {ˆà 195 ˆà 1:'blue',ˆà196 ˆà 2:'green',ˆà197 ˆà 3:'yellow',ˆà198 ˆà 4:'red',}ˆàˆà 195 0:'blue',ˆà ˆà 196 1:'green',ˆà ˆà 197 2:'yellow',ˆà ˆà 198 3:'red',}ˆà 199 199 if old_snake == None:ˆà 200 ˆà self.snakes[snake_number -1] = snake.Snake(cells, color_dic[snake_number])ˆàˆà 200 self.snakes[snake_number] = snake.Snake(cells, color_dic[snake_number])ˆà 201 201 else:ˆà 202 202 old_snake.cells = cellsˆà 203 ˆà self.psnakes[snake_number-1] = self.snakes[snake_number-1]ˆà 204 ˆà return self.snakes[snake_number-1]ˆà ˆà 203 return self.snakes[snake_number]ˆà 205 204 ˆà 206 205 def refill(self):ˆà -
main.py
r156 r160 ˆà 7 7 ˆà 8 8 class UI(object):ˆà 9 ˆà """User Interface:"""ˆà ˆà 9 """User Interface:ˆà ˆà 10 ˆà ˆà 11 Atributes:ˆà ˆà 12 ˆà ˆà 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' - fime of the step"""ˆà 10 19 def __init__ (self):ˆà 11 20 """Create Python Battle game window.ˆà òÀæ òÀæ ˆà 19 28 self.engine = engine.Engine(self.canvas)ˆà 20 29 self.after_id = Noneˆà ˆà 30 self.step_length = 150ˆà 21 31 returnˆà 22 32 ˆà òÀæ òÀæ ˆà 25 35 Definition of button functions."""ˆà 26 36 buttons = tk.Frame(root)ˆà 27 ˆà load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load( 1))ˆàˆà 37 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))ˆà 28 38 load_1.grid(row=1, column=2, stick="news")ˆà 29 ˆà load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load( 2))ˆàˆà 39 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))ˆà 30 40 load_2.grid(row=2, column=3, stick="news")ˆà 31 41 run_b = tk.Button(buttons, text="Run", command=lambda: self.run())ˆà 32 ˆà run_b.grid(row=1, column=5, stick="news")ˆà 33 ˆà load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))ˆà ˆà 42 run_b.grid(row=2, column=2, stick="news")ˆà ˆà 43 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart())ˆà ˆà 44 restart_b.grid(row=1, column=5, stick="news")ˆà ˆà 45 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))ˆà 34 46 load_3.grid(row=3, column=2, stick="news")ˆà 35 ˆà load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load( 4))ˆàˆà 47 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))ˆà 36 48 load_4.grid(row=2, column=1, stick="news")ˆà 37 49 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())ˆà òÀæ òÀæ ˆà 50 62 self.step_id = 0ˆà 51 63 self.engine.snakes = [None, None, None, None]ˆà 52 ˆà self.engine.psnakes = [None, None, None, None]ˆà53 64 passˆà 54 65 if self.step_id == 0:ˆà òÀæ òÀæ ˆà 64 75 ˆà 65 76 def run (self):ˆà 66 ˆà """Run the game with 150 ms step"""ˆà 67 ˆà if self.step_id == 666:ˆà 68 ˆà self.step_id = 0ˆà 69 ˆà for i, snake in enumerate(self.engine.psnakes):ˆà 70 ˆà self.engine.snakes[i] = snakeˆà 71 ˆà self.engine.create_snake(i, snake)ˆà ˆà 77 """Run the game with 'step_length' ms stepˆà ˆà 78 After the end of the game - restarts it with snakes survived inˆà ˆà 79 previous game"""ˆà ˆà 80 self.restart()ˆà 72 81 if self.dead_snake_check() == False:ˆà 73 82 returnˆà òÀæ òÀæ ˆà 77 86 self.step_id = self.step_id+1ˆà 78 87 self.engine.step()ˆà 79 ˆà self.after_id = self.canvas.after( 150, self.run)ˆàˆà 88 self.after_id = self.canvas.after(self.step_length, self.run)ˆà 80 89 returnˆà ˆà 90 ˆà 81 91 def step (self):ˆà 82 92 """Do the next game step"""ˆà òÀæ òÀæ ˆà 106 116 self.end()ˆà 107 117 return Falseˆà ˆà 118 ˆà ˆà 119 def restart(self):ˆà ˆà 120 """"Restarts the game after the end of the game with snakes survived"""ˆà ˆà 121 if self.step_id == 666:ˆà ˆà 122 print "her"ˆà ˆà 123 self.step_id = 0ˆà ˆà 124 for i, snake in enumerate(self.engine.psnakes):ˆà ˆà 125 if self.engine.psnakes[i] != None:ˆà ˆà 126 self.engine.snakes[i] = snakeˆà ˆà 127 self.engine.create_snake(i, snake)ˆà ˆà 128 self.engine.refill()ˆà ˆà 129 self.engine.redraw()ˆà ˆà 130 self.engine.psnakes = self.engine.snakesˆà 108 131 ˆà 109 132 def end (self):ˆà
Note: See TracChangeset
for help on using the changeset viewer.