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

snake

view engine.py @ 96:3619305694ad

Actually fixed buttons resizing. Fixed code separation for buttons drawing.
author Danya Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 02:26:35 +0300
parents 378227a79ebc
children 1e97abfe23bc
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 self.redraw()
71 return
72 def step(self):
73 for i, snake in enumerate(self.snakes):
74 if len(snake.cells) == 0:
75 self.snakes[i] = None
76 if snake == None:
77 pass
78 else:
79 self.legal_moves(snake)
80 self.move_snake(snake)
81 self.refill()
82 self.redraw()
83 return
84 def move_snake(self, snake):
85 applied_dir = None
86 for rule in snake.rules:
87 if applied_dir != None:
88 choose_dir = []
89 for direction in snake.legal_dir:
90 rule.direction = direction
91 if rule.applies() == True:
92 choose_dir.append(direction)
93 pass
94 pass
95 if len(choose_move) != 0:
96 applied_dir = rnd.shuffle(choose_dir)[0]
97 pass
98 else:
99 self.move_do(snake, applied_dir)
100 break
101 if applied_dir == None:
102 applied_dir = snake.legal_dir[0]
103 self.move_do(snake, applied_dir)
104 pass
105 return
106 def move_do(self, snake, applied_dir):
107 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
108 if dir_cell.type == 'empty':
109 snake.cells.insert(0,dir_cell)
110 del snake.cells[-1]
111 pass
112 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
113 snake.cells.insert(0,dir_cell)
114 del dir_cell.snake.cells[-1]
115 pass
117 def create_snake(self, snake_number):
118 cells_id = []
119 for y in range(10):
120 cells_id.append((10, y+1))
121 for rot_num in range(snake_number - 1):
122 for i, cell in enumerate(cells_id):
123 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)
124 cells = []
125 for cell in cells_id:
126 cells.append(self.field[cell])
127 color_dic = {
128 1:'blue',
129 2:'green',
130 3:'yellow',
131 4:'red',}
132 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
133 return self.snakes[snake_number-1]
134 def refill(self):
135 for x in range(1,20):
136 for y in range(1,20):
137 self.field[x, y].type = 'empty'
138 self.field[x, y].snake = None
139 pass
140 for snake in self.snakes:
141 if snake == None:
142 pass
143 else:
144 snake.fill()
145 pass
146 return
147 def redraw(self):
148 self.canvas.delete(all)
149 for cell_coord in self.field:
150 self.field[cell_coord].redraw()
151 return
152 def legal_moves(self, snake):
153 snake.legal_dir = []
154 for direction in directions:
155 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
156 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
157 snake.legal_dir.append(direction)
158 rnd.shuffle(snake.legal_dir)
159 return