def load(self):
        """load the game if save file exists

        Returns:
            bool -- true if game has been succesfully loaded,
                false otherwise
        """

        has_loaded = False

        try:
            with open("sav.txt", "rb") as savefile:

                temp_dict = pickle.load(savefile)
                self.__dict__.update(temp_dict)
                log_debug("game loaded")
                has_loaded = True

        except OSError:
            log_info("No save file found :(")

        return has_loaded
Ejemplo n.º 2
0
    def __enemy_move(self):
        """enemy moving loop
        """

        while True:

            time.sleep(1)

            self.__lock.acquire()

            if self.__player.position == self.__enemy.position:

                if self.__player.health > 0:

                    self.__player.damage(1)
                    log_info("Enemy has found and damaged the player!")

                    if self.__player.health == 0:
                        log_info("Enemy has killed the player!")
                        self.__end_of_game.set()

            self.__lock.release()

            self.__enemy.move()
"""main module for Dungeon Game
"""
import exceptions

from game_logger import log_debug
from game_logger import log_info

import terrain
import game

debug_mode = False

log_info("Welcome to the game!")

game_session = None

while True:

    log_info("Type \"new\" to start new game")
    log_info("Type \"load\" to load saved game")
    log_info("Type \"debug\" to enter debug mode")

    try:
        command = input()

        if command == "new":

            try:

                scale = int(input("Enter map scale: "))
    def run(self):
        """run the game

            function is proceeded until player finds a treasure or enters a trap

        Arguments:
            start_row int -- start position row
            start_col int -- start position column
            squares_map [str] -- game map
        """

        log_info("Type \"up/down/left/right\" to move")
        log_info("Type \"save\" to save current game")

        border = self.__terrain.scale - 1

        while True:

            curr_row, curr_col = self.__player.position

            if self.__terrain.get_square(curr_row, curr_col) == 't':

                log_debug("player has entered a trap")
                log_info("You have entered a trap!")
                self.__player.damage(1)

            if self.__terrain.get_square(curr_row, curr_col) == '*':

                log_debug("player has found a treasure")
                log_info("You have found a treasure!")
                self.__player.grant_treasure(1)

            if self.__player.health <= 0:
                log_debug("player has died")
                log_debug("game ends")
                log_info("You have died!")
                break

            if self.__player.bag >= self.__win_condition:
                log_debug("player has found enough")
                log_debug("game ends")
                log_info("You have found enogh treasure!")
                break

            if self.__structure_is_near('t'):

                log_debug("trap is within one square")
                log_info('There is a trap within one square!')

            if self.__structure_is_near('*'):

                log_debug("treasure is within one square")
                log_info('There is a treasure within one square!')

            moved = False

            while not moved:

                command = input("What to do?: ")

                if command == 'up':

                    if curr_row == 0:

                        log_info("You are already on top of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row -= 1
                        log_info("Moving up")

                elif command == 'down':

                    if curr_row == border:

                        log_info("You are already on bottom of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row += 1
                        log_info("Moving down")

                elif command == 'right':

                    if curr_col == border:

                        msg = "You are already on the right border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col += 1
                        log_info("Moving right")

                elif command == 'left':

                    if curr_col == 0:

                        msg = "You are already on the left border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col -= 1
                        log_info("Moving left")

                elif command == "save":

                    with open("sav.txt", "wb") as savefile:

                        pickle.dump(self.__dict__, savefile, protocol=0)
                        log_info("Saved succesfully")

                else:
                    log_info(F"Unknown command: {command}")

                if moved:

                    self.__player.set_position(curr_row, curr_col)
                    log_debug(F"moved {command}")
                    cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                    log_debug(cur_pos_msg)

        log_info(self.__terrain)
        log_info("* - stands for a treasure")
        log_info("t - stands for a trap")
        log_info(". - stands for an empty square")
        log_info("s - stands for the start position\n")
Ejemplo n.º 5
0
    def run(self):
        """run the game

            function is proceeded until player finds a
            treasure or enters a trap

        Arguments:
            start_row int -- start position row
            start_col int -- start position column
            squares_map [str] -- game map
        """

        enemy_thread = threading.Thread(target=self.__enemy_move)
        player_thread = threading.Thread(target=self.__player_move)

        enemy_thread.setDaemon(True)
        player_thread.setDaemon(True)

        log_info("Type \"up/down/left/right\" to move")
        log_info("Type \"save\" to save current game")

        player_thread.start()
        enemy_thread.start()

        self.__end_of_game.wait()

        log_info(self.__terrain)
        log_info("* - stands for a treasure")
        log_info("t - stands for a trap")
        log_info(". - stands for an empty square")
        log_info("s - stands for the start position\n")
Ejemplo n.º 6
0
    def __player_move(self):
        """player moving loop
        """

        border = self.__terrain.scale - 1

        while True:

            curr_row, curr_col = self.__player.position

            if self.__terrain.get_square(curr_row, curr_col) == 't':

                log_debug("player has entered a trap")
                log_info("You have entered a trap!")
                self.__player.damage(1)

            if self.__terrain.get_square(curr_row, curr_col) == '*':

                log_debug("player has found a treasure")
                log_info("You have found a treasure!")
                self.__player.grant_treasure(1)

            if self.__player.health <= 0:
                log_debug("player has died")
                log_debug("game ends")
                log_info("You have died!")
                self.__end_of_game.set()

            if self.__player.bag >= self.__win_condition:
                log_debug("player has found enough")
                log_debug("game ends")
                log_info("You have found enogh treasure!")
                self.__end_of_game.set()

            if self.__structure_is_near('t'):

                log_debug("trap is within one square")
                log_info('There is a trap within one square!')

            if self.__structure_is_near('*'):

                log_debug("treasure is within one square")
                log_info('There is a treasure within one square!')

            moved = False

            while not moved:

                command = input("What to do?: ")

                if command == 'up':

                    if curr_row == 0:

                        log_info("You are already on top of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row -= 1
                        log_info("Moving up")

                elif command == 'down':

                    if curr_row == border:

                        log_info("You are already on bottom of the map!")
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_row += 1
                        log_info("Moving down")

                elif command == 'right':

                    if curr_col == border:

                        msg = "You are already on the right border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col += 1
                        log_info("Moving right")

                elif command == 'left':

                    if curr_col == 0:

                        msg = "You are already on the left border of the map!"
                        log_info(msg)
                        self.__write_move_fail_to_log(command)

                    else:

                        moved = True
                        curr_col -= 1
                        log_info("Moving left")

                elif command == "save":

                    with open("sav.txt", "wb") as savefile:

                        pickle.dump(self.__dict__, savefile, protocol=0)
                        log_info("Saved succesfully")

                else:
                    log_info(F"Unknown command: {command}")

                if moved:

                    self.__lock.acquire()
                    self.__player.set_position(curr_row, curr_col)
                    self.__lock.release()

                    log_debug(F"moved {command}")
                    cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                    log_debug(cur_pos_msg)
def run(start_row, start_col, squares_map):
    """run the game

        function is proceeded until player finds a treasure or enters a trap

    Arguments:
        start_row int -- start position row
        start_col int -- start position column
        squares_map [str] -- game map
    """

    log_info("Type \"up/down/left/right\" to move")
    log_info("Type \"save\" to save current game")

    border = len(squares_map) - 1

    curr_row = start_row
    curr_col = start_col

    while True:

        if squares_map[curr_row][curr_col] == 't':

            log_debug("player has found a treasure")
            log_debug("game ends")
            log_info("You have entered a trap!")
            break

        if squares_map[curr_row][curr_col] == '*':

            log_debug("player has entered a trap")
            log_debug("game ends")
            log_info("You have found a treasure!")
            break

        if _structure_is_near('t', curr_row, curr_col, squares_map):

            log_debug("trap is within one square")
            log_info('There is a trap within one square!')

        if _structure_is_near('*', curr_row, curr_col, squares_map):

            log_debug("treasure is within one square")
            log_info('There is a treasure within one square!')

        moved = False

        while not moved:

            command = input("What to do?: ")

            if command == 'up':

                if curr_row == 0:

                    log_info("You are already on top of the map!")
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_row -= 1
                    log_info("Moving up")

            elif command == 'down':

                if curr_row == border:

                    log_info("You are already on bottom of the map!")
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_row += 1
                    log_info("Moving down")

            elif command == 'right':

                if curr_col == border:

                    msg = "You are already on the right border of the map!"
                    log_info(msg)
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_col += 1
                    log_info("Moving right")

            elif command == 'left':

                if curr_col == 0:

                    msg = "You are already on the left border of the map!"
                    log_info(msg)
                    _write_move_fail_to_log(command)

                else:

                    moved = True
                    curr_col -= 1
                    log_info("Moving left")

            elif command == "save":

                with open("sav.txt", "wb") as savefile:

                    data_to_save = [curr_row, curr_col, squares_map]
                    pickle.dump(data_to_save, savefile, protocol=0)
                    log_info("Saved succesfully")

            else:
                log_info(F"Unknown command: {command}")

            if moved:

                log_debug(F"moved {command}")
                cur_pos_msg = F"current position: ({curr_row}, {curr_col})"
                log_debug(cur_pos_msg)
import os.path

from game_logger import log_debug
from game_logger import log_info

import terrain
import game


debug_mode = False

start_row = 0
start_col = 0
squares_map = []

log_info("Welcome to the game!")

while True:

    log_info("Type \"new\" to start new game")
    log_info("Type \"load\" to load saved game")
    log_info("Type \"debug\" to enter debug mode")

    command = input()

    if command == "new":

        scale = input("Enter map scale: ")
        start_row, start_col, squares_map = terrain.generate(int(scale))
        log_debug("new game created")
        break
"""main module for Dungeon Game
"""

from game_logger import log_debug
from game_logger import log_info

import terrain
import game


debug_mode = False

log_info("Welcome to the game!")

game_session = None

while True:

    log_info("Type \"new\" to start new game")
    log_info("Type \"load\" to load saved game")
    log_info("Type \"debug\" to enter debug mode")

    command = input()

    if command == "new":

        scale = input("Enter map scale: ")
        game_session = game.GameSession(3, 3, int(scale))
        log_debug("new game created")
        break