Exemplo n.º 1
0
class TestLevel(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        minimal_setup_for_game()
        cls.save_path = "saves/save_0.xml"

    def setUp(self):
        # Window parameters
        screen = pg.display.set_mode((MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT))
        self.start_screen = StartScreen(screen)
        self.start_screen.display()

    def import_save_file(self, import_path):
        shutil.copyfile(import_path, self.save_path)
        self.start_screen.click(LEFT_BUTTON, LOAD_GAME_BUTTON_POS)
        self.start_screen.display()
        self.start_screen.click(LEFT_BUTTON, LOAD_FIRST_SLOT_BUTTON_POS)
        self.level = self.start_screen.level

    def simulate_trade_item(self, item, active_player, other_player,
                            is_active_the_sender):
        self.level.interact(active_player, other_player, ())
        self.level.execute_trade_action(TradeMenu.INTERAC_ITEM,
                                        ((0, 0), item,
                                         (active_player, other_player)))
        self.level.execute_item_action(
            ItemMenu.TRADE_ITEM,
            ((), (),
             (active_player, other_player, 0 if is_active_the_sender else 1)))
        self.level.execute_action((), (GenericActions.CLOSE, ()))
        self.level.execute_action((), (GenericActions.CLOSE, ()))

    def test_distance_between_two_entities(self):
        # Import simple save file
        self.import_save_file("tests/test_saves/simple_save.xml")

        players = self.level.players
        foe = self.level.entities['foes'][0]
        entities_with_dist = self.level.distance_between_all(foe, players)

        self.assertEqual(5, entities_with_dist[players[0]])

    def test_distance_between_many_entities(self):
        # Import complete save file
        self.import_save_file("tests/test_saves/complete_first_level_save.xml")

        players = self.level.players
        foes = self.level.entities['foes']

        raimund = None
        braern = None
        thokdrum = None
        for player in players:
            pos = (player.pos[0] // TILE_SIZE, player.pos[1] // TILE_SIZE)
            if pos == (17, 10):
                raimund = player
            elif pos == (16, 9):
                braern = player
            elif pos == (16, 10):
                thokdrum = player

        specific_skeleton = None
        specific_necrophage = None
        for f in foes:
            pos = (f.pos[0] // TILE_SIZE, f.pos[1] // TILE_SIZE)
            if pos == (12, 8):
                specific_skeleton = f
            if pos == (13, 5):
                specific_necrophage = f

        entities_with_dist = self.level.distance_between_all(
            specific_skeleton, players)
        self.assertEqual(12, entities_with_dist[raimund])
        self.assertEqual(4, entities_with_dist[braern])
        self.assertEqual(5, entities_with_dist[thokdrum])

        entities_with_dist = self.level.distance_between_all(
            specific_necrophage, players)
        self.assertEqual(8, entities_with_dist[raimund])
        self.assertEqual(6, entities_with_dist[braern])
        self.assertEqual(7, entities_with_dist[thokdrum])

    @unittest.skip
    def test_cancel_movement_after_trade_item_sent(self):
        # Import complete save file
        self.import_save_file("tests/test_saves/complete_first_level_save.xml")

        players = self.level.players

        active_player = [
            player for player in players if player.name == 'raimund'
        ][0]
        receiver_player = [
            player for player in players if player.name == 'braern'
        ][0]
        self.level.selected_player = active_player

        item = rd.choice(active_player.items)

        # Make trade (send item from active player to receiver player)
        self.simulate_trade_item(item, active_player, receiver_player, True)

        self.assertFalse(item in active_player.items)
        self.assertTrue(item in receiver_player.items)

        # Cancel active player turn
        self.level.right_click()

        self.assertTrue(item in active_player.items)
        self.assertFalse(item in receiver_player.items)

    @unittest.skip
    def test_cancel_movement_after_trade_item_received(self):
        # Import complete save file
        self.import_save_file("tests/test_saves/complete_first_level_save.xml")

        players = self.level.players

        active_player = [
            player for player in players if player.name == 'raimund'
        ][0]
        sender_player = [
            player for player in players if player.name == 'braern'
        ][0]
        self.level.selected_player = active_player

        item = rd.choice(sender_player.items)

        # Make trade (send item from active player to receiver player)
        self.simulate_trade_item(item, active_player, sender_player, False)

        self.assertTrue(item in active_player.items)
        self.assertFalse(item in sender_player.items)

        # Cancel active player turn
        self.level.right_click()

        self.assertFalse(item in active_player.items)
        self.assertTrue(item in sender_player.items)

    @unittest.skip
    def test_cancel_movement_after_trade_items_sent_and_received(self):
        # Import complete save file
        self.import_save_file("tests/test_saves/complete_first_level_save.xml")

        players = self.level.players

        active_player = [
            player for player in players if player.name == 'raimund'
        ][0]
        trade_partner_player = [
            player for player in players if player.name == 'braern'
        ][0]
        self.level.selected_player = active_player

        item = rd.choice(trade_partner_player.items)
        second_item = rd.choice(active_player.items)

        # Make trade (send item from active player to receiver player)
        self.simulate_trade_item(item, active_player, trade_partner_player,
                                 False)
        self.simulate_trade_item(second_item, active_player,
                                 trade_partner_player, True)

        self.assertTrue(item in active_player.items)
        self.assertFalse(item in trade_partner_player.items)
        self.assertFalse(second_item in active_player.items)
        self.assertTrue(second_item in trade_partner_player.items)

        # Cancel active player turn
        self.level.right_click()

        self.assertFalse(item in active_player.items)
        self.assertTrue(item in trade_partner_player.items)
        self.assertTrue(second_item in active_player.items)
        self.assertFalse(second_item in trade_partner_player.items)

    @unittest.skip
    def test_cancel_movement_after_trade_gold_sent(self):
        pass

    @unittest.skip
    def test_cancel_movement_after_trade_gold_received(self):
        pass

    @unittest.skip
    def test_cancel_movement_after_trade_items_and_gold_sent_and_received(
            self):
        pass
Exemplo n.º 2
0
    Level.init_constant_sprites()

    # Load some data
    races = Loader.load_races()
    classes = Loader.load_classes()
    Character.init_data(races, classes)

    clock = pg.time.Clock()

    start_screen = StartScreen(screen)

    quit_game = False
    while not quit_game:
        for e in pg.event.get():
            if e.type == pg.QUIT:
                quit_game = True
            elif e.type == pg.MOUSEMOTION:
                start_screen.motion(e.pos)
            elif e.type == pg.MOUSEBUTTONUP:
                if e.button == 1 or e.button == 3:
                    quit_game = start_screen.click(e.button, e.pos)
            elif e.type == pg.MOUSEBUTTONDOWN:
                if e.button == 1 or e.button == 3:
                    start_screen.button_down(e.button, e.pos)
        start_screen.update_state()
        start_screen.display()
        show_fps(screen, clock, fonts.fonts['FPS_FONT'])
        pg.display.update()
        clock.tick(60)
    raise SystemExit
class TestLevel(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        minimal_setup_for_game()
        cls.save_url = "saves/save_0.xml"

    def setUp(self):
        # Window parameters
        screen = pg.display.set_mode((MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT))
        self.start_screen = StartScreen(screen)
        self.start_screen.display()

    def test_distance_between_two_entities(self):
        print(self.start_screen.level)
        # Import simple save file
        shutil.copyfile("tests/test_saves/simple_save.xml", self.save_url)
        self.start_screen.click(LEFT_BUTTON, LOAD_GAME_BUTTON_POS)
        print(self.start_screen.level)
        self.start_screen.display()
        self.start_screen.click(LEFT_BUTTON, LOAD_FIRST_SLOT_BUTTON_POS)

        level = self.start_screen.level
        players = level.players
        foe = level.entities['foes'][0]
        entities_with_dist = level.distance_between_all(foe, players)

        self.assertEqual(5, entities_with_dist[players[0]])

    def test_distance_between_many_entities(self):
        # Import complete save file
        shutil.copyfile("tests/test_saves/complete_first_level_save.xml",
                        self.save_url)
        self.start_screen.click(LEFT_BUTTON, LOAD_GAME_BUTTON_POS)
        self.start_screen.display()
        self.start_screen.click(LEFT_BUTTON, LOAD_FIRST_SLOT_BUTTON_POS)

        level = self.start_screen.level
        players = level.players
        foes = level.entities['foes']

        raimund = None
        braern = None
        thokdrum = None
        for p in players:
            pos = (p.pos[0] // TILE_SIZE, p.pos[1] // TILE_SIZE)
            if pos == (17, 10):
                raimund = p
            elif pos == (16, 9):
                braern = p
            elif pos == (16, 10):
                thokdrum = p

        specific_skeleton = None
        specific_necrophage = None
        for f in foes:
            pos = (f.pos[0] // TILE_SIZE, f.pos[1] // TILE_SIZE)
            if pos == (12, 8):
                specific_skeleton = f
            if pos == (13, 5):
                specific_necrophage = f

        entities_with_dist = level.distance_between_all(
            specific_skeleton, players)
        self.assertEqual(12, entities_with_dist[raimund])
        self.assertEqual(4, entities_with_dist[braern])
        self.assertEqual(5, entities_with_dist[thokdrum])

        entities_with_dist = level.distance_between_all(
            specific_necrophage, players)
        self.assertEqual(8, entities_with_dist[raimund])
        self.assertEqual(6, entities_with_dist[braern])
        self.assertEqual(7, entities_with_dist[thokdrum])
class TestStartScreen(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestStartScreen, cls).setUpClass()
        pg.init()
        font.init_fonts()
        cls.save_url = "saves/main_save.xml"
        cls.level_class = Level
        cls.buttons = []
        cls.buttons.append(Rect(NEW_GAME_BUTTON_POS[0], NEW_GAME_BUTTON_POS[1], BUTTON_SIZE[0], BUTTON_SIZE[1]))
        cls.buttons.append(Rect(LOAD_GAME_BUTTON_POS[0], LOAD_GAME_BUTTON_POS[1], BUTTON_SIZE[0], BUTTON_SIZE[1]))
        cls.buttons.append(Rect(OPTIONS_BUTTON_POS[0], NEW_GAME_BUTTON_POS[1], BUTTON_SIZE[0], BUTTON_SIZE[1]))
        cls.buttons.append(Rect(EXIT_GAME_BUTTON_POS[0], NEW_GAME_BUTTON_POS[1], BUTTON_SIZE[0], BUTTON_SIZE[1]))
        races = Loader.load_races()
        classes = Loader.load_classes()
        Character.init_data(races, classes)

    @staticmethod
    def generate_position(start_pos, end_pos):
        # Generate random pos in a rect having start_pos as top left corner, and end_pos as bottom right corner
        pos = (randrange(start_pos[0], end_pos[0] + 1),
               randrange(start_pos[1], end_pos[1] + 1))
        # Print pos in case of test failure
        print("Generated position : " + str(pos))
        return pos

    def setUp(self):
        # Window parameters
        screen = pg.display.set_mode((MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT))
        self.start_screen = StartScreen(screen)
        self.start_screen.display()

    def test_no_game_at_launch(self):
        # Verify no game is launched
        self.assertIsNone(self.start_screen.level)
        self.assertIsNone(self.start_screen.level_id)

    def test_new_game(self):
        # Memorize old screen
        screen = self.start_screen.screen.copy()

        # Generate random pos on new game button
        pos = self.generate_position(NEW_GAME_BUTTON_POS, NEW_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(LEFT_BUTTON, pos)

        self.assertIsInstance(self.start_screen.level, self.level_class)
        self.assertEqual(self.start_screen.level_id, 0)
        self.assertNotEqual(self.start_screen.screen.get_rect(), screen.get_rect())

    def test_load_nonexistent_save(self):
        # Make a copy of the current window
        screen = self.start_screen.screen.copy()
        old_level = self.start_screen.level
        old_level_id = self.start_screen.level_id
        old_active_menu = self.start_screen.active_menu
        self.assertEqual(len(self.start_screen.background_menus), 0)

        # Erase save file if any
        if os.path.exists(self.save_url):
            os.remove(self.save_url)

        # Generate random pos on load game button
        pos = self.generate_position(LOAD_GAME_BUTTON_POS, LOAD_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(LEFT_BUTTON, pos)

        self.assertIsNone(self.start_screen.level)
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(len(self.start_screen.background_menus), 1)
        self.assertEqual(self.start_screen.background_menus[0][0], old_active_menu)
        self.assertNotEqual(self.start_screen.active_menu, old_active_menu)

    def test_load_existent_save(self):
        # Memorize old screen
        screen = self.start_screen.screen.copy()

        # Import simple save file
        shutil.copyfile("tests/test_saves/simple_save.xml", self.save_url)

        # Generate random pos on load game buttons
        pos = self.generate_position(LOAD_GAME_BUTTON_POS, LOAD_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(LEFT_BUTTON, pos)
        self.start_screen.display()
        pos = self.generate_position(LOAD_FIRST_SLOT_BUTTON_POS, LOAD_FIRST_SLOT_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(LEFT_BUTTON, pos)

        self.assertIsInstance(self.start_screen.level, self.level_class)
        self.assertEqual(self.start_screen.level_id, 0)
        self.assertNotEqual(self.start_screen.screen.get_rect(), screen.get_rect())

    def test_options_menu(self):
        # Make a copy of the current window
        old_active_menu = self.start_screen.active_menu

        # Generate random pos on options button
        pos = self.generate_position(OPTIONS_BUTTON_POS, OPTIONS_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(LEFT_BUTTON, pos)

        self.assertEqual(self.start_screen.background_menus[0][0], old_active_menu)
        self.assertNotEqual(self.start_screen.active_menu, old_active_menu)

    def test_exit_game(self):
        # Generate random pos on exit game button
        pos = self.generate_position(EXIT_GAME_BUTTON_POS, EXIT_GAME_BUTTON_POS + BUTTON_SIZE)
        self.assertEqual(self.start_screen.click(LEFT_BUTTON, pos), True)

    def test_click_on_nothing(self):
        # Make a copy of the current window
        screen = self.start_screen.screen.copy()
        old_level = self.start_screen.level
        old_level_id = self.start_screen.level_id
        old_active_menu = self.start_screen.active_menu

        # Generate random pos not on any existent buttons
        valid_pos = False
        pos = (0, 0)
        while not valid_pos:
            pos = (randrange(0, self.start_screen.screen.get_size()[0]),
                   randrange(0, self.start_screen.screen.get_size()[1]))
            valid_pos = True
            for button in self.buttons:
                if button.collidepoint(pos):
                    valid_pos = False
        # Print pos in case of a test failure
        print(pos)

        self.start_screen.click(LEFT_BUTTON, pos)

        # Verify state is unchanged
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(self.start_screen.active_menu, old_active_menu)

    #ToDo test esc key like below
    def test_right_click(self):
        # Make a copy of the current window
        screen = self.start_screen.screen.copy()
        old_level = self.start_screen.level
        old_level_id = self.start_screen.level_id
        old_active_menu = self.start_screen.active_menu

        # Generate random pos on new game button
        pos = self.generate_position(NEW_GAME_BUTTON_POS, NEW_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(RIGHT_BUTTON, pos)

        # Verify state is unchanged
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(self.start_screen.active_menu, old_active_menu)

        # Generate random pos on load game button
        pos = self.generate_position(LOAD_GAME_BUTTON_POS, LOAD_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(RIGHT_BUTTON, pos)

        # Verify state is unchanged
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(self.start_screen.active_menu, old_active_menu)

        # Generate random pos on options button
        pos = self.generate_position(OPTIONS_BUTTON_POS, OPTIONS_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(RIGHT_BUTTON, pos)

        # Verify state is unchanged
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(self.start_screen.active_menu, old_active_menu)

        # Generate random pos on exit game button
        pos = self.generate_position(EXIT_GAME_BUTTON_POS, EXIT_GAME_BUTTON_POS + BUTTON_SIZE)
        self.start_screen.click(RIGHT_BUTTON, pos)

        # Verify state is unchanged
        self.assertEqual(self.start_screen.screen.get_rect(), screen.get_rect())
        self.assertEqual(self.start_screen.level, old_level)
        self.assertEqual(self.start_screen.level_id, old_level_id)
        self.assertEqual(self.start_screen.active_menu, old_active_menu)