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

snake

view engine.py @ 80:2f56e186e75c

Fixed initial value for snake.Rule.direction
author Danya Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 01:08:29 +0300
parents 198e91c5b94c
children 50056998a673
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 return
71 def step(self):
72 for i, snake in enumerate(self.snakes):
73 if len(snake.cells) == 0:
74 self.snakes[i] = None
75 if snake == None:
76 pass
77 else:
78 self.legal_moves(snake)
79 self.move_snake(snake)
80 self.refill()
81 self.redraw()
82 return
83 def move_snake(self, snake):
84 applied_dir = None
85 for rule in snake.rules:
86 if applied_dir != None:
87 choose_dir = []
88 for direction in snake.legal_dir:
89 rule.direction = direction
90 if rule.applies() == True:
91 choose_dir.append(direction)
92 pass
93 pass
94 if len(choose_move) != 0:
95 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
96 pass
97 else: