Ejemplo n.º 1
0
class Sokosnake(Game):
	element_hash = {'_': Passage, 'W': Wall,   'S': Head,    'A': Apple,
	                'R': Room,    'K': Rock,   'D': Diamond, 'T': Teleport,
	                't': Teleend, 'H': Hole,   'G': Gate}

	def __init__(self, level_file):
		self.parser = Parser(self.element_hash)
		size_x, size_y = self.parser.load(open(level_file).read())
		Game.__init__(self, size_x, size_y)

	def load(self):
		for element in self.parser.parse():
			self.add(element)
		self.snake = self.find_element(lambda e: type(e) == Head)
		self.points = 0
		self._diamonds = 0
		self.diamonds_all = len(self.find_elements(lambda e: type(e) == Diamond))
		self.post_initialize()

	def get_diamonds(self):
		return self._diamonds

	def set_diamonds(self, val):
		self._diamonds = val
		if self._diamonds == self.diamonds_all:
			raise GameOver("You won!")

	diamonds = property(get_diamonds, set_diamonds)

	def step(self, dir):
		target_field = (self.snake.x + dir[0], self.snake.y + dir[1])
		Action(self.snake, 'move', {'field': target_field}).run()
Ejemplo n.º 2
0
	def __init__(self, level_file):
		self.parser = Parser(self.element_hash)
		size_x, size_y = self.parser.load(open(level_file).read())
		Game.__init__(self, size_x, size_y)