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

snake

view engine.py @ 62:eefa136de996

enumerate simplification
author Alex Martynov
date Mon, 20 Dec 2010 00:28:05 +0300
parents a3bb04e72924
children 547cc748b4be
line source
1 import random as rnd
2 import Tkinter as tk
4 directions = [(0,1), (1,0), (0,-1), (-1,0)]
5 tm = [[0, -1], [1, 0]]
7 class Cell(object):
8 def __init__(self, x, y, canvas = None):
9 self.x = x
10 self.y = y
11 self.canvas = canvas
12 self.snake = None
13 self.type = 'empty'
14 return
15 def redraw(self):
16 field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())
17 offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size)
18 if self.type == 'wall':
19 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")
20 pass
21 elif self.type == 'empty':
22 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")
23 pass
24 elif self.type == 'body':
25 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)
26 pass
27 elif self.type == 'head':
28 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)
29 pass
30 elif self.type == 'tail':
31 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)
32 pass
33 return
34 def __eq__(self, pattern):
35 if pattern.type == 'any':
36 return True
37 if pattern.type != self.type:
38 return False
39 if pattern.snake_type == 'my' and pattern.snake != self.snake:
40 return False
41 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
42 return False
43 return True
44 def clear(self):
45 self.snake = None
46 self.type = 'empty'
47 return
50 class Engine(object):
51 def __init__(self, canvas):
52 self.canvas = canvas
53 self.w = min(canvas.winfo_height(), canvas.winfo_width())
54 self.h = min(canvas.winfo_height(), canvas.winfo_width())
55 self.snakes = [None, None, None, None]
56 self.init_field()
57 return
58 def init_field (self):
59 self.field = {}
60 for x in range(21):
61 for y in range(21):
62 self.field[x, y] = Cell(x, y, self.canvas)
63 for y in range(21):
64 self.field[0, y].type = 'wall'
65 self.field[20, y].type = 'wall'
66 for x in range(1,20):
67 self.field[x, 0].type = 'wall'
68 self.field[x, 20].type = 'wall'
69 return
70 def step(self):
71 for i, snake in enumerate(self.snakes):
72 if len(snake.cells) == 0:
73 self.snakes[i] = None
74 if snake == None:
75 pass
76 else:
77 self.legal_moves()
78 self.move_snake(snake)
79 self.refill()
80 self.redraw()
81 return
82 def move_snake(self, snake):
83 applied_dir = None
84 for rule in snake.rules:
85 if applied_dir != None:
86 choose_dir = []
87 for direction in snake.legal_dir:
88 rule.direction = direction
89 if rule.applies() == True:
90 choose_dir.append(direction)
91 pass
92 pass
93 if len(choose_move) != 0:
94 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
95 pass
96 else:
97 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
98 if dir_cell.type == 'empty':
99 snake.cells.insert(0,dir_cell)
100 del snake.cells[-1]
101 pass
102 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
103 snake.cells.insert(0,dir_cell)
104 del dir_cell.snake.cells[-1]
105 pass
106 break
107 if applied_dir == None:
108 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]
109 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + 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
118 pass
119 return
120 def create_snake(self, snake_number):
121 cells_id = []
122 for y in range(10):
123 cells_id.append((10, y+1))
124 for rot_num in range(snake_number - 1):
125 for i, cell in enumerate(cells_id):
126 cells_id[i] = (tm[0][0]*cell[0] + tm[0][1]*cell[1],tm[1][0]*cell[0] + tm[1][1]*cell[1])
127 cells = []
128 for cell in cells_id:
129 cells.append(self.field[cell])
130 color_dic = {
131 1:'blue',
132 2:'green',
133 3:'yellow',
134 4:'red',}
135 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
136 pass
137 def refill(self):
138 for x in range(1,20):
139 for y in range(1,20):
140 self.field[x, y].type = 'empty'
141 self.field[x, y].snake = None
142 pass
143 for snake in self.snakes:
144 if snake == None:
145 pass
146 else:
147 snake.fill()
148 pass
149 return
150 def redraw(self):
151 self.canvas.delete(all)
152 for cell_coord in self.field:
153 self.field[cell_coord].redraw()
154 return
155 def legal_moves(self, snake):
156 snake.legal_dir = []
157 for direction in directions:
158 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
159 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
160 snake.legal_dir.append(direction)
161 return
162 return