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

snake

view engine.py @ 124:bc310be50e73

pattern cell always has snake_type
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 15:48:15 +0300
parents 793011c1119e
children b7d2bfd5860d
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)]
7 class Dict(dict):
8 pass
10 class Cell(object):
11 def __init__(self, x, y, canvas = None):
12 self.x = x
13 self.y = y
14 self.canvas = canvas
15 self.snake = None
16 self.type = 'empty'
17 return
18 def redraw(self, offset, c_size):
20 x0=offset[0] + self.x*c_size
21 y0=offset[1] + self.y*c_size
22 x1=offset[0] + (self.x+1)*c_size
23 y1=offset[1] + (self.y+1)*c_size
24 x2=offset[0] + (self.x+1/2.0)*c_size
25 if self.type == 'wall':
26 self.canvas.create_rectangle(x0, y0, x1, y1, fill="grey")
27 pass
28 elif self.type == 'empty':
29 self.canvas.create_rectangle(x0, y0, x1, y1, fill="black")
30 pass
31 elif self.type == 'body':
32 self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.snake.color)
33 pass
34 elif self.type == 'head':
35 self.canvas.create_oval(x0, y0, x1, y1, fill=self.snake.color)
36 pass
37 elif self.type == 'tail':
38 self.canvas.create_polygon(x0, y0, x1, y0, x2, y1, fill=self.snake.color)
39 pass
40 return
41 def __eq__(self, pattern):
42 if pattern.type == 'any':
43 return True
44 if pattern.type != self.type:
45 return False
46 if pattern.snake_type == 'my' and pattern.snake != self.snake:
47 return False
48 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
49 return False
50 return True
51 def clear(self):
52 self.snake = None
53 self.type = 'empty'
54 return
57 class Engine(object):
58 def __init__(self, canvas):
59 self.canvas = canvas
60 self.w = min(canvas.winfo_height(), canvas.winfo_width())
61 self.h = min(canvas.winfo_height(), canvas.winfo_width())
62 self.snakes = [None, None, None, None]
63 self.init_field()
64 self.start_snake_length = 10
65 return
66 def init_field (self):
67 self.field = Dict()
68 self.field.w = 31
69 self.field.h = 31
70 f_w = self.field.w
71 f_h = self.field.h
72 for x in range(f_w):
73 for y in range(f_h):
74 self.field[x, y] = Cell(x, y, self.canvas)
75 for y in range(f_h):
76 self.field[0, y].type = 'wall'
77 self.field[f_w-1, y].type = 'wall'
78 for x in range(1,f_w-1):
79 self.field[x, 0].type = 'wall'
80 self.field[x, f_h-1].type = 'wall'
81 self.refill()
82 self.redraw()
83 return
84 def step(self):
85 for i, snake in enumerate(self.snakes):
86 if snake != None:
87 if len(snake.cells) == 0:
88 self.snakes[i] = None
89 continue
90 self.legal_moves(snake)
91 self.move_snake(snake)
92 self.refill()
93 self.redraw()
94 return
95 def move_snake(self, snake):
96 head = snake.cells[0]
97 for rule in snake.rules:
98 for direction in snake.legal_dir:
99 rule.rotate(direction)
100 if rule.applies(self.field, head.x, head.y) == True:
101 self.move_do(snake, direction)
102 return
103 if snake.legal_dir != []:
104 self.move_do(snake, snake.legal_dir[0])
105 pass
106 return
107 def move_do(self, snake, applied_dir):
108 head = snake.cells[0]
109 dir_cell = self.field[head.x + applied_dir[0], head.y + applied_dir[1]]
110 if dir_cell.type == 'empty':
111 snake.cells.insert(0,dir_cell)
112 del snake.cells[-1]
113 pass
114 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
115 snake.cells.insert(0,dir_cell)
116 del dir_cell.snake.cells[-1]
117 pass
119 def create_snake(self, snake_number):
120 cells_id = []
121 f_h = self.field.h
122 f_w = self.field.w
123 for y in range(self.start_snake_length):
124 cells_id.append(((f_w-1)/2, y+1))
125 for rot_num in range(snake_number - 1):
126 for i, cell in enumerate(cells_id):
127 cells_id[i] = (cell[1],min(f_h, f_w)-cell[0])
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 f_w = self.field.w
140 f_h = self.field.h
141 for x in range(1,f_w-1):
142 for y in range(1,f_h-1):
143 self.field[x, y].type = 'empty'
144 self.field[x, y].snake = None
145 pass
146 for snake in self.snakes:
147 if snake == None:
148 pass
149 else:
150 snake.fill()
151 pass
152 return
153 def redraw(self):
154 self.canvas.delete("all")
155 w = self.canvas.winfo_width()
156 h = self.canvas.winfo_height()
157 cw = w/float(self.field.w)
158 ch = h/float(self.field.h)
159 c = min(cw, ch)
160 field_geometry = (self.field.w*c,self.field.h*c)
161 offset = ((w - field_geometry[0])/2.0, (h - field_geometry[1])/2.0)
162 for cell_coord in self.field:
163 self.field[cell_coord].redraw(offset, c)
164 return
165 def legal_moves(self, snake):
166 snake.legal_dir = []
167 head = snake.cells[0]
168 for direction in directions:
169 dir_cell = self.field[head.x + direction[0], head.y + direction[1]]
170 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
171 snake.legal_dir.append(direction)
172 rnd.shuffle(snake.legal_dir)
173 return