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

snake

annotate engine.py @ 146:fce3acd4affa

fixed snake start placement
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Mon, 20 Dec 2010 17:57:00 +0300
parents 8b5bc7bda225
children 382ab7b65331
rev   line source
martiran@12 1 import random as rnd
martiran@15 2 import Tkinter as tk
Alex@66 3 import snake
martiran@12 4
martiran@6 5 directions = [(0,1), (1,0), (0,-1), (-1,0)]
martiran@1 6
martiran@111 7 class Dict(dict):
martiran@142 8 """Create a dictionary."""
martiran@111 9 pass
martiran@111 10
martiran@5 11 class Cell(object):
martiran@142 12 """Cells.
martiran@142 13
martiran@142 14 Atributes:
martiran@142 15 - 'x' - absciss of the cell in field
martiran@142 16 - 'y' - ordinate of the cell in field
martiran@142 17 - 'canvas' - Widget the cell belongs to
martiran@142 18 - 'snake' - snake the cell belongs to, possible values:
martiran@142 19 snake
martiran@142 20 None
martiran@142 21 'my' \
martiran@142 22 'enemy' / for patterns only
martiran@142 23
martiran@142 24 - 'type' - type of the cell, possible values:
martiran@142 25 'empty'
martiran@142 26 'wall'
martiran@142 27 'body'
martiran@142 28 'head'
martiran@142 29 'tail'
martiran@142 30 'any' } for patterns only
martiran@142 31 'void' } for cells out of the field
martiran@142 32 """
martiran@15 33 def __init__(self, x, y, canvas = None):
martiran@142 34 """Initialyze the cell with default parameters:
martiran@142 35 type = 'empty'
martiran@142 36 snake = None"""
martiran@1 37 self.x = x
martiran@1 38 self.y = y
martiran@1 39 self.canvas = canvas
martiran@1 40 self.snake = None
martiran@28 41 self.type = 'empty'
martiran@1 42 return
martiran@142 43
martiran@113 44 def redraw(self, offset, c_size):
martiran@142 45 """Draw a cell based on it content"""
martiran@113 46 x0=offset[0] + self.x*c_size
martiran@113 47 y0=offset[1] + self.y*c_size
martiran@113 48 x1=offset[0] + (self.x+1)*c_size
martiran@113 49 y1=offset[1] + (self.y+1)*c_size
martiran@113 50 x2=offset[0] + (self.x+1/2.0)*c_size
martiran@15 51 if self.type == 'wall':
Alex@101 52 self.canvas.create_rectangle(x0, y0, x1, y1, fill="grey")
martiran@15 53 pass
martiran@15 54 elif self.type == 'empty':
Alex@101 55 self.canvas.create_rectangle(x0, y0, x1, y1, fill="black")
martiran@15 56 pass
martiran@15 57 elif self.type == 'body':
Alex@101 58 self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.snake.color)
martiran@15 59 pass
martiran@15 60 elif self.type == 'head':
Alex@101 61 self.canvas.create_oval(x0, y0, x1, y1, fill=self.snake.color)
martiran@15 62 pass
martiran@15 63 elif self.type == 'tail':
Alex@101 64 self.canvas.create_polygon(x0, y0, x1, y0, x2, y1, fill=self.snake.color)
martiran@15 65 pass
martiran@15 66 return
martiran@142 67
martiran@6 68 def __eq__(self, pattern):
martiran@142 69 """Check the equaliation of the cell to the pattern cell."""
martiran@52 70 if pattern.type == 'any':
martiran@6 71 return True
martiran@52 72 if pattern.type != self.type:
martiran@6 73 return False
martiran@52 74 if pattern.snake_type == 'my' and pattern.snake != self.snake:
martiran@52 75 return False
martiran@52 76 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
martiran@52 77 return False
martiran@52 78 return True
martiran@126 79
martiran@126 80 def __ne__(self, pattern):
martiran@142 81 """Check the discrepancy of the cell to the pattern cell."""
martiran@126 82 return not self == pattern
martiran@126 83
martiran@6 84 def clear(self):
martiran@142 85 """Change the cell parameters back to default."""
martiran@1 86 self.snake = None
martiran@1 87 self.type = 'empty'
martiran@1 88 return
martiran@1 89
martiran@2 90
martiran@5 91 class Engine(object):
martiran@142 92 """Engine
martiran@142 93
martiran@142 94 Atributes:
martiran@142 95
martiran@142 96 - 'field' - game field:
martiran@142 97 'field.w' - width of the field count in cells
martiran@142 98 'field.h' - hight of the field count in cells
martiran@142 99 - 'canvas' - Widget game field is showing on
martiran@142 100 - 'snakes' - list of snakes loaded
martiran@142 101 - 'start_snake_length' - starting length of the snake"""
martiran@142 102
martiran@6 103 def __init__(self, canvas):
martiran@142 104 """Initialyze the engine:
martiran@142 105 start_snake_length = 10"""
martiran@1 106 self.canvas = canvas
martiran@12 107 self.snakes = [None, None, None, None]
martiran@9 108 self.init_field()
martiran@111 109 self.start_snake_length = 10
martiran@9 110 return
martiran@142 111
martiran@9 112 def init_field (self):
martiran@142 113 """Initialyze the field:
martiran@142 114 width = 31
martiran@142 115 hieght = 31
martiran@142 116 perimeter is made by walls"""
martiran@111 117 self.field = Dict()
martiran@117 118 self.field.w = 31
martiran@117 119 self.field.h = 31
martiran@113 120 f_w = self.field.w
martiran@113 121 f_h = self.field.h
martiran@113 122 for x in range(f_w):
martiran@113 123 for y in range(f_h):
martiran@9 124 self.field[x, y] = Cell(x, y, self.canvas)
martiran@113 125 for y in range(f_h):
martiran@9 126 self.field[0, y].type = 'wall'
martiran@113 127 self.field[f_w-1, y].type = 'wall'
martiran@113 128 for x in range(1,f_w-1):
martiran@9 129 self.field[x, 0].type = 'wall'
martiran@113 130 self.field[x, f_h-1].type = 'wall'
Alex@103 131 self.refill()
Alex@93 132 self.redraw()
martiran@1 133 return
martiran@142 134
martiran@6 135 def step(self):
martiran@142 136 """Do the step of the game."""
Alex@62 137 for i, snake in enumerate(self.snakes):
Alex@103 138 if snake != None:
Alex@103 139 if len(snake.cells) == 0:
Alex@103 140 self.snakes[i] = None
Alex@103 141 continue
Alex@66 142 self.legal_moves(snake)
martiran@12 143 self.move_snake(snake)
martiran@12 144 self.refill()
martiran@9 145 self.redraw()
martiran@9 146 return
martiran@142 147
martiran@9 148 def move_snake(self, snake):
martiran@142 149 """Check of movement direction based on the snake rule list and actual
martiran@142 150 enviroment."""
martiran@105 151 head = snake.cells[0]
martiran@12 152 for rule in snake.rules:
martiran@105 153 for direction in snake.legal_dir:
martiran@105 154 rule.rotate(direction)
martiran@105 155 if rule.applies(self.field, head.x, head.y) == True:
martiran@105 156 self.move_do(snake, direction)
martiran@105 157 return
martiran@105 158 if snake.legal_dir != []:
Alex@99 159 self.move_do(snake, snake.legal_dir[0])
martiran@12 160 pass
martiran@14 161 return
martiran@142 162
Alex@94 163 def move_do(self, snake, applied_dir):
martiran@142 164 """Do the move of the snake."""
martiran@107 165 head = snake.cells[0]
martiran@107 166 dir_cell = self.field[head.x + applied_dir[0], head.y + applied_dir[1]]
Alex@90 167 if dir_cell.type == 'empty':
Alex@90 168 snake.cells.insert(0,dir_cell)
Alex@90 169 del snake.cells[-1]
Alex@90 170 pass
Alex@90 171 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
Alex@90 172 snake.cells.insert(0,dir_cell)
Alex@90 173 del dir_cell.snake.cells[-1]
Alex@90 174 pass
Alex@90 175
martiran@6 176 def create_snake(self, snake_number):
martiran@142 177 """Create the snake:
martiran@142 178 position choice is based on number or placement of 'Load' button
martiran@146 179 snakes are placed with tails turned to the wall.
martiran@142 180 color is chosen accorting to fen shui tradition"""
martiran@46 181 cells_id = []
martiran@113 182 f_h = self.field.h
martiran@113 183 f_w = self.field.w
martiran@111 184 for y in range(self.start_snake_length):
martiran@146 185 cells_id.insert(0,((f_w-1)/2, y+1))
martiran@46 186 for rot_num in range(snake_number - 1):
Alex@62 187 for i, cell in enumerate(cells_id):
martiran@122 188 cells_id[i] = (min(f_h, f_w)-1-cell[1],cell[0])
martiran@46 189 cells = []
martiran@46 190 for cell in cells_id:
Alex@60 191 cells.append(self.field[cell])
martiran@46 192 color_dic = {
martiran@46 193 1:'blue',
martiran@46 194 2:'green',
martiran@46 195 3:'yellow',
martiran@46 196 4:'red',}
martiran@46 197 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
Alex@77 198 return self.snakes[snake_number-1]
martiran@142 199
martiran@6 200 def refill(self):
martiran@142 201 """Refill the field cells types and snakes according to the actual
martiran@142 202 position"""
martiran@113 203 f_w = self.field.w
martiran@113 204 f_h = self.field.h
martiran@113 205 for x in range(1,f_w-1):
martiran@113 206 for y in range(1,f_h-1):
martiran@12 207 self.field[x, y].type = 'empty'
martiran@31 208 self.field[x, y].snake = None
martiran@12 209 pass
martiran@12 210 for snake in self.snakes:
martiran@12 211 if snake == None:
martiran@12 212 pass
martiran@12 213 else:
martiran@31 214 snake.fill()
martiran@12 215 pass
martiran@14 216 return
martiran@142 217
martiran@9 218 def redraw(self):
martiran@142 219 """Clear the field Widget and redraw cells images on it"""
martiran@117 220 self.canvas.delete("all")
martiran@113 221 w = self.canvas.winfo_width()
martiran@113 222 h = self.canvas.winfo_height()
martiran@113 223 cw = w/float(self.field.w)
martiran@113 224 ch = h/float(self.field.h)
martiran@113 225 c = min(cw, ch)
martiran@113 226 field_geometry = (self.field.w*c,self.field.h*c)
martiran@113 227 offset = ((w - field_geometry[0])/2.0, (h - field_geometry[1])/2.0)
martiran@32 228 for cell_coord in self.field:
martiran@113 229 self.field[cell_coord].redraw(offset, c)
martiran@15 230 return
martiran@142 231
martiran@6 232 def legal_moves(self, snake):
martiran@142 233 """Check for snake legal move directions according to the game rules."""
martiran@6 234 snake.legal_dir = []
martiran@108 235 head = snake.cells[0]
martiran@6 236 for direction in directions:
martiran@108 237 dir_cell = self.field[head.x + direction[0], head.y + direction[1]]
martiran@12 238 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
martiran@6 239 snake.legal_dir.append(direction)
Alex@90 240 rnd.shuffle(snake.legal_dir)
martiran@6 241 return
martiran@6 242