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

snake

view engine.py @ 93:378227a79ebc

added redraw() after field creation
author Alex Martynov
date Mon, 20 Dec 2010 01:57:18 +0300
parents 5d7d9c19b6c2
children 61be18bf77a2
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 = choose_dir[int(rnd.random()*len(choose_dir))]
97 pass
98 else:
99 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
100 self.move_do()
101 break
102 if applied_dir == None:
103 applied_dir = snake.legal_dir[int(rnd.random()*len(snake.legal_dir))]
104 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
105 self.move_do()
106 pass
107 return
108 def move_do(self, snake, dir_cell):
109 if dir_cell.type == 'empty':
110 snake.cells.insert(0,dir_cell)
111 del snake.cells[-1]
112 pass
113 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
114 snake.cells.insert(0,dir_cell)
115 del dir_cell.snake.cells[-1]
116 pass
118 def create_snake(self, snake_number):
119 cells_id = []
120 for y in range(10):
121 cells_id.append((10, y+1))
122 for rot_num in range(snake_number - 1):
123 for i, cell in enumerate(cells_id):
124 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)
125 cells = []
126 for cell in cells_id:
127 cells.append(self.field[cell])
128 color_dic = {
129 1:'blue',
130 2:'green',
131 3:'yellow',
132 4:'red',}
133 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
134 return self.snakes[snake_number-1]
135 def refill(self):
136 for x in range(1,20):
137 for y in range(1,20):
138 self.field[x, y].type = 'empty'
139 self.field[x, y].snake = None
140 pass
141 for snake in self.snakes:
142 if snake == None:
143 pass
144 else:
145 snake.fill()
146 pass
147 return
148 def redraw(self):
149 self.canvas.delete(all)
150 for cell_coord in self.field:
151 self.field[cell_coord].redraw()
152 return
153 def legal_moves(self, snake):
154 snake.legal_dir = []
155 for direction in directions:
156 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
157 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
158 snake.legal_dir.append(direction)
159 rnd.shuffle(snake.legal_dir)
160 return