Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/c68b54a43501/engine.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 09:48:08 2013
Кодировка:
snake: c68b54a43501 engine.py

snake

view engine.py @ 104:c68b54a43501

Rule.rotate now accepts target direction, not number of times to rotate
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 11:22:26 +0300
parents a804d1e0e69e
children 020c62119e6f
line source
1 import random as rnd
2 import Tkinter as tk
3 import snake
5 directions = [(0,1), (1,0), (0,-1), (-1,0)]
6 tm = [[0, -1], [1, 0]]
8 class Cell(object):
9 def __init__(self, x, y, canvas = None):
10 self.x = x
11 self.y = y
12 self.canvas = canvas
13 self.snake = None
14 self.type = 'empty'
15 return
16 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
24 if self.type == 'wall':
25 self.canvas.create_rectangle(x0, y0, x1, y1, fill="grey")
26 pass
27 elif self.type == 'empty':
28 self.canvas.create_rectangle(x0, y0, x1, y1, fill="black")
29 pass
30 elif self.type == 'body':
31 self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.snake.color)
32 pass
33 elif self.type == 'head':
34 self.canvas.create_oval(x0, y0, x1, y1, fill=self.snake.color)
35 pass
36 elif self.type == 'tail':
37 self.canvas.create_polygon(x0, y0, x1, y0, x2, y1, fill=self.snake.color)
38 pass
39 return
40 def __eq__(self, pattern):
41 if pattern.type == 'any':
42 return True
43 if pattern.type != self.type:
44 return False
45 if pattern.snake_type == 'my' and pattern.snake != self.snake:
46 return False
47 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
48 return False
49 return True
50 def clear(self):
51 self.snake = None
52 self.type = 'empty'
53 return
56 class Engine(object):
57 def __init__(self, canvas):
58 self.canvas = canvas
59 self.w = min(canvas.winfo_height(), canvas.winfo_width())
60 self.h = min(canvas.winfo_height(), canvas.winfo_width())
61 self.snakes = [None, None, None, None]
62 self.init_field()
63 return
64 def init_field (self):
65 self.field = {}
66 for x in range(21):
67 for y in range(21):
68 self.field[x, y] = Cell(x, y, self.canvas)
69 for y in range(21):
70 self.field[0, y].type = 'wall'
71 self.field[20, y].type = 'wall'
72 for x in range(1,20):
73 self.field[x, 0].type = 'wall'
74 self.field[x, 20].type = 'wall'
75 self.refill()
76 self.redraw()
77 return
78 def step(self):
79 for i, snake in enumerate(self.snakes):
80 if snake != None:
81 if len(snake.cells) == 0:
82 self.snakes[i] = None
83 continue
84 self.legal_moves(snake)
85 self.move_snake(snake)
86 self.refill()
87 self.redraw()
88 return
89 def move_snake(self, snake):
90 dir_to_numb_of_rot={
91 (0,-1):0,
92 (1,0):1,
93 (0,1):2,
94 (-1,0):3}
95 applied_dir = None
96 for rule in snake.rules:
97 if applied_dir == None:
98 for direction in snake.legal_dir:
99 rule.rotate(dir_to_numb_of_rot[direction])
100 if rule.applies(self.field, snake.cells[0].x, snake.cells[0].y) == True:
101 applied_dir = direction
102 break
103 else:
104 self.move_do(snake, applied_dir)
105 break
106 if applied_dir == None:
107 self.move_do(snake, snake.legal_dir[0])
108 pass
109 return
110 def move_do(self, snake, applied_dir):
111 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
112 if dir_cell.type == 'empty':
113 snake.cells.insert(0,dir_cell)
114 del snake.cells[-1]
115 pass
116 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
117 snake.cells.insert(0,dir_cell)
118 del dir_cell.snake.cells[-1]
119 pass
121 def create_snake(self, snake_number):
122 cells_id = []
123 for y in range(10):
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]-10) + tm[0][1]*(cell[1]-10))+10,(tm[1][0]*(cell[0]-10) + tm[1][1]*(cell[1]-10))+10)
128 cells = []
129 for cell in cells_id:
130 cells.append(self.field[cell])
131 color_dic = {
132 1:'blue',
133 2:'green',
134 3:'yellow',
135 4:'red',}
136 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
137 return self.snakes[snake_number-1]
138 def refill(self):
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
143 pass
144 for snake in self.snakes:
145 if snake == None:
146 pass
147 else:
148 snake.fill()
149 pass
150 return
151 def redraw(self):
152 self.canvas.delete(all)
153 for cell_coord in self.field:
154 self.field[cell_coord].redraw()
155 return
156 def legal_moves(self, snake):
157 snake.legal_dir = []
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)
162 rnd.shuffle(snake.legal_dir)
163 return