def test_start_fight(self):
     f = Fight()
     h = Hero("Geralt", "White wolf", 100, 100, 5)
     h.equip(Weapon("Axe", 40))
     h.learn(Spell("Storm", 30, 50, 3))
     e = Enemy(100, 200, 30)
     e.equip(Weapon("Sword", 40))
     e.learn(Spell("Fire", 30, 20, 3))
     d = 2
     h = f.start_fight(h, e, d)
     print('=======================')
     print(h.known_as(), h.get_health())
Esempio n. 2
0
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        self.dungeon = None
        self.hero = None

        self.left_pressed = False
        self.right_pressed = False
        self.up_pressed = False
        self.down_pressed = False
        self.move_char = (".", [0, 0])
        self.fight = Fight()
        arcade.set_background_color((59, 68, 75))

    def setup(self):
        self.dungeon = Dungeon("map.txt")
        self.hero = Hero("Geralt", "white wolf", 150, 150, 5)
        self.hero.equip(Weapon("Sword", 30))
        self.hero.learn(Spell("wolf's attack", 20, 20, 2))
        self.dungeon.spawn(self.hero)
        self.command = None

    def on_draw(self):
        arcade.start_render()

        x = self.dungeon.borders[1] * 20 + LEFT_MARGIN - 20
        y = 260
        arcade.draw_rectangle_filled(x, y, 410, 410, (135, 169, 107))
        arcade.draw_rectangle_outline(x, y, 420, 420, (105, 53, 156), 10)
        arcade.draw_text("To move pres the keybord arrows.\nTo attack first you should choose\ndirection. Press on of the keys:\n 'u' for up, 'd' for down,\n 'l' for left, 'r' for right\n......................................",\
            x - 200, y + 100, (55, 3, 106), 14)

        # Prints the map
        for row in range(self.dungeon.borders[0]):
            # Loop for each column
            for column in range(self.dungeon.borders[1]):
                # Calculate our location
                x = column * COLUMN_SPACING + LEFT_MARGIN
                y = (self.dungeon.borders[0] - row -
                     1) * ROW_SPACING + BOTTOM_MARGIN

                # Draw the item
                if (self.dungeon.matrix[row][column] == "#"):
                    arcade.draw_rectangle_filled(x, y, 30, 30, (191, 79, 81))
                elif (self.dungeon.matrix[row][column] == "-"):
                    if row == 0 or row == self.dungeon.borders[0] - 1:
                        if column == 0 or column == self.dungeon.borders[1] - 1:
                            arcade.draw_rectangle_filled(
                                x, y, 30, 30, (59, 68, 75))
                        else:
                            arcade.draw_rectangle_filled(
                                x, y, 50, 30, (59, 68, 75))
                    if column == 0 or column == self.dungeon.borders[1] - 1:
                        if row == 0 or row == self.dungeon.borders[0] - 1:
                            arcade.draw_rectangle_filled(
                                x, y, 30, 30, (59, 68, 75))
                        else:
                            arcade.draw_rectangle_filled(
                                x, y, 30, 50, (59, 68, 75))
                elif (self.dungeon.matrix[row][column] == "H"):
                    arcade.draw_rectangle_filled(x, y, 30, 30, (79, 134, 247))
                    arcade.draw_text("H", x - 10, y - 20, arcade.color.BLUE,
                                     28)
                elif (self.dungeon.matrix[row][column] == "E"):
                    arcade.draw_rectangle_filled(x, y, 30, 30, (255, 117, 24))
                    arcade.draw_text("E", x - 10, y - 20, arcade.color.RED, 28)
                elif (self.dungeon.matrix[row][column] == "T"):
                    arcade.draw_rectangle_filled(x, y, 30, 30, (150, 120, 182))
                    arcade.draw_text("T", x - 10, y - 20, (55, 3, 106), 28)
                elif (self.dungeon.matrix[row][column] == "G"):
                    arcade.draw_rectangle_filled(x, y, 30, 30, (153, 230, 179))
                    arcade.draw_text("G", x - 10, y - 20, (0, 130, 127), 28)
                else:
                    arcade.draw_rectangle_filled(x, y, 30, 30, (135, 169, 107))

        x = self.dungeon.borders[1] * 20 + LEFT_MARGIN - 20
        y = self.dungeon.borders[1] * 10 + BOTTOM_MARGIN
        arcade.draw_rectangle_outline(x, y, 420, 220, (105, 53, 156), 10)

    def on_update(self, delta_time):
        """
        if self.up_pressed and not self.down_pressed:
            self.dungeon.move_hero("up")
        elif self.down_pressed and not self.up_pressed:
            self.dungeon.move_hero("down")
        if self.left_pressed and not self.right_pressed:
            self.dungeon.move_hero("left")
        elif self.right_pressed and not self.left_pressed:
            self.dungeon.move_hero("left")
        """

    def on_key_press(self, key, modifiers):
        """Called whenever a key is pressed. """

        if key == arcade.key.UP:

            self.move_char = self.dungeon.move_hero("up")
            print("HERREEEEERERERER")  #, self.move_hero)

            self.check_move()
            #self.up_pressed = True
        elif key == arcade.key.DOWN:

            self.move_char = self.dungeon.move_hero("down")
            self.check_move()
            #self.down_pressed = True
        elif key == arcade.key.LEFT:

            self.move_char = self.dungeon.move_hero("left")
            self.check_move()
            # self.left_pressed = True
        elif key == arcade.key.RIGHT:

            self.move_char = self.dungeon.move_hero("right")
            self.check_move()
            # self.right_pressed = True

        elif key == arcade.key.NUM_1 or key == arcade.key.KEY_1:
            self.command = self.fight.get_hero_command(1)

        elif key == arcade.key.NUM_2 or key == arcade.key.KEY_2:
            print('hhhhhhh')
            self.command = self.fight.get_hero_command(2)
            print(self.command)

        elif key == arcade.key.NUM_3 or key == arcade.key.KEY_3:
            self.command = self.fight.get_hero_command(3)

        elif key == arcade.key.NUM_4 or key == arcade.key.KEY_4:
            self.command = self.fight.get_hero_command(4)

    def on_key_release(self, key, modifiers):
        """Called when the user releases a key. """

        if key == arcade.key.UP:
            self.up_pressed = False
        elif key == arcade.key.DOWN:
            self.down_pressed = False
        elif key == arcade.key.LEFT:
            self.left_pressed = False
        elif key == arcade.key.RIGHT:
            self.right_pressed = False

    def check_move(self):
        print("nznznz", self.move_char)
        if self.move_char[0] == "E":
            print("WTF???")
            #fight = Fight()
            self.dungeon.hero = self.battle(1)
            if self.dungeon.hero.is_alive():
                self.dungeon.change_map(".")
                self.dungeon.hero_place[self.move_char[1]] = self.move_char[2]
                self.dungeon.change_map("H")
        else:
            return "Hero made move."

    def battle(self, distance):
        enemy = self.dungeon.enemies.pop()
        self.fight.start_fight(self.dungeon.hero, enemy, distance)
        while self.dungeon.hero.is_alive() and enemy.is_alive():
            # the desition of the player
            # self.fight.distance = distance
            if self.dungeon.hero.is_alive():
                print(self.command)
                if self.command is not None:

                    if enemy.is_alive():
                        print(self.fight.hero_attack())
                        if not enemy.is_alive():
                            print("Enemy is dead.")
                if self.enemy.is_alive():
                    if self.hero.is_alive():
                        print(self.fight.enemy_attack())
                        if not self.dungeon.hero.is_alive():
                            print("Hero is dead")
        return self.hero
