Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/f25c0439251f/engine.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 23:00:58 2014
Кодировка:
snake: engine.py annotate

snake

annotate engine.py @ 10:f25c0439251f

Automated merge with ssh://kodomo.fbb.msu.ru/snake
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Sun, 12 Dec 2010 21:02:27 +0300
parents 1b6cfae2315b 2c9ca13b4413
children 0ece930a4d1b a1a766440639
rev   line source
martiran@6 1 directions = [(0,1), (1,0), (0,-1), (-1,0)]
martiran@9 2 rtm = [[0, -1], [1, 0]]
martiran@1 3
martiran@5 4 class Cell(object):
martiran@5 5 def __init__(self, x, y, canvas):
martiran@1 6 self.x = x
martiran@1 7 self.y = y
martiran@1 8 self.canvas = canvas
martiran@1 9 self.snake = None
martiran@1 10 self.type = empty
martiran@1 11 return
martiran@6 12 def redraw(self):
martiran@1 13 pass
martiran@6 14 def __eq__(self, pattern):
martiran@6 15 if pattern.type == self.type:
martiran@6 16 return True
martiran@6 17 else:
martiran@6 18 return False
martiran@6 19 return
martiran@6 20 def clear(self):
martiran@1 21 self.snake = None
martiran@1 22 self.type = 'empty'
martiran@1 23 return
martiran@1 24
martiran@2 25
martiran@5 26 class Engine(object):
martiran@6 27 def __init__(self, canvas):
martiran@1 28 self.canvas = canvas
martiran@6 29 self.w = min(canvas.height, canvas.width)
martiran@6 30 self.h = min(canvas.height, canvas.width)
martiran@1 31 self.snakes = []
martiran@9 32 self.init_field()
martiran@9 33 return
martiran@9 34 def init_field (self):
martiran@9 35 self.field = {}
martiran@9 36 for x in range(21):
martiran@9 37 for y in range(21):
martiran@9 38 self.field[x, y] = Cell(x, y, self.canvas)
martiran@9 39 pass
martiran@9 40 pass
martiran@9 41 for y in range(21):
martiran@9 42 self.field[0, y].type = 'wall'
martiran@9 43 self.field[20, y].type = 'wall'
martiran@9 44 pass
martiran@9 45 for x in range(1,20):
martiran@9 46 self.field[x, 0].type = 'wall'
martiran@9 47 self.field[x, 20].type = 'wall'
martiran@9 48 pass
martiran@1 49 return
martiran@6 50 def step(self):
martiran@9 51 for snake in self.snakes:
martiran@9 52 self.legal_moves()
martiran@9 53 self.move_snake(snake)
martiran@9 54 self.refill()
martiran@9 55 self.redraw()
martiran@9 56 return
martiran@9 57 def move_snake(self, snake):
martiran@1 58 pass
martiran@6 59 def create_snake(self, snake_number):
martiran@1 60 pass
martiran@6 61 def refill(self):
martiran@1 62 pass
martiran@9 63 def redraw(self):
martiran@1 64 pass
martiran@6 65 def legal_moves(self, snake):
martiran@6 66 snake.legal_dir = []
martiran@6 67 for direction in directions:
me@8 68 dir_cell = field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
martiran@6 69 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake == 'enemy')):
martiran@6 70 snake.legal_dir.append(direction)
martiran@6 71 return
martiran@6 72 return
martiran@6 73