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

snake

annotate engine.py @ 56:2d48ad3500d2

fixed syntax error
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Sun, 19 Dec 2010 23:57:51 +0300
parents 0c02646962fa
children a3bb04e72924
rev   line source
martiran@12 1 import random as rnd
martiran@15 2 import Tkinter as tk
martiran@12 3
martiran@6 4 directions = [(0,1), (1,0), (0,-1), (-1,0)]
martiran@46 5 tm = [[0, -1], [1, 0]]
martiran@1 6
martiran@5 7 class Cell(object):
martiran@15 8 def __init__(self, x, y, canvas = None):
martiran@1 9 self.x = x
martiran@1 10 self.y = y
martiran@1 11 self.canvas = canvas
martiran@1 12 self.snake = None
martiran@28 13 self.type = 'empty'
martiran@1 14 return
martiran@6 15 def redraw(self):
martiran@54 16 field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width())
martiran@55 17 offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size)
martiran@15 18 if self.type == 'wall':
martiran@15 19 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")
martiran@15 20 pass
martiran@15 21 elif self.type == 'empty':
martiran@15 22 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")
martiran@15 23 pass
martiran@15 24 elif self.type == 'body':
martiran@15 25 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)
martiran@15 26 pass
martiran@15 27 elif self.type == 'head':
martiran@15 28 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)
martiran@15 29 pass
martiran@15 30 elif self.type == 'tail':
martiran@15 31 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)
martiran@15 32 pass
martiran@15 33 return
martiran@6 34 def __eq__(self, pattern):
martiran@52 35 if pattern.type == 'any':
martiran@6 36 return True
martiran@52 37 if pattern.type != self.type:
martiran@6 38 return False
martiran@52 39 if pattern.snake_type == 'my' and pattern.snake != self.snake:
martiran@52 40 return False
martiran@52 41 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake:
martiran@52 42 return False
martiran@52 43 return True
martiran@6 44 def clear(self):
martiran@1 45 self.snake = None
martiran@1 46 self.type = 'empty'
martiran@1 47 return
martiran@1 48
martiran@2 49
martiran@5 50 class Engine(object):
martiran@6 51 def __init__(self, canvas):
martiran@1 52 self.canvas = canvas
martiran@54 53 self.w = min(canvas.winfo_height(), canvas.winfo_width())
martiran@54 54 self.h = min(canvas.winfo_height(), canvas.winfo_width())
martiran@12 55 self.snakes = [None, None, None, None]
martiran@9 56 self.init_field()
martiran@9 57 return
martiran@9 58 def init_field (self):
martiran@9 59 self.field = {}
martiran@9 60 for x in range(21):
martiran@9 61 for y in range(21):
martiran@9 62 self.field[x, y] = Cell(x, y, self.canvas)
martiran@9 63 for y in range(21):
martiran@9 64 self.field[0, y].type = 'wall'
martiran@9 65 self.field[20, y].type = 'wall'
martiran@9 66 for x in range(1,20):
martiran@9 67 self.field[x, 0].type = 'wall'
martiran@9 68 self.field[x, 20].type = 'wall'
martiran@1 69 return
martiran@6 70 def step(self):
martiran@9 71 for snake in self.snakes:
martiran@12 72 if snake == None:
martiran@12 73 pass
martiran@12 74 else:
martiran@12 75 self.legal_moves()
martiran@12 76 self.move_snake(snake)
martiran@12 77 self.refill()
martiran@9 78 self.redraw()
martiran@9 79 return
martiran@9 80 def move_snake(self, snake):
martiran@12 81 applied_dir = None
martiran@12 82 for rule in snake.rules:
martiran@12 83 if applied_dir != None:
martiran@12 84 choose_dir = []
martiran@12 85 for direction in snake.legal_dir:
martiran@12 86 rule.direction = direction
martiran@12 87 if rule.applies() == True:
martiran@12 88 choose_dir.append(direction)
martiran@12 89 pass
martiran@12 90 pass
martiran@12 91 if len(choose_move) != 0:
martiran@12 92 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
martiran@12 93 pass
martiran@12 94 else:
martiran@17 95 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
martiran@17 96 if dir_cell.type == 'empty':
martiran@17 97 snake.cells.insert(0,dir_cell)
martiran@17 98 del snake.cells[-1]
martiran@12 99 pass
martiran@12 100 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@17 101 snake.cells.insert(0,dir_cell)
martiran@17 102 del dir_cell.snake.cells[-1]
martiran@12 103 pass
martiran@12 104 break
martiran@12 105 if applied_dir == None:
martiran@12 106 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]
martiran@17 107 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
martiran@17 108 if dir_cell.type == 'empty':
martiran@17 109 snake.cells.insert(0,dir_cell)
martiran@17 110 del snake.cells[-1]
martiran@12 111 pass
martiran@12 112 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@17 113 snake.cells.insert(0,dir_cell)
martiran@17 114 del dir_cell.snake.cells[-1]
martiran@12 115 pass
martiran@12 116 pass
martiran@14 117 return
martiran@6 118 def create_snake(self, snake_number):
martiran@46 119 cells_id = []
martiran@46 120 for y in range(10):
martiran@56 121 cells_id.append((10, y+1))
martiran@46 122 for rot_num in range(snake_number - 1):
martiran@46 123 for sell in cells_id:
martiran@46 124 cells_id[cells_id.index(sell)] = (tm[0][0]*sell[0] + tm[0][1]*sell[1],tm[1][0]*sell[0] + tm[1][1]*sell[1])
martiran@46 125 cells = []
martiran@46 126 for cell in cells_id:
martiran@46 127 cells.append(self.field[sell])
martiran@46 128 color_dic = {
martiran@46 129 1:'blue',
martiran@46 130 2:'green',
martiran@46 131 3:'yellow',
martiran@46 132 4:'red',}
martiran@46 133 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number])
martiran@1 134 pass
martiran@6 135 def refill(self):
martiran@12 136 for x in range(1,20):
martiran@12 137 for y in range(1,20):
martiran@12 138 self.field[x, y].type = 'empty'
martiran@31 139 self.field[x, y].snake = None
martiran@12 140 pass
martiran@12 141 for snake in self.snakes:
martiran@12 142 if snake == None:
martiran@12 143 pass
martiran@12 144 else:
martiran@31 145 snake.fill()
martiran@12 146 pass
martiran@14 147 return
martiran@9 148 def redraw(self):
martiran@16 149 self.canvas.delete(all)
martiran@32 150 for cell_coord in self.field:
martiran@32 151 self.field[cell_coord].redraw()
martiran@15 152 return
martiran@6 153 def legal_moves(self, snake):
martiran@6 154 snake.legal_dir = []
martiran@6 155 for direction in directions:
martiran@17 156 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
martiran@12 157 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
martiran@6 158 snake.legal_dir.append(direction)
martiran@6 159 return
martiran@6 160 return
martiran@6 161