Exemple #1
0
def moveEntity(level: Level, name: str, currBoard: int, newBoardNum: int,
               destination: tuple, isPlayer: bool):
    """
    Move the Player/Enemy with the given name to the given destination
    in the level.
    :param level: Level
    :param name: str
    :param currBoard: int
    :param newBoardNum: int
    :param destination: tuple
    :param isPlayer: bool
    """
    board = level.boards[currBoard]
    entity = getPlayer(level, name) if isPlayer else getEnemy(level, name)
    destInBoard = locationInBounds(destination, board.origin, board.dimensions)

    if destInBoard:
        entity.location = destination
    else:
        boardEntities = board.players if isPlayer else board.enemies
        del boardEntities[name]
        if newBoardNum != -1:
            otherBoard = level.boards[newBoardNum]
            entity.location = destination

            if isPlayer:
                otherBoard.players[name] = entity
            else:
                otherBoard.enemies[name] = entity

            level.currBoard = newBoardNum

    return level
Exemple #2
0
def move(entityName: str, destination, game: Dungeon, isPlayer=True):
    """
    Updates the game to reflect the movement of a player if they
    can move to the given destination in the game or skip a turn when
    destination is None. Returns an unmodified game if the player cannot
    move based on game rules.
    :param entityName: str
    :param destination: tuple | None
    :param game: Dungeon
    """
    currLevel: Level = game.levels[game.currLevel]

    # Player move is skipped
    if destination is None:
        # only increment player turn
        updatedPlayerTurn = currLevel.playerTurn + 1
        if updatedPlayerTurn == len(game.players) - 1:
            currLevel.playerTurn = 0
        else:
            currLevel.playerTurn = updatedPlayerTurn
        return game

    entity = getPlayer(currLevel, entityName) if isPlayer else getEnemy(
        currLevel, entityName)
    currBoardNum = whichBoardInLevel(currLevel, entity.location)
    newBoardNum = whichBoardInLevel(currLevel, destination)
    numMoves = 2
    if isPlayer and playerCanMoveTo(destination, entity, currLevel, numMoves):
        updatedLevel = moveEntity(currLevel,
                                  entityName,
                                  currBoardNum,
                                  newBoardNum,
                                  destination,
                                  isPlayer=True)
        if updatedLevel.playerTurn == len(game.players) - 1:
            updatedLevel.playerTurn = 0
            updatedLevel.enemyTurn = updatedLevel.enemyTurn + 1
        else:
            updatedLevel.playerTurn = updatedLevel.playerTurn + 1
            updatedLevel.enemyTurn = updatedLevel.enemyTurn + 1
        game.levels[game.currLevel] = updatedLevel
        updatedGame = interact(entityName, destination, game)
        return updatedGame
    elif not isPlayer:
        updatedLevel = moveEntity(currLevel,
                                  entityName,
                                  currBoardNum,
                                  newBoardNum,
                                  destination,
                                  isPlayer=False)
        game.levels[game.currLevel] = updatedLevel
        updatedGame = enemyInteract(entityName, destination, game)
        return updatedGame
    else:
        return game
Exemple #3
0
def interactWithWall(enemyName: str, destination: tuple, game: Dungeon):
    """
    Only for ghosts, teleports the enemy to a random location in the level,
    or doesn't do anything if that's not possible. Returns an updated (or not)
    game.
    """
    currLevel = game.levels[game.currLevel]
    randBoardNum, randBoard = getRandomRoomInLevel(currLevel)
    enemy = getEnemy(currLevel, enemyName)
    while True:
        loc = genXRandCoords(1, [enemy.location], randBoard.origin,
                             randBoard.dimensions).pop()
        if not destHasWall(loc, currLevel) and not destHasPlayer(
                loc, randBoard):
            newDestForGhost = loc
            break
    # Move ghost to newDest, and delete from currBoard
    game = move(enemyName, newDestForGhost, game, isPlayer=False)
    return game