Esempio n. 3
0
class Dungeon:
    def __init__(self, text_file , lootListPath):
        self.matrix = []
        with open(str(text_file)) as f:
            self.matrix = f.readlines()
        self.matrix = [x.strip() for x in self.matrix]
        self.hero = None
        self.fight = Fight()
        self.borders = [len(self.matrix), len(self.matrix[0])]
        self.hero_place = []
        self.end_of_game = False
        enemy = Enemy(100, 120, 20)
        enemy.equip(Weapon("Sword", 40))
        enemy.learn(Spell("Fire", 30, 20, 3))
        self.enemies = [enemy, Enemy(70, 70, 15), Enemy(50, 50, 10)]
        self.treasure = [Weapon("Axe", 35), Spell("FireStorm", 40, 20, 3), ["h", 20], ["h", 30]]

        self.loot_dict = self.__class__.extract_loot_dictionary(lootListPath)

    @classmethod
    def extract_loot_dictionary(cls, filePath):
        with open(filePath, 'r') as f:
            loot_dict = json.load(f)
        return loot_dict

    def print_map(self):
        for line in self.matrix:
            print(line)

    def get_start(self):
        for i in range(len(self.matrix)):
            if 'S' in self.matrix[i]:
                return [i, self.matrix[i].index('S')]
        return [0, 0]

    def get_end(self):
        for i in range(len(self.matrix)):
            if 'G' in self.matrix[i]:
                return [i, self.matrix[i].index('G')]
        return [0, 0]

    def spawn(self, hero):
        self.hero = hero
        if hero.is_alive():
            self.hero_place = self.get_start()
            self.change_map("H")
        else:
            print('End')

    def change_map(self, ch):
        s = list(self.matrix[self.hero_place[0]])
        s[self.hero_place[1]] = ch
        self.matrix[self.hero_place[0]] = "".join(s)

    def get_treasure(self):
        treasure = random.choice(self.treasure)
        if type(treasure) is list:
            self.hero.take_healing(treasure[1])
            print("hero health is", self.hero.get_health())
        elif type(treasure) is Spell:
            self.hero.learn(treasure)
            print("hero learned", treasure.name)
        else:
            self.hero.equip(treasure)
            print("hero found", treasure.name)
        print()

    def move_hero(self, direction):
        directions_vertical = {'up': -1, 'down': 1}
        directions_horisontal = {'left': -1, 'right': 1}
        if direction in directions_vertical.keys():
            next_point = self.hero_place[0] + directions_vertical[direction]
            if next_point > 0 or next_point < self.borders[0]:
                if self.matrix[next_point][self.hero_place[1]] == "#":
                    return False
                if self.matrix[next_point][self.hero_place[1]] == "-":
                    return False
                elif self.matrix[next_point][self.hero_place[1]] == "E":
                    self.hero_atack(direction)
                    self.change_map(".")
                    self.hero_place[0] = next_point
                    self.change_map("H")
                elif self.matrix[next_point][self.hero_place[1]] == "T":
                    self.change_map(".")
                    self.hero_place[0] = next_point
                    self.change_map("H")
                    self.get_treasure()
                    # add treasure
                    # print("Found treasure!")
                elif self.matrix[next_point][self.hero_place[1]] == ".":
                    self.change_map(".")
                    self.hero_place[0] = next_point
                    self.change_map("H")
                else:
                    print("End of the game!")
                    print("You won!!!")
            else:
                return False
        if direction in directions_horisontal.keys():
            next_point = self.hero_place[1] + directions_horisontal[direction]
            if next_point > 0 or next_point < self.borders[1]:
                if self.matrix[self.hero_place[0]][next_point] == "#":
                    return False
                if self.matrix[self.hero_place[0]][next_point] == "-":
                    return False
                elif self.matrix[self.hero_place[0]][next_point] == "E":
                    self.hero_atack(direction)
                    self.change_map(".")
                    self.hero_place[1] = next_point
                    self.change_map("H")
                elif self.matrix[self.hero_place[0]][next_point] == "T":
                    self.change_map(".")
                    self.hero_place[1] = next_point
                    self.change_map("H")
                    # add treasure
                    print("Found treasure!")
                elif self.matrix[self.hero_place[0]][next_point] == ".":
                    self.change_map(".")
                    self.hero_place[1] = next_point
                    self.change_map("H")
                else:
                    self.end_of_game = True
                    print("End of the game!")
                    print("You won!!!")
            else:
                return False
        return False

    def hero_atack(self, direction):
        distance = self.check_for_enemy(direction)
        if distance == 0:
            print("There is no enemy in hero's ramge")
        else:
            enemy = self.enemies.pop()
            self.hero = self.fight.start_fight(self.hero, enemy, distance)
            if not self.hero.is_alive():
                self.end_of_game = True
            # else:
            #    s = list(self.matrix[coord[0]])
            #    s[coord[1]] = "."
            #    self.matrix[self.hero_place[0]] = "".join(s)

    def check_for_enemy(self, direction):
        directions_vertical = {'up': -1, 'down': 1}
        directions_horisontal = {'left': -1, 'right': 1}
        spell_range = self.hero.spell.cast_range
        coord = self.hero_place
        distance = 0

        if direction in directions_vertical.keys():
            for i in range(spell_range + 1):
                index = self.hero_place[0] + i * directions_vertical[direction]
                if index <= 0 or index >= self.borders[1] - 1:
                    break
                if self.matrix[index][self.hero_place[1]] == "#":
                    distance = 0
                    break
                elif self.matrix[index][self.hero_place[1]] == "E":
                    coord = [index, self.hero_place[1]]
                    
                    ############

                    s = list(self.matrix[index])
                    s[hero_place[1]] == "."
                    self.matrix[index] = "".join(s)


                    break
                else:
                    distance += 1
            return [distance, coord]
        if direction in directions_horisontal.keys():
            for i in range(spell_range + 1):
                index = self.hero_place[1] + i * directions_horisontal[direction]
                if index <= 0 or index >= self.borders[1] - 1:
                    break
                if self.matrix[self.hero_place[0]][index] == "#":
                    return 0
                elif self.matrix[self.hero_place[0]][index] == "E":
                    coord = [self.hero_place[1], index]
                    
                    #############

                    s = list(self.matrix[self.hero_place[0]])
                    s[index] = "."
                    self.matrix[self.hero_place[0]] = "".join(s)

                    return [distance, coord]
                else:
                    distance += 1
        return [0, coord]

    def remove_enemy(self, direction):
        directions_vertical = {'up': -1, 'down': 1}
        directions_horisontal = {'left': -1, 'right': 1}
        spell_range = self.hero.spell.cast_range

        distance = 0

        if direction in directions_vertical.keys():
            for i in range(spell_range + 1):
                index = self.hero_place[0] + i * directions_vertical[direction]
                if index <= 0 or index >= self.borders[1] - 1:
                    break
                if self.matrix[index][self.hero_place[1]] == "#":
                    distance = 0
                    break
                elif self.matrix[index][self.hero_place[1]] == "E":
                    break
                else:
                    distance += 1
            return distance
        if direction in directions_horisontal.keys():
            for i in range(spell_range + 1):
                index = self.hero_place[1] + i * directions_horisontal[direction]
                if index <= 0 or index >= self.borders[1] - 1:
                    break
                if self.matrix[self.hero_place[0]][index] == "#":
                    return 0
                elif self.matrix[self.hero_place[0]][index] == "E":
                    return distance
                else:
                    distance += 1
        return 0