Exemplo n.º 1
0
    def __call__(self):
        retKey = ''
        key = getch()

        # If CTRL + C is pressed
        if (key == '\3'):
            exit()

        if (key == ' '):
            retKey = 'SPACE_KEY'

        if (key == '\r'):
            retKey = 'ENTER_KEY'

        if (key == '\033'):
            getch()

            arrow_key = getch()

            if (arrow_key == 'A'):
                retKey = 'U_KEY'
            elif (arrow_key == 'B'):
                retKey = 'D_KEY'
            elif (arrow_key == 'C'):
                retKey = 'R_KEY'
            elif (arrow_key == 'D'):
                retKey = 'L_KEY'

        return retKey
Exemplo n.º 2
0
    def __call__(self):
        retKey = ''

        first_ch = getch()

        if (first_ch == b' '):
            retKey = 'SPACE_KEY'

        if (first_ch == b'\r'):
            retKey = 'ENTER_KEY'

        # If CTRL + C is pressed
        if (first_ch == b'\x03'):
            exit()

        if (first_ch == b'\x00' or first_ch == b'\xe0'):

            key = getch()

            if (key == b'H'):
                retKey = 'U_KEY'
            elif (key == b'M'):
                retKey = 'R_KEY'
            elif (key == b'P'):
                retKey = 'D_KEY'
            elif (key == b'K'):
                retKey = 'L_KEY'

        return retKey
Exemplo n.º 3
0
    def __call__(self):
        retKey = ''
        key = getch()

        if (key == '\033'):
            getch()

            arrow_key = getch()

            if (arrow_key == 'A'):
                retKey = 'U_KEY'
            elif (arrow_key == 'B'):
                retKey = 'R_KEY'
            elif (arrow_key == 'C'):
                retKey = 'D_KEY'
            elif (arrow_key == 'D'):
                retKey = 'L_KEY'

        return retKey
Exemplo n.º 4
0
    def __call__(self):
        retKey = ''

        first_ch = getch()

        if (first_ch == b'\x03'):
            exit()

        if (first_ch == b'\x00'):

            key = getch()

            if (key == b'H'):
                retKey = 'U_KEY'
            elif (key == b'M'):
                retKey = 'R_KEY'
            elif (key == b'P'):
                retKey = 'D_KEY'
            elif (key == b'K'):
                retKey = 'L_KEY'

        return retKey
Exemplo n.º 5
0
 # valid_actions = "wasd"
 valid_actions = '123456789'
 for _ in range(30):
     s, _ = env.reset()
     cv2.imshow("env_state", cv2.resize(s, None, fx=5, fy=5))
     x = envmodel.encode(s)
     if use_latent_visualizer:
         x_visualized = lv.get_nearest_image(x)
         cv2.imshow("envmodel_state", cv2.resize(x_visualized, None,
                                                 fx=5, fy=5))
     done = False
     print("New game! Press q to quit")
     i = 0
     while not done:
         i += 1
         action = getch()
         if action == 'q':
             exit()
         elif action not in valid_actions:
             print("Input must be one of {}, was {}".format(valid_actions, action))
             continue # next loop iteration
         else:
             action = valid_actions.find(action)
         s, rew, done, info = env.step(action)
         x, _ = envmodel.stepforward(x, action)
         if use_latent_visualizer:
             x_visualized = lv.get_nearest_image(x)
             cv2.imshow("envmodel_state", cv2.resize(x_visualized, None,
                                                     fx=5, fy=5))
         gs = info['goal_state']
         gx = envmodel.encode(gs)
Exemplo n.º 6
0
import sys, os
# Extend NetworkClient path
extended_path = os.path.abspath(__file__)
parent = os.path.sep.join(extended_path.split(os.path.sep)[:-2])
sys.path.append(parent)

import socket
from utils.getch import getch

HOST = 'localhost'  #'192.168.0.6'       # The remote host
PORT = 58888  # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    client_loop = True
    while client_loop:
        data = getch()
        s.sendall(bytes(data, encoding="utf8"), socket.MSG_WAITALL)
        if data == "q":
            break

