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

snake

annotate engine.py @ 119:0255870e79ce

snakes added
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Mon, 20 Dec 2010 14:45:04 +0300
parents 793011c1119e
children b7d2bfd5860d
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@111 8 pass
martiran@111 9
martiran@5 10 class Cell(object):
martiran@15 11 def __init__(self, x, y, canvas = None):
martiran@1 12 self.x = x
martiran@1 13 self.y = y
martiran@1 14 self.canvas = canvas
martiran@1 15 self.snake = None
martiran@28 16 self.type = 'empty'
martiran@1 17 return
martiran@113 18 def redraw(self, offset, c_size):
martiran@113 19
martiran@113 20 x0=offset[0] + self.x*c_size
martiran@113 21 y0=offset[1] + self.y*c_size
martiran@113 22 x1=offset[0] + (self.x+1)*c_size
martiran@113 23 y1=offset[1] + (self.y+1)*c_size
martiran@113 24 x2=offset[0] + (self.x+1/2.0)*c_size
martiran@15 25 if self.type == 'wall':
Alex@101 26 self.canvas.create_rectangle(x0, y0, x1, y1, fill="grey")
martiran@15 27 pass
martiran@15 28 elif self.type == 'empty':
Alex@101 29 self.canvas.create_rectangle(x0, y0, x1, y1, fill="black")
martiran@15 30 pass
martiran@15 31 elif self.type == 'body':
Alex@101 32 self.canvas.create_rectangle(x0, y0, x1, y1, fill=self.snake.color)
martiran@15 33 pass
martiran@15 34 elif self.type == 'head':
Alex@101 35 self.canvas.create_oval(x0, y0, x1, y1, fill=self.snake.color)
martiran@15 36 pass
martiran@15 37 elif self.type == 'tail':
Alex@101 38 self.canvas.create_polygon(x0, y0, x1, y0, x2, y1, fill=self.snake.color)
martiran@15 39 pass
martiran@15 40 return
martiran@6 41 def __eq__(self, pattern):
martiran@52 42 if pattern.type == 'any':
martiran@6 43 return True
martiran@52 44 if pattern.type != self.type:
martiran@6 45 return False
martiran@52 46 if pattern.snake_type == 'my' and pattern.snake != self.snake:
martiran@52 47 return False
martiran@52 48 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
martiran@52 49 return False
martiran@52 50 return True
martiran@6 51 def clear(self):
martiran@1 52 self.snake = None
martiran@1 53 self.type = 'empty'
martiran@1 54 return
martiran@1 55
martiran@2 56
martiran@5 57 class Engine(object):
martiran@6 58 def __init__(self, canvas):
martiran@1 59 self.canvas = canvas
martiran@54 60 self.w = min(canvas.winfo_height(), canvas.winfo_width())
martiran@54 61 self.h = min(canvas.winfo_height(), canvas.winfo_width())
martiran@12 62 self.snakes = [None, None, None, None]
martiran@9 63 self.init_field()
martiran@111 64 self.start_snake_length = 10
martiran@9 65 return
martiran@9 66 def init_field (self):
martiran@111 67 self.field = Dict()
martiran@117 68 self.field.w = 31
martiran@117 69 self.field.h = 31
martiran@113 70 f_w = self.field.w
martiran@113 71 f_h = self.field.h
martiran@113 72 for x in range(f_w):
martiran@113 73 for y in range(f_h):
martiran@9 74 self.field[x, y] = Cell(x, y, self.canvas)
martiran@113 75 for y in range(f_h):
martiran@9 76 self.field[0, y].type = 'wall'
martiran@113 77 self.field[f_w-1, y].type = 'wall'
martiran@113 78 for x in range(1,f_w-1):
martiran@9 79 self.field[x, 0].type = 'wall'
martiran@113 80 self.field[x, f_h-1].type = 'wall'
Alex@103 81 self.refill()
Alex@93 82 self.redraw()
martiran@1 83 return
martiran@6 84 def step(self):
Alex@62 85 for i, snake in enumerate(self.snakes):
Alex@103 86 if snake != None:
Alex@103 87 if len(snake.cells) == 0:
Alex@103 88 self.snakes[i] = None
Alex@103 89 continue
Alex@66 90 self.legal_moves(snake)
martiran@12 91 self.move_snake(snake)
martiran@12 92 self.refill()
martiran@9 93 self.redraw()
martiran@9 94 return
martiran@9 95 def move_snake(self, snake):
martiran@105 96 head = snake.cells[0]
martiran@12 97 for rule in snake.rules:
martiran@105 98 for direction in snake.legal_dir:
martiran@105 99 rule.rotate(direction)
martiran@105 100 if rule.applies(self.field, head.x, head.y) == True:
martiran@105 101 self.move_do(snake, direction)
martiran@105 102 return
martiran@105 103 if snake.legal_dir != []:
Alex@99 104 self.move_do(snake, snake.legal_dir[0])
martiran@12 105 pass
martiran@14 106 return
Alex@94 107 def move_do(self, snake, applied_dir):
martiran@107 108 head = snake.cells[0]
martiran@107 109 dir_cell = self.field[head.x + applied_dir[0], head.y + applied_dir[1]]
Alex@90 110 if dir_cell.type == 'empty':
Alex@90 111 snake.cells.insert(0,dir_cell)
Alex@90 112 del snake.cells[-1]
Alex@90 113 pass
Alex@90 114 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
Alex@90 115 snake.cells.insert(0,dir_cell)
Alex@90 116 del dir_cell.snake.cells[-1]
Alex@90 117 pass
Alex@90 118
martiran@6 119 def create_snake(self, snake_number):
martiran@46 120 cells_id = []
martiran@113 121 f_h = self.field.h
martiran@113 122 f_w = self.field.w
martiran@111 123 for y in range(self.start_snake_length):
martiran@114 124 cells_id.append(((f_w-1)/2, y+1))
martiran@46 125 for rot_num in range(snake_number - 1):
Alex@62 126 for i, cell in enumerate(cells_id):
martiran@113 127 cells_id[i] = (cell[1],min(f_h, f_w)-cell[0])
martiran@46 128 cells = []
martiran@46 129 for cell in cells_id:
Alex@60 130 cells.append(self.field[cell])
martiran@46 131 color_dic = {
martiran@46 132 1:'blue',
martiran@46 133 2:'green',
martiran@46 134 3:'yellow',
martiran@46 135 4:'red',}
martiran@46 136 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
Alex@77 137 return self.snakes[snake_number-1]
martiran@6 138 def refill(self):
martiran@113 139 f_w = self.field.w
martiran@113 140 f_h = self.field.h
martiran@113 141 for x in range(1,f_w-1):
martiran@113 142 for y in range(1,f_h-1):
martiran@12 143 self.field[x, y].type = 'empty'
martiran@31 144 self.field[x, y].snake = None
martiran@12 145 pass
martiran@12 146 for snake in self.snakes:
martiran@12 147 if snake == None:
martiran@12 148 pass
martiran@12 149 else:
martiran@31 150 snake.fill()
martiran@12 151 pass
martiran@14 152 return
martiran@9 153 def redraw(self):
martiran@117 154 self.canvas.delete("all")
martiran@113 155 w = self.canvas.winfo_width()
martiran@113 156 h = self.canvas.winfo_height()
martiran@113 157 cw = w/float(self.field.w)
martiran@113 158 ch = h/float(self.field.h)
martiran@113 159 c = min(cw, ch)
martiran@113 160 field_geometry = (self.field.w*c,self.field.h*c)
martiran@113 161 offset = ((w - field_geometry[0])/2.0, (h - field_geometry[1])/2.0)
martiran@32 162 for cell_coord in self.field:
martiran@113 163 self.field[cell_coord].redraw(offset, c)
martiran@15 164 return
martiran@6 165 def legal_moves(self, snake):
martiran@6 166 snake.legal_dir = []
martiran@108 167 head = snake.cells[0]
martiran@6 168 for direction in directions:
martiran@108 169 dir_cell = self.field[head.x + direction[0], head.y + direction[1]]
martiran@12 170 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
martiran@6 171 snake.legal_dir.append(direction)
Alex@90 172 rnd.shuffle(snake.legal_dir)
martiran@6 173 return
martiran@6 174