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

snake

view engine.py @ 57:2f62804e21fc

Added example snake
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 00:03:35 +0300
parents b2eaeeb74d87
children f12238066c6d
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.height, self.canvas.width)
17 offset = (self.canvas.width - field_size, self.canvas.hieght - 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 == self.type:
36 return True
37 else:
38 return False
39 return
40 def clear(self):
41 self.snake = None
42 self.type = 'empty'
43 return
46 class Engine(object):
47 def __init__(self, canvas):
48 self.canvas = canvas
49 #self.w = min(canvas.height, canvas.width)
50 #self.h = min(canvas.height, canvas.width)
51 self.snakes = [None, None, None, None]
52 self.init_field()
53 return
54 def init_field (self):
55 self.field = {}
56 for x in range(21):
57 for y in range(21):
58 self.field[x, y] = Cell(x, y, self.canvas)
59 for y in range(21):
60 self.field[0, y].type = 'wall'
61 self.field[20, y].type = 'wall'
62 for x in range(1,20):
63 self.field[x, 0].type = 'wall'
64 self.field[x, 20].type = 'wall'
65 return
66 def step(self):
67 for snake in self.snakes:
68 if snake == None:
69 pass
70 else:
71 self.legal_moves()
72 self.move_snake(snake)
73 self.refill()
74 self.redraw()
75 return
76 def move_snake(self, snake):
77 applied_dir = None
78 for rule in snake.rules:
79 if applied_dir != None:
80 choose_dir = []
81 for direction in snake.legal_dir:
82 rule.direction = direction
83 if rule.applies() == True:
84 choose_dir.append(direction)
85 pass
86 pass
87 if len(choose_move) != 0:
88 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
89 pass
90 else:
91 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
92 if dir_cell.type == 'empty':
93 snake.cells.insert(0,dir_cell)
94 del snake.cells[-1]
95 pass
96 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
97 snake.cells.insert(0,dir_cell)
98 del dir_cell.snake.cells[-1]
99 pass
100 break
101 if applied_dir == None:
102 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]
103 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
104 if dir_cell.type == 'empty':
105 snake.cells.insert(0,dir_cell)
106 del snake.cells[-1]
107 pass
108 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
109 snake.cells.insert(0,dir_cell)
110 del dir_cell.snake.cells[-1]
111 pass
112 pass
113 return
114 def create_snake(self, snake_number):
115 cells_id = []
116 for y in range(10):
117 cells_id.append(10, y+1)
118 for rot_num in range(snake_number - 1):
119 for sell in cells_id:
120 cells_id[cells_id.index(sell)] = (tm[0][0]*sell[0] + tm[0][1]*sell[1],tm[1][0]*sell[0] + tm[1][1]*sell[1])
121 cells = []
122 for cell in cells_id:
123 cells.append(self.field[sell])
124 color_dic = {
125 1:'blue',
126 2:'green',
127 3:'yellow',
128 4:'red',}
129 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
130 pass
131 def refill(self):
132 for x in range(1,20):
133 for y in range(1,20):
134 self.field[x, y].type = 'empty'
135 self.field[x, y].snake = None
136 pass
137 for snake in self.snakes:
138 if snake == None:
139 pass
140 else:
141 snake.fill()
142 pass
143 return
144 def redraw(self):
145 self.canvas.delete(all)
146 for cell_coord in self.field:
147 self.field[cell_coord].redraw()
148 return
149 def legal_moves(self, snake):
150 snake.legal_dir = []
151 for direction in directions:
152 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
153 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
154 snake.legal_dir.append(direction)
155 return
156 return