Changeset 32:b2eaeeb74d87
Legend:
- Unmodified
- Added
- Removed
-
engine.py
r31 r32 ˆà 14 14 returnˆà 15 15 def redraw(self):ˆà 16 ˆà field_size = min( canvas.height,canvas.width)ˆà17 ˆà offset = ( canvas.width - field_size,canvas.hieght - field_size)ˆàˆà 16 field_size = min(self.canvas.height, self.canvas.width)ˆà ˆà 17 offset = (self.canvas.width - field_size, self.canvas.hieght - field_size)ˆà 18 18 if self.type == 'wall':ˆà 19 19 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="grey")ˆà òÀæ òÀæ ˆà 113 113 returnˆà 114 114 def create_snake(self, snake_number):ˆà ˆà 115 ˆà 115 116 passˆà 116 117 def refill(self):ˆà òÀæ òÀæ ˆà 129 130 def redraw(self):ˆà 130 131 self.canvas.delete(all)ˆà 131 ˆà for cell in self.field:ˆà132 ˆà cell.redraw()ˆàˆà 132 for cell_coord in self.field:ˆà ˆà 133 self.field[cell_coord].redraw()ˆà 133 134 returnˆà 134 135 def legal_moves(self, snake):ˆà -
main.py
r29 r32 ˆà 2 2 import tkFileDialog as tkfdˆà 3 3 import engineˆà ˆà 4 import snakeˆà 4 5 ˆà 5 6 ˆà òÀæ òÀæ ˆà 16 17 self.step_id = 0ˆà 17 18 self.engine = engine.Engine(self.canvas)ˆà ˆà 19 self.after_id = Noneˆà 18 20 returnˆà 19 21 def buttons_pack(self, frame):ˆà 20 ˆà load_1 = tk.Button(frame, text="Load ", command=lambda: self.load(1))ˆà21 ˆà load_1.pack(side=" top", fill="both", expand = "yes")ˆà22 ˆà load_2 = tk.Button(frame, text="Load ", command=lambda: self.load(2))ˆà23 ˆà load_2.pack(side=" top", fill="both", expand = "yes")ˆàˆà 22 load_1 = tk.Button(frame, text="Load 1", command=lambda: self.load(1))ˆà ˆà 23 load_1.pack(side="left", fill="both", expand = "yes")ˆà ˆà 24 load_2 = tk.Button(frame, text="Load 2", command=lambda: self.load(2))ˆà ˆà 25 load_2.pack(side="left", fill="both", expand = "yes")ˆà 24 26 run_b = tk.Button(frame, text="Run", command=lambda: self.run())ˆà 25 ˆà run_b.pack(side=" top", fill="both", expand = "yes")ˆà26 ˆà load_3 = tk.Button(frame, text="Load ", command=lambda: self.load(3))ˆà27 ˆà load_3.pack(side=" bottom", fill="both", expand = "yes")ˆà28 ˆà load_4 = tk.Button(frame, text="Load ", command=lambda: self.load(4))ˆà29 ˆà load_4.pack(side=" bottom", fill="both", expand = "yes")ˆàˆà 27 run_b.pack(side="left", fill="both", expand = "yes")ˆà ˆà 28 load_3 = tk.Button(frame, text="Load 3", command=lambda: self.load(3))ˆà ˆà 29 load_3.pack(side="right", fill="both", expand = "yes")ˆà ˆà 30 load_4 = tk.Button(frame, text="Load 4", command=lambda: self.load(4))ˆà ˆà 31 load_4.pack(side="right", fill="both", expand = "yes")ˆà 30 32 step_b = tk.Button(frame, text="Step", command=lambda: self.step())ˆà 31 ˆà step_b.pack(side=" bottom", fill="both", expand = "yes")ˆàˆà 33 step_b.pack(side="right", fill="both", expand = "yes")ˆà 32 34 returnˆà 33 35 ˆà òÀæ òÀæ ˆà 46 48 ˆà 47 49 def run (self):ˆà 48 ˆà self.step_id = self. id+1ˆàˆà 50 self.step_id = self.step_id+1ˆà 49 51 self.engine.step()ˆà 50 52 self.after_id = self.canvas.after(300, self.run())ˆà òÀæ òÀæ ˆà 55 57 returnˆà 56 58 def step (self):ˆà 57 ˆà if self.id <= 200:ˆà 58 ˆà self.canvas.after_cancel(self.after_id)ˆà 59 ˆà self.id = self.id+1ˆà ˆà 59 if self.step_id <= 200:ˆà ˆà 60 if self.after_id != None:ˆà ˆà 61 self.canvas.after_cancel(self.after_id)ˆà ˆà 62 passˆà ˆà 63 self.step_id = self.step_id+1ˆà 60 64 self.engine.step()ˆà 61 65 passˆà òÀæ òÀæ ˆà 71 75 root.mainloop()ˆà 72 76 passˆà ˆà 77 ˆà ˆà 78 snake_batle = UI()ˆà ˆà 79 snake_batle.root.mainloop()ˆà 73 80 ˆà -
snake.py
r31 r32 ˆà 1 1 ˆà 2 2 class Snake(object):ˆà 3 ˆà def __init__ (self, cells):ˆà ˆà 3 def __init__ (self, cells, color):ˆà ˆà 4 self.cells = cellsˆà ˆà 5 self.color = colorˆà ˆà 6 self.rules = []ˆà 4 7 passˆà 5 8 def load (self, file):ˆà ˆà 9 ˆà 6 10 passˆà 7 11 def fill (self):ˆà 8 ˆà for sell in self.sells:ˆà9 ˆà sell.snake = selfˆàˆà 12 for cell in self.cells:ˆà ˆà 13 cell.snake = selfˆà 10 14 snake.cells[0].type = 'head'ˆà 11 15 snake.cells[-1].type = 'tail'ˆà òÀæ òÀæ ˆà 15 19 ˆà 16 20 class Rule(object):ˆà 17 ˆà def __init__ (self):ˆà ˆà 21 def __init__ (self, snake):ˆà ˆà 22 self.snake = snakeˆà ˆà 23 self.direction = (1, -1)ˆà 18 24 passˆà 19 ˆà def load (file):ˆà ˆà 25 def load (self, file):ˆà ˆà 26 ˆà 20 27 passˆà 21 ˆà def applies (field, x, y):ˆà ˆà 28 def applies (self, field, x, y):ˆà ˆà 29 ˆà 22 30 passˆà 23 ˆà def rotate ( rot):ˆàˆà 31 def rotate (self, rot):ˆà 24 32 passˆà
Note: See TracChangeset
for help on using the changeset viewer.