diff options
| -rw-r--r-- | Door.py | 31 | ||||
| -rw-r--r-- | Game.py | 48 | ||||
| -rw-r--r-- | GameView.py | 49 | ||||
| -rw-r--r-- | PressurePlate.py | 34 | ||||
| -rw-r--r-- | Trail.py | 29 | ||||
| -rw-r--r-- | Walls.py | 38 |
6 files changed, 206 insertions, 23 deletions
@@ -0,0 +1,31 @@ + +class Door: + + pos = None + + _open = False + + _active = False + + + def __init__(self, pos, isOpen = False, isActive = False): + self.pos = pos + self._open = isOpen + self._active = False + + + def isOpen(self): return self._open + + def isActive(self): return self._active + + + def activate(self): + self._open = True + self._active = True + + def deactivate(self): + self._active = False + + def close(self): + self._open = False + @@ -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 diff --git a/GameView.py b/GameView.py index 50785a7..3b458d8 100644 --- a/GameView.py +++ b/GameView.py @@ -1,6 +1,10 @@ from Game import Game from Box import Box +from Door import Door +from PressurePlate import PressurePlate +from Trail import Trail + import pygame class GameView: @@ -31,29 +35,36 @@ class GameView: surface.fill("black") for cell in self.game.walls.walls(): - pygame.draw.rect(surface, "white", pygame.Rect( - cell.x*self.cellWidth, - cell.y*self.cellWidth, - self.cellWidth, - self.cellWidth - )) + self._drawBoxOfColor(surface, "white", cell.x, cell.y) + + for static in self.game.statics: + match static: + case Door(): + if static.isOpen(): + self._drawBoxOfColor(surface, "lightgray", static.pos.x, static.pos.y) + else: + self._drawBoxOfColor(surface, "gray", static.pos.x, static.pos.y) + case PressurePlate(): + self._drawBoxOfColor(surface, "yellow", static.pos.x, static.pos.y) + case Trail(): + if static.isOn(): + self._drawBoxOfColor(surface, "pink", static.pos.x, static.pos.y) + else: + self._drawBoxOfColor(surface, "purple", static.pos.x, static.pos.y) for box in self.game.boxes: - pygame.draw.rect(surface, "brown", pygame.Rect( - box.pos.x*self.cellWidth, - box.pos.y*self.cellWidth, - self.cellWidth, - self.cellWidth - )) + self._drawBoxOfColor(surface, "brown", box.pos.x, box.pos.y) for cell in self.game.snake.cells: - pygame.draw.rect(surface, "red", pygame.Rect( - cell.x*self.cellWidth, - cell.y*self.cellWidth, - self.cellWidth, - self.cellWidth - )) - + self._drawBoxOfColor(surface, "red", cell.x, cell.y) + + def _drawBoxOfColor(self, surface, color, x, y): + pygame.draw.rect(surface, color, pygame.Rect( + x*self.cellWidth, + y*self.cellWidth, + self.cellWidth, + self.cellWidth + )) def update(self, time): diff --git a/PressurePlate.py b/PressurePlate.py new file mode 100644 index 0000000..0bcb7c3 --- /dev/null +++ b/PressurePlate.py @@ -0,0 +1,34 @@ + + +class PressurePlate: + + pos = None + + _trails = [] + + _isActive = False + + + def __init__(self, pos, trails = []): + self.pos = pos + self._trails = trails + + + def addTrail(self, trail): + self._trails.append(trail) + + + def isActive(self): + return self._isActive + + + def activate(self): + for trail in self._trails: + trail.activate() + self._isActive = True + + + def deactivate(self): + for trail in self._trails: + trail.deactivate() + self._isActive = False diff --git a/Trail.py b/Trail.py new file mode 100644 index 0000000..f393645 --- /dev/null +++ b/Trail.py @@ -0,0 +1,29 @@ + +from Vec import Vec2 +import Trail + +class Trail: + + + _nextTrail = None + pos = None + _isOn = False + + + def __init__(self, pos: Vec2, nextTrail: Trail): + self.pos = pos + self._nextTrail = nextTrail + + + def isOn(self) -> bool: + return self._isOn + + + def activate(self): + self._isOn = True + self._nextTrail.activate() + + + def deactivate(self): + self._isOn = False + self._nextTrail.deactivate() diff --git a/Walls.py b/Walls.py new file mode 100644 index 0000000..860a7ef --- /dev/null +++ b/Walls.py @@ -0,0 +1,38 @@ + +from Vec import Vec2 + + +class Walls: + """Contains the walls of a game. + Supports iterating over the walls and checking if there is a wall at a specific coordinate. + Useful as a wrapper to later increase performance of these operations.""" + + _walls = [] + + def __init__(self, walls): + self._walls = walls + + def fromString(wallString): + walls = [] + y = 0 + for line in wallString.split('\n'): + x = 0 + for char in line: + if char == "#": + walls.append(Vec2(x, y)) + x += 1 + y += 1 + return Walls(walls) + + def walls(self): + return self._walls + + def wallAt(self, pos): + return (pos in self._walls) + + def width(self): + return max(self._walls, key=lambda p: p.x).x + 1 + + def height(self): + return max(self._walls, key=lambda p: p.y).y + 1 + |
