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

snake

view engine.py @ 70:e8eabd9530a1

snake.Rule.load returns self for convenience
author Danya Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 00:43:36 +0300
parents eefa136de996
children 198e91c5b94c
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, 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")
21 pass
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")
24 pass
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)
27 pass
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)
30 pass
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)
33 pass
34 return
35 def __eq__(self, pattern):
36 if pattern.type == 'any':
37 return True
38 if pattern.type != self.type:
39 return False
40 if pattern.snake_type == 'my' and pattern.snake != self.snake:
41 return False
42 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
43 return False
44 return True
45 def clear(self):
46 self.snake = None
47 self.type = 'empty'
48 return
51 class Engine(object):
52 def __init__(self, canvas):
53 self.canvas = 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]
57 self.init_field()
58 return
59 def init_field (self):
60 self.field = {}
61 for x in range(21):
62 for y in range(21):
63 self.field[x, y] = Cell(x, y, self.canvas)
64 for y in range(21):
65 self.field[0, y].type = 'wall'
66 self.field[20, y].type = 'wall'
67 for x in range(1,20):
68 self.field[x, 0].type = 'wall'
69 self.field[x, 20].type = 'wall'
70 return
71 def step(self):
72 for i, snake in enumerate(self.snakes):
73 if len(snake.cells) == 0:
74 self.snakes[i] = None
75 if snake == None:
76 pass
77 else:
78 self.legal_moves(snake)
79 self.move_snake(snake)
80 self.refill()
81 self.redraw()
82 return
83 def move_snake(self, snake):
84 applied_dir = None
85 for rule in snake.rules:
86 if applied_dir != None:
87 choose_dir = []
88 for direction in snake.legal_dir:
89 rule.direction = direction
90 if rule.applies() == True:
91 choose_dir.append(direction)
92 pass
93 pass
94 if len(choose_move) != 0:
95 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
96 pass
97 else:
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)
101 del snake.cells[-1]
102 pass
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]
106 pass
107 break
108 if applied_dir == None:
109 applied_dir = legal_dir[int(rnd.random()*len(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)
113 del snake.cells[-1]
114 pass
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]
118 pass
119 pass
120 return
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] + tm[0][1]*cell[1],tm[1][0]*cell[0] + tm[1][1]*cell[1])
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 pass
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 return
163 return