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

Поисковые слова: tail
snake: 94945f11c78d engine.py

snake

view engine.py @ 40:94945f11c78d

snake.Snake.fill: tail takes precedence over body & head
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Sun, 19 Dec 2010 22:46:39 +0300
parents 76d0514d1ef9
children 85221e4417b7
line source
1 import random as rnd
2 import Tkinter as tk
4 directions = [(0,1), (1,0), (0,-1), (-1,0)]
5 rtm = [[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):
116 pass
117 def refill(self):
118 for x in range(1,20):
119 for y in range(1,20):
120 self.field[x, y].type = 'empty'
121 self.field[x, y].snake = None
122 pass
123 for snake in self.snakes:
124 if snake == None:
125 pass
126 else:
127 snake.fill()
128 pass
129 return
130 def redraw(self):
131 self.canvas.delete(all)
132 for cell_coord in self.field:
133 self.field[cell_coord].redraw()
134 return
135 def legal_moves(self, snake):
136 snake.legal_dir = []
137 for direction in directions:
138 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
139 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
140 snake.legal_dir.append(direction)
141 return
142 return