-
Notifications
You must be signed in to change notification settings - Fork 0
/
sokosnake.py
49 lines (39 loc) · 1.29 KB
/
sokosnake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sokolib.game import Game
from sokolib.parser import Parser
from sokolib.action import *
from elements import *
Right = (1,0)
Down = (0,1)
Left = (-1,0)
Up = (0,-1)
class GameOver:
def __init__(self, val):
self.val = val
def __str__(self):
return self.val
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()