print("Client closed")
Exemplo n.º 7
0
    def battle(self, ctx, difficulty: int):
        """
        The deadly battle begins... Choose a difficulty between 1 and 5.
        Get coins when you succeed! The more difficult, the more coins you get.
        """
        if difficulty < 1 or difficulty > 5:
            return ctx.print("Please choose a difficulty between 1 and 5.")
        field = [
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
        ]
        boss_row = random.randint(0, 4)
        boss_column = random.randint(0, 4)
        field[boss_row][boss_column] = 1
        player_row = random.randint(0, 4)
        player_column = random.randint(0, 4)
        field[player_row][player_column] = 2
        if player_row == boss_row and player_column == boss_column:
            ctx.print("Oh, it looks like the boss is eager to fight with you.")
            ctx.print("They have come to you...")
        else:

            def print_field(field):
                ctx.print_empty()
                for row in field:
                    for column in row:
                        if column == 0:
                            ctx.print("•", end=" ")
                        elif column == 1:
                            ctx.print("X", end=" ")
                        else:
                            ctx.print("O", end=" ")
                    ctx.print_empty()

            move_speed = 6 - difficulty
            ctx.print("Before the battle starts, you need to catch the boss.")
            ctx.print(
                f"The boss will move every {move_speed} times you move to a random location."
            )
            ctx.print(
                'You are represented "O" and the boss is represented by "X".')
            ctx.print(f"Use [w] [a] [s] [d] to move.")
            print_field(field)

            end = False
            moves = 0
            while not end:
                ctx.print("\nEnter a move: ", end="")
                try:
                    movement = getch().lower()
                except UnicodeDecodeError:
                    return ctx.print(
                        "Uh oh, you entered an invalid character. Battle ends..."
                    )
                ctx.print(f"{movement}")
                ctx.print_empty()
                if movement not in ["w", "a", "s", "d"]:
                    return ctx.print(
                        "Uh oh, you entered an invalid movement. Battle ends..."
                    )
                if ((movement == "w" and player_row == 0)
                        or (movement == "a" and player_column == 0)
                        or (movement == "s" and player_row == 4)
                        or (movement == "d" and player_column == 4)):
                    return ctx.print(
                        "Uh oh, you bumped into the wall. Battle ends...")
                field[player_row][player_column] = 0
                if movement == "w":
                    player_row -= 1
                elif movement == "a":
                    player_column -= 1
                elif movement == "s":
                    player_row += 1
                elif movement == "d":
                    player_column += 1
                if field[player_row][player_column] == 1:
                    end = True
                else:
                    field[player_row][player_column] = 2
                    print_field(field)
                    moves += 1
                    if moves == move_speed:
                        moves = 0
                        ctx.print_empty()
                        ctx.print("The boss is moving...")
                        field[boss_row][boss_column] = 0
                        boss_row = random.randint(0, 4)
                        boss_column = random.randint(0, 4)
                        while boss_row == player_row and boss_column == player_column:
                            boss_row = random.randint(0, 4)
                        field[boss_row][boss_column] = 1
                        print_field(field)
            ctx.print("You caught the boss!")
            ctx.print_empty()
        ctx.print("THE DEADLY BATTLE BEGINS...")
        ctx.print_empty()
        character = self.handler.character
        sword = character.equipped_sword
        shield = character.equipped_shield
        boss_hp = difficulty * 100
        player_hp = 100 + character.level * 10
        while boss_hp > 0 and player_hp > 0:
            ctx.print(f"Boss HP: {boss_hp}")
            ctx.print(f"Player HP: {player_hp}")
            ctx.print_empty()
            boss_damage = random.randint(difficulty * 15, difficulty * 20)
            player_damage = random.randint(sword.level * 10, sword.level * 15)
            player_shield = random.randint(shield.level * 5, shield.level * 10)
            effective_damage = boss_damage - player_shield
            if effective_damage < 5:
                effective_damage = 5
            ctx.print(f"The boss attacked and dealt {boss_damage} damage.")
            ctx.print(
                f"Because of your shield, effective damage was {effective_damage}."
            )
            player_hp -= effective_damage
            if player_hp < 0:
                break
            ctx.print("Press any key to attack...")
            getch()
            boss_hp -= player_damage
            ctx.print(f"You dealt {player_damage} damage.")
            ctx.print_empty()
        if boss_hp < 0:
            ctx.print("The boss was defeated!")
            xp = random.randint(difficulty * 15, difficulty * 20)
            coins = random.randint(difficulty * 15, difficulty * 20)
            self.handler.character.add_xp(xp)
            self.handler.character.add_coins(coins)
            ctx.print(f"You received {xp} XP and {coins} coins.")
        else:
            ctx.print("You were defeated! Better luck next time...")