Beispiel #1
0
class Game:

    """Class that consists of  main, character, options and play menu and
    implements the communication between them.

    """

    def __init__(self):
        """Initialize a pygame.display and a MainMenu member variables."""
        pygame.init()

        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        self.status = MAIN_MENU

    def run_main_menu(self):
        """Call the main method of main_menu.

        If the instance has no 'main_menu' attribute or it is not properly
        initialized a new one is instantiated.

        Game.loop_main_menu(): return:
            EXIT if the user(s) quitted the game
            CHARACTER_MENU if the user(s) pressed the Play button
            OPTIONS_MENU if the user(s) pressed the Options button

        """
        if not hasattr(self, "main_menu"):
            self.main_menu = MainMenu()
        else:
            self.main_menu.reinit()

        return self.main_menu.main(self.screen)

    def run_character_menu(self):
        """Call the main method of character_menu.

        If the instance has no 'character_menu' attribute or it is not properly
        initialized a new one is instantiated.

        Game.loop_character_menu(): return:
            EXIT if the user(s) quitted the game
            MAIN_MENU if the user(s) when back to the main menu
            PLAY_MENU if the user(s) properly selected their
            characters and advanced to play the game

        """
        if not hasattr(self, "character_menu"):
            self.character_menu = CharacterMenu()
        else:
            self.character_menu.reinit()

        return self.character_menu.main(self.screen)

    def run_options_menu(self):
        """Call the main method of options_menu.

        If the instance has no 'options_menu' attribute or it is not properly
        initialized a new one is instantiated.

        Game.loop_options_menu(): return:
            EXIT if the user(s) quitted the game
            MAIN_MENU if the user(s) when back to the main menu

        """
        if not hasattr(self, "options_menu"):
            self.options_menu = OptionsMenu()

        return self.options_menu.main(self.screen)

    def run_play_menu(self):
        """Call the main method of play_menu.

        If the instance has 'play_menu' it is deleted and a new one is
        instantiated.

        Game.loop_play_menu(): return:
            EXIT if the display has been closed
            CHARACTER_MENU if the round has ended (a player died or the timer
            reached ROUND_TIME)

        """
        if hasattr(self, "play_menu"):
            del self.play_menu
        self.play_menu = PlayMenu(self.character_menu.get_characters())

        return self.play_menu.main(self.screen)

    def play(self):
        """Make the menus communicated with each other until an EXIT value
        is returned by the main method of a menu. The returned values are
        held in the status attribute.

        Game.play(): return None

        """
        while self.status != EXIT:
            if self.status == MAIN_MENU:
                self.status = self.run_main_menu()
            elif self.status == CHARACTER_MENU:
                self.status = self.run_character_menu()
            elif self.status == OPTIONS_MENU:
                self.status = self.run_options_menu()
            elif self.status == PLAY_MENU:
                self.status = self.run_play_menu()

        pygame.quit()
Beispiel #2
0
class MainMenuTest(unittest.TestCase):
    def setUp(self):
        # pygame.display.set_mode()

        self.main_menu = MainMenu()
        self.surface = pygame.Surface((0, 0))
        self.event_quit = pygame.event.Event(pygame.QUIT)
        self.event_enter1 = pygame.event.Event(pygame.KEYDOWN,
                                               {'key': MAIN_CONTROLS[0]})
        self.event_enter2 = pygame.event.Event(pygame.KEYDOWN,
                                               {'key': MAIN_CONTROLS[1]})
        self.event_escape = pygame.event.Event(pygame.KEYDOWN,
                                               {'key': MAIN_CONTROLS[2]})
        self.event_up1 = pygame.event.Event(pygame.KEYDOWN,
                                            {'key': P1_CONTROLS[2]})
        self.event_up2 = pygame.event.Event(pygame.KEYDOWN,
                                            {'key': P2_CONTROLS[2]})
        self.event_down1 = pygame.event.Event(pygame.KEYDOWN,
                                              {'key': P1_CONTROLS[3]})
        self.event_down2 = pygame.event.Event(pygame.KEYDOWN,
                                              {'key': P2_CONTROLS[3]})

    def tearDown(self):
        # pygame.display.quit()

        del self.main_menu
        del self.surface
        del self.event_quit
        del self.event_escape
        del self.event_enter1
        del self.event_enter2
        del self.event_up1
        del self.event_up2
        del self.event_down1
        del self.event_down2

    def test_sound(self):
        pygame.event.post(self.event_quit)
        self.main_menu.main(self.surface)
        self.assertFalse(pygame.mixer.get_busy())

    def test_reinit(self):
        pygame.event.post(self.event_up1)
        pygame.event.post(self.event_enter2)
        self.main_menu.main(self.surface)
        self.assertFalse(self.main_menu.pointer == 0)
        self.assertIsNot(self.main_menu.get_pressed_button(), None)

        self.main_menu.reinit()
        self.assertTrue(self.main_menu.pointer == 0)
        self.assertIs(self.main_menu.get_pressed_button(), None)

    def test_update(self):
        self.main_menu.pointer = 182732
        self.main_menu.update()

        self.assertTrue(self.main_menu.pointer >= 0 and
                        self.main_menu.pointer < len(self.main_menu.buttons))
        self.assertTrue(self.main_menu.buttons[self.main_menu.pointer].
                        is_flagged)
        for index in range(len(self.main_menu.buttons)):
            if index != self.main_menu.pointer:
                self.assertFalse(self.main_menu.buttons[index].is_flagged)

    def test_main(self):
        pygame.event.post(self.event_quit)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, EXIT)

        self.main_menu.reinit()
        pygame.event.post(self.event_escape)
        resutl = self.main_menu.main(self.surface)
        self.assertEqual(resutl, EXIT)

        self.main_menu.reinit()
        pygame.event.post(self.event_enter1)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, CHARACTER_MENU)

        self.main_menu.reinit()
        pygame.event.post(self.event_enter2)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, CHARACTER_MENU)

        self.main_menu.reinit()
        pygame.event.post(self.event_down1)
        pygame.event.post(self.event_enter1)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, OPTIONS_MENU)

        self.main_menu.reinit()
        pygame.event.post(self.event_up2)
        pygame.event.post(self.event_up1)
        pygame.event.post(self.event_enter2)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, OPTIONS_MENU)

        self.main_menu.reinit()
        pygame.event.post(self.event_up1)
        pygame.event.post(self.event_enter1)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, EXIT)

        self.main_menu.reinit()
        pygame.event.post(self.event_down1)
        pygame.event.post(self.event_down2)
        pygame.event.post(self.event_enter2)
        result = self.main_menu.main(self.surface)
        self.assertEqual(result, EXIT)