Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/rev/8b5bc7bda225
Дата изменения: Unknown
Дата индексирования: Mon Oct 1 23:39:51 2012
Кодировка:
snake: 8b5bc7bda225

snake

changeset 142:8b5bc7bda225

engine documentation
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Mon, 20 Dec 2010 17:53:36 +0300
parents 416418c9aaef
children bbc928643613
files engine.py
diffstat 1 files changed, 66 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/engine.py	Mon Dec 20 17:10:22 2010 +0300
     1.2 +++ b/engine.py	Mon Dec 20 17:53:36 2010 +0300
     1.3 @@ -5,17 +5,44 @@
     1.4  directions = [(0,1), (1,0), (0,-1), (-1,0)]
     1.5  
     1.6  class Dict(dict):
     1.7 +    """Create a dictionary."""
     1.8      pass
     1.9  
    1.10  class Cell(object):
    1.11 +    """Cells.
    1.12 +
    1.13 +    Atributes:
    1.14 +    - 'x' - absciss of the cell in field
    1.15 +    - 'y' - ordinate of the cell in field
    1.16 +    - 'canvas' - Widget the cell belongs to
    1.17 +    - 'snake' - snake the cell belongs to, possible values:
    1.18 +        snake
    1.19 +        None
    1.20 +        'my'        \
    1.21 +        'enemy'     /  for patterns only
    1.22 +
    1.23 +    - 'type' - type of the cell, possible values:
    1.24 +        'empty'
    1.25 +        'wall'
    1.26 +        'body'
    1.27 +        'head'
    1.28 +        'tail'
    1.29 +        'any'  } for patterns only
    1.30 +        'void' } for cells out of the field
    1.31 +    """
    1.32      def __init__(self, x, y, canvas = None):
    1.33 +        """Initialyze the cell with default parameters:
    1.34 +            type = 'empty'
    1.35 +            snake = None"""
    1.36          self.x = x
    1.37          self.y = y
    1.38          self.canvas = canvas
    1.39          self.snake = None
    1.40          self.type = 'empty'
    1.41          return
    1.42 +    
    1.43      def redraw(self, offset, c_size):
    1.44 +        """Draw a cell based on it content"""
    1.45          x0=offset[0] + self.x*c_size
    1.46          y0=offset[1] + self.y*c_size
    1.47          x1=offset[0] + (self.x+1)*c_size
    1.48 @@ -37,7 +64,9 @@
    1.49              self.canvas.create_polygon(x0, y0, x1, y0, x2, y1, fill=self.snake.color)
    1.50              pass
    1.51          return
    1.52 +    
    1.53      def __eq__(self, pattern):
    1.54 +        """Check the equaliation of the cell to the pattern cell."""
    1.55          if pattern.type == 'any':
    1.56              return True
    1.57          if pattern.type != self.type:
    1.58 @@ -49,24 +78,42 @@
    1.59          return True
    1.60      
    1.61      def __ne__(self, pattern):
    1.62 +        """Check the discrepancy of the cell to the pattern cell."""
    1.63          return not self == pattern
    1.64      
    1.65      def clear(self):
    1.66 +        """Change the cell parameters back to default."""
    1.67          self.snake = None
    1.68          self.type = 'empty'
    1.69          return
    1.70  
    1.71  
    1.72  class Engine(object):
    1.73 +    """Engine
    1.74 +
    1.75 +    Atributes:
    1.76 +
    1.77 +    - 'field' - game field:
    1.78 +        'field.w' - width of the field count in cells
    1.79 +        'field.h' - hight of the field count in cells
    1.80 +    - 'canvas' - Widget game field is showing on
    1.81 +    - 'snakes' - list of snakes loaded
    1.82 +    - 'start_snake_length' - starting length of the snake"""
    1.83 +    
    1.84      def __init__(self, canvas):
    1.85 +        """Initialyze the engine:
    1.86 +        start_snake_length = 10"""
    1.87          self.canvas = canvas
    1.88 -        self.w = min(canvas.winfo_height(), canvas.winfo_width())
    1.89 -        self.h = min(canvas.winfo_height(), canvas.winfo_width())
    1.90          self.snakes = [None, None, None, None]
    1.91          self.init_field()
    1.92          self.start_snake_length = 10
    1.93          return
    1.94 +    
    1.95      def init_field (self):
    1.96 +        """Initialyze the field:
    1.97 +        width = 31
    1.98 +        hieght = 31
    1.99 +        perimeter is made by walls"""
   1.100          self.field = Dict()
   1.101          self.field.w = 31
   1.102          self.field.h = 31
   1.103 @@ -84,7 +131,9 @@
   1.104          self.refill()
   1.105          self.redraw()
   1.106          return
   1.107 +    
   1.108      def step(self):
   1.109 +        """Do the step of the game."""
   1.110          for i, snake in enumerate(self.snakes):
   1.111              if snake != None:
   1.112                  if len(snake.cells) == 0:
   1.113 @@ -95,7 +144,10 @@
   1.114                  self.refill()
   1.115          self.redraw()    
   1.116          return
   1.117 +    
   1.118      def move_snake(self, snake):
   1.119 +        """Check of movement direction based on the snake rule list and actual
   1.120 +        enviroment."""
   1.121          head = snake.cells[0]
   1.122          for rule in snake.rules:
   1.123              for direction in snake.legal_dir:
   1.124 @@ -107,7 +159,9 @@
   1.125              self.move_do(snake, snake.legal_dir[0])
   1.126              pass
   1.127          return
   1.128 +    
   1.129      def move_do(self, snake, applied_dir):
   1.130 +        """Do the move of the snake."""
   1.131          head = snake.cells[0]
   1.132          dir_cell = self.field[head.x + applied_dir[0], head.y + applied_dir[1]]
   1.133          if dir_cell.type == 'empty':
   1.134 @@ -120,6 +174,9 @@
   1.135              pass
   1.136  
   1.137      def create_snake(self, snake_number):
   1.138 +        """Create the snake:
   1.139 +        position choice is based on number or placement of 'Load' button
   1.140 +        color is chosen accorting to fen shui tradition"""
   1.141          cells_id = []
   1.142          f_h = self.field.h
   1.143          f_w = self.field.w
   1.144 @@ -138,7 +195,10 @@
   1.145              4:'red',}
   1.146          self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
   1.147          return self.snakes[snake_number-1]
   1.148 +    
   1.149      def refill(self):
   1.150 +        """Refill the field cells types and snakes according to the actual
   1.151 +        position"""
   1.152          f_w = self.field.w
   1.153          f_h = self.field.h
   1.154          for x in range(1,f_w-1):
   1.155 @@ -153,7 +213,9 @@
   1.156                  snake.fill()
   1.157                  pass
   1.158          return
   1.159 +    
   1.160      def redraw(self):
   1.161 +        """Clear the field Widget and redraw cells images on it"""
   1.162          self.canvas.delete("all")
   1.163          w = self.canvas.winfo_width()
   1.164          h = self.canvas.winfo_height()
   1.165 @@ -165,7 +227,9 @@
   1.166          for cell_coord in self.field:
   1.167              self.field[cell_coord].redraw(offset, c)
   1.168          return
   1.169 +    
   1.170      def legal_moves(self, snake):
   1.171 +        """Check for snake legal move directions according to the game rules."""
   1.172          snake.legal_dir = []
   1.173          head = snake.cells[0]
   1.174          for direction in directions: