Example #1
0
    def create_end_point(self):
        os.system('cls' if os.name == 'nt' else 'clear')
        player = Player()
        self.field.enter(player)
        print(self.field.render())
        print()

        selection = input(
            "Enter the coordinate of the item (e.g row, col)\n'B' to return to configure menu.\n'M' to return to main menu. "
        ).lower()

        if not (selection == 'm' or selection == 'b'):
            try:
                split = selection.split(',')
                row = int(split[0])
                col = int(split[1])

                block = self.field.blocks[row - 1][col - 1]

                if not isinstance(block, Path):
                    print(f"Cannot place an end point on non-passageways!")
                    input("Press Enter to continue...")
                    return

                if block == self.field.start_block:
                    print(f"Cannot place an end point on the start block!")
                    input("Press Enter to continue...")
                    return

                blocks = [block for row in self.field.blocks for block in row]
                portals = [
                    block for block in blocks if isinstance(block, Portal)
                ]

                for portal in portals:
                    self.field.blocks[portal.y][portal.x] = Path(
                        portal.x, portal.y)

                self.field.blocks[row - 1][col - 1] = Portal(col - 1, row - 1)
                self.field.enter(player)

                os.system('cls' if os.name == 'nt' else 'clear')
                self.field.enter(Player())
                print(self.field.render())
                print()
                print(f"End Point block placed at {row}, {col}")
                input("Press Enter to continue...")
            except:
                print("Invalid block coordinates!")
                input("Press Enter to continue...")
        elif selection == 'm':
            self.end()
Example #2
0
def test_player_block_render():
    player = Player()
    path = Path(0, 0)

    path.step(player)

    assert path.render() == 'A'
Example #3
0
 def sensorLoop(self):
     while True:
         for sensor in self.sensors:
             sensor.read()
         time.sleep(SENSOR_TIMEOUT)
         for sensor, player in zip(self.sensors, pinConfiguration.values()):
             if sensor.value == 1:
                 self.punchIn(Player(player))
Example #4
0
    def view_maze(self):
        os.system('cls' if os.name == 'nt' else 'clear')

        if self.field == None:
            print("There is no maze currently loaded.")
            input("Press Enter to continue...")
            return

        self.player = Player()
        self.field.enter(self.player)
        print(self.field.render())
        input("Press Enter to continue...")
def test_field_move_wall():
    left_top = Path(0, 0)
    right_top = Wall(1, 0)
    left_bottom = Path(0, 1)
    right_bottom = Path(1, 1)
    blocks = [[left_top, right_top], [left_bottom, right_bottom]]
    field = Field(blocks, left_top)
    player = Player()

    field.enter(player)
    field.move(player, Direction.right)
    assert player.block == left_top
Example #6
0
def test_player_portal_block_step():
    player = Player()
    path = Path(0, 0)
    portal = Portal(0, 1)

    path.step(player)

    assert player.is_finished == False

    portal.step(player)

    assert player.is_finished == True
Example #7
0
    def load_maze(self):
        os.system('cls' if os.name == 'nt' else 'clear')
        file_name = input("Enter file name (without extension): ")

        try:
            csv_file = open(f'{file_name}.csv')
            csv_reader = csv.reader(csv_file, delimiter=',')
            csv_data = list(csv_reader)
            blocks = [[] for _ in range(sum(1 for row in csv_data))]
            start_block = None
            is_invalid = False
            csv_file.close()

            for i, row in enumerate(csv_data):
                for ii, col in enumerate(row):
                    block = (Wall(ii, i) if col == 'X' else
                             Portal(ii, i) if col == 'B' else
                             Path(ii, i) if col == 'A' or col == 'O' else True)
                    blocks[i].append(block)
                    if col == 'A':
                        start_block = block

                    if block == True:
                        is_invalid = True
                        break

            if is_invalid:
                print(
                    "Invalid file type/content, please ensure that the file is proper."
                )
                input("Press Enter to continue...")
                return

            if start_block is None:
                print(
                    "Maze does not contain a start point, please verify the maze file!"
                )
                input("Press Enter to continue...")
                return

            print(f'Processed {len(blocks)} lines.')

            self.field = Field(blocks, start_block)
            self.player = Player()
            self.field.enter(self.player)
            print("\n" + self.field.render() + '\n')

            print("Successfully loaded maze!")
            input("Press Enter to continue...")
        except IOError:
            print("File does not exist.")
            input("Press Enter to continue...")
def test_field_enter():
    start_block = Path(0, 0)
    blocks = [[start_block, Path(1, 0)]]
    field = Field(blocks, start_block)
    player = Player()

    assert player.field == None
    assert player.block == None

    field.enter(player)

    assert player.field == field
    assert player.block == start_block
