diff options
| author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-11-01 21:46:26 +0200 |
|---|---|---|
| committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-11-01 23:42:16 +0200 |
| commit | 314be3895ece7dbeb47bcdd85a05acbc4bc0ff9c (patch) | |
| tree | 6fb7f6a8b8c6edc5301ef3e3dad0a2c4b4435200 /Game.py | |
| parent | 394959f11cdf5493673d60df4cb7a98683fc6afc (diff) | |
| download | SnakePuzzle-314be3895ece7dbeb47bcdd85a05acbc4bc0ff9c.tar.gz SnakePuzzle-314be3895ece7dbeb47bcdd85a05acbc4bc0ff9c.zip | |
feat: (a bit hacky) pressure plate, signal and door system
Diffstat (limited to 'Game.py')
| -rw-r--r-- | Game.py | 48 |
1 files changed, 44 insertions, 4 deletions
@@ -2,6 +2,9 @@ from Snake import Snake from Vec import Vec2 from Box import Box +from Door import Door +from PressurePlate import PressurePlate +from Trail import Trail class Walls: """Contains the walls of a game. @@ -45,39 +48,76 @@ class Game: snake = None boxes = [] walls = None + statics = [] def __init__(self): self.snake = Snake([Vec2(6, 6), Vec2(6, 7), Vec2(6,8), Vec2(7,8), Vec2(8, 8), Vec2(9, 8)], self) _box1 = Box(Vec2(11, 4)) _box2 = Box(Vec2(3, 10)) self.boxes = [_box1, _box2] + + self.statics = [Door(Vec2(0, 6)), Door(Vec2(6, 11)), PressurePlate(Vec2(6, 9))] + self.statics.append(Trail(Vec2(6,10), self.statics[1])) + self.statics[2].addTrail(self.statics[3]) + self.walls = Walls.fromString("""############### # # # f # # # # b # # # -# h # +D h # # t # # tttt # -# # +# _ # # b # -# ### # +######D###### # # f # ###############""") def width(self): return self.walls.width() def height(self): return self.walls.height() + def tick(self): self.snake.move() + for static in self.statics: + match static: + case PressurePlate(): + if static.isActive() and (not (static.pos in self.snake.cells)) and (not (static.pos in map(lambda box: box.pos, self.boxes))): + static.deactivate() + elif (not static.isActive()) and ((static.pos in self.snake.cells) or (static.pos in map(lambda box: box.pos, self.boxes))): + static.activate() + case Door(): + if static.isOpen() and not static.isActive(): + if not (static.pos in self.snake.cells or static.pos in map(lambda box: box.pos, self.boxes)): + static.close() + def isLost(self): return self.snake.hasCollided + + def closedDoorAt(self, pos: Vec2) -> bool: + res = False + for static in self.statics: + match static: + case Door(): + res = res or (static.pos == pos and not static.isOpen()) + return res + + def switchAt(self, pos: Vec2): + for static in self.statics: + match static: + case PressurePlate(): + if static.pos == pos: + return static + return None + + def enter(self, pos: Vec2, inDir: Vec2) -> bool: boxAt = next(filter(lambda box: box.pos == pos, self.boxes), None) - if self.walls.wallAt(pos): + if self.walls.wallAt(pos) or self.closedDoorAt(pos): return False elif pos in self.snake.cells: return False |
