Changeset 111:9486211ec334
Legend:
- Unmodified
- Added
- Removed
-
engine.py
r108 r111 ˆà 5 5 directions = [(0,1), (1,0), (0,-1), (-1,0)]ˆà 6 6 tm = [[0, -1], [1, 0]]ˆà ˆà 7 ˆà ˆà 8 class Dict(dict):ˆà ˆà 9 passˆà 7 10 ˆà 8 11 class Cell(object):ˆà òÀæ òÀæ ˆà 15 18 returnˆà 16 19 def redraw(self):ˆà 17 ˆà field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())ˆà 18 ˆà offset = ((self.canvas.winfo_width() - field_size)/2.0, (self.canvas.winfo_height() - field_size)/2.0)ˆà 19 ˆà x0=offset[0] + self.x*field_size/21.0ˆà 20 ˆà y0=offset[1] + self.y*field_size/21.0ˆà 21 ˆà x1=offset[0] + (self.x+1)*field_size/21.0ˆà 22 ˆà y1=offset[1] + (self.y+1)*field_size/21.0ˆà 23 ˆà x2=offset[0] + (self.x+1/2.0)*field_size/21.0ˆà ˆà 20 field_geometry = min(self.canvas.winfo_height(), self.canvas.winfo_width())ˆà ˆà 21 f_s = float(self.field.size)ˆà ˆà 22 offset = ((self.canvas.winfo_width() - field_geometry)/2.0, (self.canvas.winfo_height() - field_geometry)/2.0)ˆà ˆà 23 x0=offset[0] + self.x*field_geometry/f_sˆà ˆà 24 y0=offset[1] + self.y*field_geometry/f_sˆà ˆà 25 x1=offset[0] + (self.x+1)*field_geometry/f_sˆà ˆà 26 y1=offset[1] + (self.y+1)*field_geometry/f_sˆà ˆà 27 x2=offset[0] + (self.x+1/2.0)*field_geometry/f_sˆà 24 28 if self.type == 'wall':ˆà 25 29 self.canvas.create_rectangle(x0, y0, x1, y1, fill="grey")ˆà òÀæ òÀæ ˆà 61 65 self.snakes = [None, None, None, None]ˆà 62 66 self.init_field()ˆà ˆà 67 self.start_snake_length = 10ˆà 63 68 returnˆà 64 69 def init_field (self):ˆà 65 ˆà self.field = {}ˆà 66 ˆà for x in range(21):ˆà 67 ˆà for y in range(21):ˆà ˆà 70 self.field = Dict()ˆà ˆà 71 self.field.size = 27ˆà ˆà 72 f_s = self.field.sizeˆà ˆà 73 for x in range(f_s):ˆà ˆà 74 for y in range(f_s):ˆà 68 75 self.field[x, y] = Cell(x, y, self.canvas)ˆà 69 ˆà for y in range( 21):ˆàˆà 76 for y in range(f_s):ˆà 70 77 self.field[0, y].type = 'wall'ˆà 71 ˆà self.field[ 20, y].type = 'wall'ˆà72 ˆà for x in range(1, 20):ˆàˆà 78 self.field[f_s-1, y].type = 'wall'ˆà ˆà 79 for x in range(1,f_s-1):ˆà 73 80 self.field[x, 0].type = 'wall'ˆà 74 ˆà self.field[x, 20].type = 'wall'ˆàˆà 81 self.field[x, f_s-1].type = 'wall'ˆà 75 82 self.refill()ˆà 76 83 self.redraw()ˆà òÀæ òÀæ ˆà 113 120 def create_snake(self, snake_number):ˆà 114 121 cells_id = []ˆà 115 ˆà for y in range(10):ˆà 116 ˆà cells_id.append((10, y+1))ˆà ˆà 122 f_s = self.field.sizeˆà ˆà 123 for y in range(self.start_snake_length):ˆà ˆà 124 cells_id.append(((f_s+1)/2, y+1))ˆà 117 125 for rot_num in range(snake_number - 1):ˆà 118 126 for i, cell in enumerate(cells_id):ˆà òÀæ òÀæ ˆà 129 137 return self.snakes[snake_number-1]ˆà 130 138 def refill(self):ˆà 131 ˆà for x in range(1,20):ˆà 132 ˆà for y in range(1,20):ˆà ˆà 139 f_s = self.field.sizeˆà ˆà 140 for x in range(1,f_s-1):ˆà ˆà 141 for y in range(1,f_s-1):ˆà 133 142 self.field[x, y].type = 'empty'ˆà 134 143 self.field[x, y].snake = Noneˆà -
main.py
r107 r111 ˆà 51 51 self.step_id = self.step_id+1ˆà 52 52 self.engine.step()ˆà 53 ˆà self.after_id = self.canvas.after(300, self.run ())ˆàˆà 53 self.after_id = self.canvas.after(300, self.run)ˆà 54 54 if self.step_id == 200:ˆà 55 55 self.end()ˆà -
snake.py
- Property exe set to *
r104 r111 ˆà 47 47 for cell in self.cells:ˆà 48 48 cell.snake = selfˆà ˆà 49 for cell in self.cells:ˆà ˆà 50 cell.type = 'body'ˆà 49 51 self.cells[0].type = 'head'ˆà 50 ˆà for cell in self.cells[1:-1]:ˆà51 ˆà cell.type = 'body'ˆà52 52 self.cells[-1].type = 'tail'ˆà 53 53 returnˆà
Note: See TracChangeset
for help on using the changeset viewer.