예제 #1
0
    def test_add_option(self):
        """Tests that Menu.add_option() adds a valid MenuOption to Menu.contents."""
        opt = MenuOption("Howdy!",
                         width=12,
                         height=5,
                         on_select=lambda x: print("Howdy!"),
                         subtext="It says howdy",
                         pad_horizontal=1,
                         pad_vertical=1,
                         color=(200, 255, 200))
        menu = Menu(20, 20)
        menu.add_option(opt)

        assert opt in menu.contents
예제 #2
0
    def test_clear(self):
        """Tests that Menu.clear() will remove all its MenuOptions from its contents."""
        opt = MenuOption("Howdy!",
                         width=12,
                         height=5,
                         on_select=lambda x: print("Howdy!"),
                         subtext="It says howdy",
                         pad_horizontal=1,
                         pad_vertical=1,
                         color=(200, 255, 200))
        menu = Menu(20, 20, contents=[opt])
        assert opt in menu.contents

        menu.clear()
        assert opt not in menu.contents
예제 #3
0
    def test_contents(self):
        """Tests getter for Menu.contents and that it rejects assignment."""
        opt = MenuOption("Howdy!",
                         width=12,
                         height=5,
                         on_select=lambda x: print("Howdy!"),
                         subtext="It says howdy",
                         pad_horizontal=1,
                         pad_vertical=1,
                         color=(200, 255, 200))
        menu = Menu(width=20, height=20, has_border=True, contents=[opt])

        assert opt in menu.contents

        with self.assertRaises(Exception):
            menu.contents = [opt]
예제 #4
0
    def test_color(self):
        """Tests getter and setter for color"""
        menu = Menu(20, 20, color=(125, 125, 125))
        assert menu.color == (125, 125, 125)

        menu.color = (200, 200, 200)
        assert menu.color == (200, 200, 200)

        with self.assertRaises(Exception):
            menu.color = None  # Can't be None
        with self.assertRaises(Exception):
            menu.color = (2, 14)  # Too few args
        with self.assertRaises(Exception):
            menu.color = (2, 154, 24, 99)  # Too many args
        with self.assertRaises(Exception):
            menu.color = (4, "beef", 99)  # One invalid arg
        with self.assertRaises(Exception):
            menu.color = (-4, 192, 94)  # One negative argument
예제 #5
0
def main():
    # Make the multiprocessing logic used in the modules happy.
    mp.freeze_support()

    # Load the tileset and context
    tileset = tcod.tileset.load_tilesheet("tilesets/yayo_c64_16x16.png", 16, 16,
                                          charmap=tcod.tileset.CHARMAP_CP437)
    context = tcod.context.new(width=WIDTH,
                               height=HEIGHT,
                               tileset=tileset,
                               sdl_window_flags=FLAGS)

    interface = Interface(context)

    # Mock-up of generating the main menu, which should be its own class, I think
    menu = Menu(width=floor(WIDTH/(2*TILESET_SIZE)),
                height=floor(HEIGHT/TILESET_SIZE),
                interface=interface,
                spacing=1)

    def launch_the_game(event):
        interface.new_playfield(width=200,
                                height=80)

        floors = []
        for y in range(0, 40):
            floors.append([(x, y, WalkableTerrain())
                           for x in range(0, 60)])

        for f in sum(floors, []):
            x, y, ent = f
            ent.introduce_at(x, y, interface.playfield)

        walls = [(x, 20, Wall()) for x in range(0, 11)]
        for w in walls:
            x, y, ent = w
            ent.introduce_at(x, y, interface.playfield)

        player_char = Mobile(size=4,
                             sigil=Sigil("@", priority=3),
                             name="Player Character")
        player_char.introduce_at(10, 10, interface.playfield)
        interface.playfield.player_character = player_char

        interface.playfield.origin = (2, 2)
        interface.add_animation(7, 7, Animation(frames=[AnimationFrame(Sigil("\\"), 5),
                                                        AnimationFrame(Sigil("|"), 5),
                                                        AnimationFrame(Sigil("/"), 5),
                                                        AnimationFrame(Sigil("-"), 5)],
                                                repeating=True))
        blue_floor = WalkableTerrain(color=(50, 50, 255))
        blue_floor.sigil = Sigil("!", priority=blue_floor.sigil.priority)
        blue_floor.introduce_at(12, 12, interface.playfield)

        # print(interface.playfield.get_cell(12, 12).sigils)
        interface.new_game_log(height=10, width=40)
        interface.print_to_log("This is a log tests!", color=(25, 250, 25))
        interface.print_to_log("This is an exceptionally long log tests so we can see how well it handles multiples of these.")
        interface.print_to_log("This is another line.")
        interface.print_to_log("This is yet another line.")
        interface.print_to_log("This should bump the first line off of the screen.")
        menu.close_menu()

    menu.add_option(MenuOption(text="Start Game",
                               width=24, height=5,
                               on_select=launch_the_game))
    menu.add_option(MenuOption(text="Do Nothing",
                               width=24, height=5,
                               on_select=lambda x: print("Did nothing!")))
    menu.add_option(MenuOption(text="Do Nothing Else",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Do More Nothing",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Make this menu long",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Like REALLY long",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Because we, ah...",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Need to tests something",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="You know, uh...",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="that one thing...",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Oh yeah!",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    menu.add_option(MenuOption(text="Overflow behavior! :)",
                               width=24, height=5,
                               on_select=lambda x: print("Did more nothing!")))
    interface.open_menu(menu)

    last_tick = __time_ms()  # In epoch milliseconds
    tick_length = 50         # Milliseconds per engine tick
    while True:
        now_ms = __time_ms()

        # Only run the main loop logic if we've reached the next clock increment
        if now_ms - last_tick >= tick_length:
            if interface.playfield:
                win_width, win_height = context.recommended_console_size(min_columns=50,
                                                                         min_rows=40)
                interface.playfield.window = (win_width - READOUT_WIDTH,
                                              win_height - GAMELOG_HEIGHT)
            interface.tick()
            interface.print_self()

            # Forward the reference time to the time at which we started this batch of logic
            last_tick = now_ms
예제 #6
0
 def change_menu(self, menu: Menu, **kwargs):
     if menu.on_enter(**kwargs):
         self.current_menu.on_leave()
         self.current_menu = menu
         self.menu_history.append(menu)
예제 #7
0
    def test_padding(self):
        """Tests getters for each direction of padding."""
        menu = Menu(20, 20, padding=(2, 1, 3, 4))

        assert menu.pad_top == 2
        assert menu.pad_right == 1
        assert menu.pad_bottom == 3
        assert menu.pad_left == 4

        menu.padding = (2, 2, 1, 2)

        assert menu.pad_top == 2
        assert menu.pad_right == 2
        assert menu.pad_bottom == 1
        assert menu.pad_left == 2

        # Cannot directly assign new padding to their respective properties
        with self.assertRaises(Exception):
            menu.pad_top = 2
        with self.assertRaises(Exception):
            menu.pad_right = 2
        with self.assertRaises(Exception):
            menu.pad_bottom = 2
        with self.assertRaises(Exception):
            menu.pad_left = 2

        with self.assertRaises(Exception):
            menu.padding = (2, 1, 0)  # Too few params
        with self.assertRaises(Exception):
            menu.padding = (2, 5, 1, 2, 4)  # Too many params
        with self.assertRaises(Exception):
            menu.padding = (3, 5, 2, -4)  # Negative parameter
        with self.assertRaises(Exception):
            menu.padding = ("pipe", 4, 2, 1)  # Invalid parameter type