view engine.py @ 80:2f56e186e75c
Fixed initial value for snake.Rule.direction
author |
Danya Alexeyevsky <me.dendik@gmail.com> |
date |
Mon, 20 Dec 2010 01:08:29 +0300 |
parents |
198e91c5b94c |
children |
50056998a673 |
line source
5 directions = [(0,1), (1,0), (0,-1), (-1,0)]
9 def __init__(self, x, y, canvas = None):
17 field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())
18 offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size)
19 if self.type == 'wall':
20 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")
22 elif self.type == 'empty':
23 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="black")
25 elif self.type == 'body':
26 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=self.snake.color)
28 elif self.type == 'head':
29 self.canvas.create_oval(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color)
31 elif self.type == 'tail':
32 self.canvas.create_polygon(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1], offset[0] + self.x*field_size/(2*21.0), offset[1] + self.y*field_size/21.0, fill=self.snake.color)
35 def __eq__(self, pattern):
36 if pattern.type == 'any':
38 if pattern.type != self.type:
40 if pattern.snake_type == 'my' and pattern.snake != self.snake:
42 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
52 def __init__(self, canvas):
54 self.w = min(canvas.winfo_height(), canvas.winfo_width())
55 self.h = min(canvas.winfo_height(), canvas.winfo_width())
56 self.snakes = [None, None, None, None]
59 def init_field (self):
63 self.field[x, y] = Cell(x, y, self.canvas)
65 self.field[0, y].type = 'wall'
66 self.field[20, y].type = 'wall'
68 self.field[x, 0].type = 'wall'
69 self.field[x, 20].type = 'wall'
72 for i, snake in enumerate(self.snakes):
73 if len(snake.cells) == 0:
78 self.legal_moves(snake)
79 self.move_snake(snake)
83 def move_snake(self, snake):
85 for rule in snake.rules:
86 if applied_dir != None:
88 for direction in snake.legal_dir:
89 rule.direction = direction
90 if rule.applies() == True:
91 choose_dir.append(direction)
94 if len(choose_move) != 0:
95 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
98 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
99 if dir_cell.type == 'empty':
100 snake.cells.insert(0,dir_cell)
103 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
104 snake.cells.insert(0,dir_cell)
105 del dir_cell.snake.cells[-1]
108 if applied_dir == None:
109 applied_dir = snake.legal_dir[int(rnd.random()*len(snake.legal_dir))]
110 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
111 if dir_cell.type == 'empty':
112 snake.cells.insert(0,dir_cell)
115 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
116 snake.cells.insert(0,dir_cell)
117 del dir_cell.snake.cells[-1]
121 def create_snake(self, snake_number):
124 cells_id.append((10, y+1))
125 for rot_num in range(snake_number - 1):
126 for i, cell in enumerate(cells_id):
127 cells_id[i] = (tm[0][0]*cell[0] + tm[0][1]*cell[1],tm[1][0]*cell[0] + tm[1][1]*cell[1])
129 for cell in cells_id:
130 cells.append(self.field[cell])
136 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
137 return self.snakes[snake_number-1]
139 for x in range(1,20):
140 for y in range(1,20):
141 self.field[x, y].type = 'empty'
142 self.field[x, y].snake = None
144 for snake in self.snakes:
152 self.canvas.delete(all)
153 for cell_coord in self.field:
154 self.field[cell_coord].redraw()
156 def legal_moves(self, snake):
158 for direction in directions:
159 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
160 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
161 snake.legal_dir.append(direction)