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

snake

annotate engine.py @ 32:b2eaeeb74d87

fixed lots of small errors
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Sun, 19 Dec 2010 19:22:49 +0300
parents 76d0514d1ef9
children 85221e4417b7
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@9 5 rtm = [[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@32 16 field_size = min(self.canvas.height, self.canvas.width)
martiran@32 17 offset = (self.canvas.width - field_size, self.canvas.hieght - 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@6 35 if pattern.type == self.type:
martiran@6 36 return True
martiran@6 37 else:
martiran@6 38 return False
martiran@6 39 return
martiran@6 40 def clear(self):
martiran@1 41 self.snake = None
martiran@1 42 self.type = 'empty'
martiran@1 43 return
martiran@1 44
martiran@2 45
martiran@5 46 class Engine(object):
martiran@6 47 def __init__(self, canvas):
martiran@1 48 self.canvas = canvas
martiran@28 49 #self.w = min(canvas.height, canvas.width)
martiran@28 50 #self.h = min(canvas.height, canvas.width)
martiran@12 51 self.snakes = [None, None, None, None]
martiran@9 52 self.init_field()
martiran@9 53 return
martiran@9 54 def init_field (self):
martiran@9 55 self.field = {}
martiran@9 56 for x in range(21):
martiran@9 57 for y in range(21):
martiran@9 58 self.field[x, y] = Cell(x, y, self.canvas)
martiran@9 59 for y in range(21):
martiran@9 60 self.field[0, y].type = 'wall'
martiran@9 61 self.field[20, y].type = 'wall'
martiran@9 62 for x in range(1,20):
martiran@9 63 self.field[x, 0].type = 'wall'
martiran@9 64 self.field[x, 20].type = 'wall'
martiran@1 65 return
martiran@6 66 def step(self):
martiran@9 67 for snake in self.snakes:
martiran@12 68 if snake == None:
martiran@12 69 pass
martiran@12 70 else:
martiran@12 71 self.legal_moves()
martiran@12 72 self.move_snake(snake)
martiran@12 73 self.refill()
martiran@9 74 self.redraw()
martiran@9 75 return
martiran@9 76 def move_snake(self, snake):
martiran@12 77 applied_dir = None
martiran@12 78 for rule in snake.rules:
martiran@12 79 if applied_dir != None:
martiran@12 80 choose_dir = []
martiran@12 81 for direction in snake.legal_dir:
martiran@12 82 rule.direction = direction
martiran@12 83 if rule.applies() == True:
martiran@12 84 choose_dir.append(direction)
martiran@12 85 pass
martiran@12 86 pass
martiran@12 87 if len(choose_move) != 0:
martiran@12 88 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]
martiran@12 89 pass
martiran@12 90 else:
martiran@17 91 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
martiran@17 92 if dir_cell.type == 'empty':
martiran@17 93 snake.cells.insert(0,dir_cell)
martiran@17 94 del snake.cells[-1]
martiran@12 95 pass
martiran@12 96 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@17 97 snake.cells.insert(0,dir_cell)
martiran@17 98 del dir_cell.snake.cells[-1]
martiran@12 99 pass
martiran@12 100 break
martiran@12 101 if applied_dir == None:
martiran@12 102 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]
martiran@17 103 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]]
martiran@17 104 if dir_cell.type == 'empty':
martiran@17 105 snake.cells.insert(0,dir_cell)
martiran@17 106 del snake.cells[-1]
martiran@12 107 pass
martiran@12 108 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):
martiran@17 109 snake.cells.insert(0,dir_cell)
martiran@17 110 del dir_cell.snake.cells[-1]
martiran@12 111 pass
martiran@12 112 pass
martiran@14 113 return
martiran@6 114 def create_snake(self, snake_number):
martiran@32 115
martiran@1 116 pass
martiran@6 117 def refill(self):
martiran@12 118 for x in range(1,20):
martiran@12 119 for y in range(1,20):
martiran@12 120 self.field[x, y].type = 'empty'
martiran@31 121 self.field[x, y].snake = None
martiran@12 122 pass
martiran@12 123 for snake in self.snakes:
martiran@12 124 if snake == None:
martiran@12 125 pass
martiran@12 126 else:
martiran@31 127 snake.fill()
martiran@12 128 pass
martiran@14 129 return
martiran@9 130 def redraw(self):
martiran@16 131 self.canvas.delete(all)
martiran@32 132 for cell_coord in self.field:
martiran@32 133 self.field[cell_coord].redraw()
martiran@15 134 return
martiran@6 135 def legal_moves(self, snake):
martiran@6 136 snake.legal_dir = []
martiran@6 137 for direction in directions:
martiran@17 138 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]
martiran@12 139 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):
martiran@6 140 snake.legal_dir.append(direction)
martiran@6 141 return
martiran@6 142 return
martiran@6 143