Example #9
0
    def play_maze(self):
        os.system('cls' if os.name == 'nt' else 'clear')

        if self.field == None:
            print("There is no maze currently loaded.")
            input("Press Enter to continue...")
            return

        self.player = Player()
        self.field.enter(self.player)
        self.is_in_maze = True

        while self.is_in_maze:
            os.system('cls' if os.name == 'nt' else 'clear')
            print(self.field.render())
            print()

            start = self.field.start_block
            blocks = [block for row in self.field.blocks for block in row]
            ends = [block for block in blocks if isinstance(block, Portal)]
            print("Location of start (A) = {0}".format(
                f"(row {start.y + 1}, col {start.x + 1})"))
            print("Location of End (B) = {0}".format(', '.join([
                f"(row {block.y + 1}, col {block.x + 1})" for block in ends
            ])))

            def move(player, direction):
                if not self.field.move(player, direction):
                    print("Invalid movement entered in game. Please try again")
                    time.sleep(1)

            menu = Menu([
                Option("w", "Move up",
                       lambda: move(self.player, Direction.up)),
                Option("a", "Move left",
                       lambda: move(self.player, Direction.left)),
                Option("s", "Move down",
                       lambda: move(self.player, Direction.down)),
                Option("d", "Move right",
                       lambda: move(self.player, Direction.right)),
                Option("m", "Return to menu", lambda: self.end_play_maze()),
            ])
            selection = input(
                "Use the WASD to move or M key to return to menu: ")
            menu.select(selection)

            if self.player.is_finished:
                print()
                print("You have completed the maze, congratulations!")
                input("Press Enter to continue...")
                self.end_play_maze()
Example #10
0
    def create_wall(self):
        os.system('cls' if os.name == 'nt' else 'clear')
        player = Player()
        self.field.enter(player)
        print(self.field.render())
        print()

        selection = input(
            "Enter the coordinate of the item (e.g row, col)\n'B' to return to configure menu.\n'M' to return to main menu. "
        ).lower()

        if not (selection == 'm' or selection == 'b'):
            try:
                split = selection.split(',')
                row = int(split[0])
                col = int(split[1])

                block = self.field.blocks[row - 1][col - 1]

                if not isinstance(block, Path):
                    print(f"Walls can only be placed over paths")
                    input("Press Enter to continue...")
                    return

                self.field.blocks[row - 1][col - 1] = Wall(col - 1, row - 1)
                self.field.enter(player)

                os.system('cls' if os.name == 'nt' else 'clear')
                self.field.enter(Player())
                print(self.field.render())
                print()
                print(f"Wall block placed at {row}, {col}")
                input("Press Enter to continue...")
            except:
                print("Invalid block coordinates!")
                input("Press Enter to continue...")
        elif selection == 'm':
            self.end()
Example #11
0
    def create_start_point(self):
        os.system('cls' if os.name == 'nt' else 'clear')
        player = Player()
        self.field.enter(player)
        print(self.field.render())
        print()

        selection = input(
            "Enter the coordinate of the item (e.g row, col)\n'B' to return to configure menu.\n'M' to return to main menu. "
        ).lower()

        if not (selection == 'm' or selection == 'b'):
            try:
                split = selection.split(',')
                row = int(split[0])
                col = int(split[1])

                block = self.field.blocks[row - 1][col - 1]

                if not isinstance(block, Path):
                    print(f"Cannot place a start point on non-passageways!")
                    input("Press Enter to continue...")
                    return

                self.field.start_block = block
                self.field.enter(player)

                os.system('cls' if os.name == 'nt' else 'clear')
                self.field.enter(Player())
                print(self.field.render())
                print()
                print(f"A Start Point block placed at {row}, {col}")
                input("Press Enter to continue...")
            except:
                print("Invalid block coordinates!")
                input("Press Enter to continue...")
        elif selection == 'm':
            self.end()
Example #12
0
def test_player_block_step():
    player = Player()
    path1 = Path(0, 0)
    path2 = Path(0, 1)

    assert player.block == None

    path1.step(player)

    assert player.block == path1

    path2.step(player)

    assert player.block == path2
def test_field_render():
    start_block = Path(0, 0)
    blocks = [[start_block, Path(1, 0)]]
    field = Field(blocks, start_block)

    assert field.render() == 'O, O'

    player = Player()
    field.enter(player)

    assert field.render() == 'A, O'

    start_block = Path(0, 0)
    blocks = [[start_block, Path(1, 0)], [Path(0, 1), Path(1, 1)]]
    field = Field(blocks, start_block)

    assert field.render() == 'O, O\nO, O'
Example #14
0
    def start(self):
        while not self.ended:
            os.system('cls' if os.name == 'nt' else 'clear')
            self.field.enter(Player())
            print(self.field.render())
            print()
            print("Configuration Menu")
            print("==================")

            menu = Menu([
                Option("1", "Create wall", self.create_wall),
                Option("2", "Create passageway", self.create_passageway),
                Option("3", "Create start point", self.create_start_point),
                Option("4", "Create end point", self.create_end_point),
                Option("0", "Exit to Main Menu", lambda: self.end()),
            ])
            print(menu.render())
            selection = input("Enter your option: ")
            menu.select(selection)
    def start_game(self):
        # Start by clearing everything
        self.reset()

        # Create a player in the middle of the window
        self.player = Player(pos=(self.width / 2, self.height / 2), player_batch=self.player_batch,
                             ui_batch=self.ui_batch)
        self._add_game_object(self.player)

        # Add a level that controls enemy and pellet spawning
        self.level = Level(player=self.player, batch=self.main_batch)
        self._add_game_object(self.level)

        # Add UI which is only the score for now
        self.ui = GameUI(player=self.player, space=self.space, ui_batch=self.ui_batch,
                         background_batch=self.background_batch)
        self._add_game_object(self.ui)

        # Add walls on window edges
        self._add_walls()

        # Initialize collisions
        Pellet.init_collision(self)
        EnemySlider.init_collision(self)
Example #16
0
 def setUp(self):
     self.data = loadDatabase("tests/sampleData")
     self.player1 = Player("Herbert")
     self.player2 = Player("Toni")
     self.game = Game(self.data)
     self.date = datetime.datetime(2020, 2, 25, 22, 22, 22)