diff options
| author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-11-01 16:32:45 +0200 |
|---|---|---|
| committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-11-01 16:32:45 +0200 |
| commit | ae319acbf4c29551be07c1406c434e129a84b51e (patch) | |
| tree | 5533cf9db4c73a7619c6107dca0842c4bc8f9319 /Game.py | |
| parent | 57f20a5ef761985b34817846d471a064b180e089 (diff) | |
| download | SnakePuzzle-ae319acbf4c29551be07c1406c434e129a84b51e.tar.gz SnakePuzzle-ae319acbf4c29551be07c1406c434e129a84b51e.zip | |
feat: boxes, pushing, failing before entering 'solid' tiles
Diffstat (limited to 'Game.py')
| -rw-r--r-- | Game.py | 42 |
1 files changed, 32 insertions, 10 deletions
@@ -1,6 +1,7 @@ from Snake import Snake from Vec import Vec2 +from Box import Box class Walls: """Contains the walls of a game. @@ -40,21 +41,29 @@ class Walls: class Game: """Class responsible for the main logic of a game. For a game, this will probably be a singleton.""" + + snake = None + boxes = [] + walls = None - snake = Snake([Vec2(6, 6), Vec2(6, 7), Vec2(6,8), Vec2(7,8)]) - walls = Walls.fromString("""############### -# # -# # -# # -# # -# # -# # + def __init__(self): + self.snake = Snake([Vec2(6, 6), Vec2(6, 7), Vec2(6,8), Vec2(7,8)], self) + _box1 = Box(Vec2(11, 4)) + _box2 = Box(Vec2(3, 10)) + self.boxes = [_box1, _box2] + self.walls = Walls.fromString("""############### # # +# f # # # +# b # # # +# h # +# t # +# tt # # # +# b # # ### # -# # +# f # ###############""") def width(self): return self.walls.width() @@ -64,5 +73,18 @@ class Game: self.snake.move() def isLost(self): - return self.snake.headInTail() or self.walls.wallAt(self.snake.head()) + return self.snake.hasCollided + + def enter(self, pos: Vec2, inDir: Vec2) -> bool: + boxAt = next(filter(lambda box: box.pos == pos, self.boxes), None) + if self.walls.wallAt(pos): + return False + elif boxAt != None: + if self.enter(pos + inDir, inDir): + boxAt.pos = pos + inDir + return True + else: + return False + else: + return True |
