Example #1
0
def __move_units():
    """Moves CPU units."""

    if len(globals_.units_to_move) > 0:
        for un in globals_.units_to_move:
            if un[2] == UP and un[0].rect.y > un[3] - un[1]:
                if un[0].rect.y - un[0].speed >= un[3] - un[1]:
                    un[0].move(UP)
                else:
                    if scripting._possible_to_move(un[0], un[0].speed, UP):
                        un[0].move(UP)
            elif un[2] == DOWN and un[0].rect.y + un[0].rect.height \
            < un[0].rect.height + un[3] + un[1]:
                if un[0].rect.y + un[0].rect.height + un[0].speed \
                <= un[0].rect.height + un[3] + un[1]:
                    un[0].move(DOWN)
                else:
                    if scripting._possible_to_move(un[0], un[0].speed, DOWN):
                        un[0].move(DOWN)
            elif un[2] == LEFT and un[0].rect.x > un[3] - un[1]:
                if un[0].rect.x - un[0].speed >= un[3] - un[1]:
                    un[0].move(LEFT)
                else:
                    if scripting._possible_to_move(un[0], un[0].speed, LEFT):
                        un[0].move(LEFT)
            elif un[2] == RIGHT and un[0].rect.x + un[0].rect.width \
            < un[0].rect.width + un[3] + un[1]:
                if un[0].rect.x + un[0].rect.width + un[0].speed <= \
                un[0].rect.width + un[3] + un[1]:
                    un[0].move(RIGHT)
                else:
                    if scripting._possible_to_move(un[0], un[0].speed, RIGHT):
                        un[0].move(RIGHT)
            else:
                un[0].stop()
                globals_.units_to_move.remove(un)
Example #2
0
def normal_mode_events(event):
    """Manages events for the normal mode.

    'event' parameter is a pygame event type.
    """

    # Manages events for when a key is pressed.
    if event.type == KEYDOWN:
        unit = globals_.player_unit

        # Events for the arrow up key
        if event.key == K_UP:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, UP):
                # Enters a building if possible.
                if scripting._can_enter(unit):
                    map_ = globals_.current_map

                    number = scripting._can_enter(unit)
                    map_.interior = number

                    unit.rect.x = map_.areas[number - 1][0].x + \
                    int(map_.areas[number - 1][0].width // 2) - \
                    int(unit.rect.width // 2)

                    unit.rect.y = map_.areas[number - 1][0].y - \
                    unit.rect.height

                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone, map_.interior)
                else:
                    unit.move(UP)
            else:
                unit.turn(UP)

        # Events for the arrow down key
        elif event.key == K_DOWN:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, DOWN):
                # Enters a building if possible.
                if scripting._can_enter(unit):
                    map_ = globals_.current_map
                    current_interior = map_.interior
                    map_.interior = 0

                    unit.rect.x = map_.areas[current_interior - 1][0].x + \
                    int(map_.areas[current_interior - 1][0].width // 2) - \
                    int(unit.rect.width // 2)

                    unit.rect.y = map_.areas[current_interior - 1][0].y + \
                    unit.rect.height

                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
                else:
                    unit.move(DOWN)
            else:
                unit.turn(DOWN)

        # Events for the arrow left key
        elif event.key == K_LEFT:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, LEFT):
                unit.move(LEFT)
            else:
                unit.turn(LEFT)

        # Events for the arrow right key
        elif event.key == K_RIGHT:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, RIGHT):
                unit.move(RIGHT)
            else:
                unit.turn(RIGHT)

        # Events for the Escape key
        elif event.key == K_ESCAPE:
            pygame.key.set_repeat(0, 0)

            buttons = [
                menus.Button("ingame_button.bmp",
                             text="Resume",
                             action="actions.resume_game()"),
                menus.Button("ingame_button.bmp", text="Load"),
                menus.Button("ingame_button.bmp", text="Options"),
                menus.Button("ingame_button.bmp",
                             text="Exit",
                             action="actions.exit_to_menu()")
            ]

            globals_.current_menu[0] = INGAME_MENU
            globals_.current_menu[1] = menus.InGameMenu(
                "ingame_menu.png", buttons)
            globals_.fps = SLOW_FPS
            globals_.current_mode = MENU_MODE

        elif event.key == K_SPACE:
            if not scripting._can_pick_up() is None:
                item = scripting._can_pick_up()

                scripting.remove_item(item)
                globals_.player_unit.inventory.add(item)

        #TEST
        elif event.key == K_e:

            globals_.current_window[0] = FIGHT_WINDOW
            globals_.current_window[1] = windows.ActionDialog()

            globals_.current_opponent.health = MAX_HEALTH

            globals_.current_mode = FIGHTING_MODE
            pygame.key.set_repeat(0, 0)

        #TEST
        elif event.key == K_d:

            globals_.current_panel[0] = DIALOG_PANEL
            globals_.current_panel[1] = panels.DialogPanel(
                globals_.player_unit)

            globals_.fps = SLOW_FPS
            globals_.current_mode = PANEL_MODE

        # Events for the 'i' key
        elif event.key == K_i:
            globals_.current_window[0] = INVENTORY
            globals_.current_window[1] = windows.Inventory()
            # END OF TEST

            pygame.key.set_repeat(0, 0)
            globals_.fps = SLOW_FPS
            globals_.current_mode = WINDOW_MODE

        map_ = globals_.current_map  # Game's current map
        unit = globals_.player_unit  # Player's current unit

        # Switches to a next zone if possible.
        if unit.rect.x + unit.rect.width + 16 > 1024:
            if map_.zones == 4:
                if map_.zone in (1, 3):
                    map_.zone += 1
                    unit.rect.x = unit.speed + 16
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.x - 16 < 0:
            if map_.zones == 4:
                if map_.zone in (2, 4):
                    map_.zone -= 1
                    unit.rect.x = 1024 - (unit.speed + unit.rect.width + 16)
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.y + unit.rect.height + 16 > 768:
            if map_.zones == 4:
                if map_.zone in (1, 2):
                    map_.zone += 2
                    unit.rect.y = unit.speed + 16
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.y - 16 < 0:
            if map_.zones == 4:
                if map_.zone in (3, 4):
                    map_.zone -= 2
                    unit.rect.y = 768 - (unit.speed + unit.rect.height + 16)
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)

    # Manages events for when a key is released.
    elif event.type == KEYUP:
        unit = globals_.player_unit
        unit.stop()
def normal_mode_events(event):
    """Manages events for the normal mode.

    'event' parameter is a pygame event type.
    """

    # Manages events for when a key is pressed.
    if event.type == KEYDOWN:
        unit = globals_.player_unit

        # Events for the arrow up key
        if event.key == K_UP:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, UP):
                # Enters a building if possible.
                if scripting._can_enter(unit):
                    map_ = globals_.current_map

                    number = scripting._can_enter(unit)
                    map_.interior = number

                    unit.rect.x = map_.areas[number - 1][0].x + \
                    int(map_.areas[number - 1][0].width // 2) - \
                    int(unit.rect.width // 2)

                    unit.rect.y = map_.areas[number - 1][0].y - \
                    unit.rect.height

                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone, map_.interior)
                else:
                    unit.move(UP)
            else:
                unit.turn(UP)

        # Events for the arrow down key
        elif event.key == K_DOWN:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, DOWN):
                # Enters a building if possible.
                if scripting._can_enter(unit):
                    map_ = globals_.current_map
                    current_interior = map_.interior
                    map_.interior = 0

                    unit.rect.x = map_.areas[current_interior - 1][0].x + \
                    int(map_.areas[current_interior - 1][0].width // 2) - \
                    int(unit.rect.width // 2)

                    unit.rect.y = map_.areas[current_interior - 1][0].y + \
                    unit.rect.height

                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
                else:
                    unit.move(DOWN)
            else:
                unit.turn(DOWN)

        # Events for the arrow left key
        elif event.key == K_LEFT:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, LEFT):
                unit.move(LEFT)
            else:
                unit.turn(LEFT)

        # Events for the arrow right key
        elif event.key == K_RIGHT:
            # Moves player's unit if possible.
            if scripting._possible_to_move(unit, unit.speed, RIGHT):
                unit.move(RIGHT)
            else:
                unit.turn(RIGHT)

        # Events for the Escape key
        elif event.key == K_ESCAPE:
            pygame.key.set_repeat(0, 0)

            buttons = [menus.Button("ingame_button.bmp", text="Resume",
                                    action="actions.resume_game()"),
                       menus.Button("ingame_button.bmp", text="Load"),
                       menus.Button("ingame_button.bmp", text="Options"),
                       menus.Button("ingame_button.bmp", text="Exit",
                                    action="actions.exit_to_menu()")]

            globals_.current_menu[0] = INGAME_MENU
            globals_.current_menu[1] = menus.InGameMenu("ingame_menu.png",
                                                        buttons)
            globals_.fps = SLOW_FPS
            globals_.current_mode = MENU_MODE

        elif event.key == K_SPACE:
            if not scripting._can_pick_up() is None:
                item = scripting._can_pick_up()

                scripting.remove_item(item)
                globals_.player_unit.inventory.add(item)

        #TEST
        elif event.key == K_e:

            globals_.current_window[0] = FIGHT_WINDOW
            globals_.current_window[1] = windows.ActionDialog()

            globals_.current_opponent.health = MAX_HEALTH

            globals_.current_mode = FIGHTING_MODE
            pygame.key.set_repeat(0, 0)

        #TEST
        elif event.key == K_d:

            globals_.current_panel[0] = DIALOG_PANEL
            globals_.current_panel[1] = panels.DialogPanel(globals_.player_unit)

            globals_.fps = SLOW_FPS
            globals_.current_mode = PANEL_MODE

        # Events for the 'i' key
        elif event.key == K_i:
            globals_.current_window[0] = INVENTORY
            globals_.current_window[1] = windows.Inventory()
            # END OF TEST

            pygame.key.set_repeat(0, 0)
            globals_.fps = SLOW_FPS
            globals_.current_mode = WINDOW_MODE

        map_ = globals_.current_map     # Game's current map
        unit = globals_.player_unit     # Player's current unit

        # Switches to a next zone if possible.
        if unit.rect.x + unit.rect.width + 16 > 1024:
            if map_.zones == 4:
                if map_.zone in (1, 3):
                    map_.zone += 1
                    unit.rect.x = unit.speed + 16
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.x - 16 < 0:
            if map_.zones == 4:
                if map_.zone in (2, 4):
                    map_.zone -= 1
                    unit.rect.x = 1024 - (unit.speed + unit.rect.width + 16)
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.y + unit.rect.height + 16 > 768:
            if map_.zones == 4:
                if map_.zone in (1, 2):
                    map_.zone += 2
                    unit.rect.y = unit.speed + 16
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)
        elif unit.rect.y - 16 < 0:
            if map_.zones == 4:
                if map_.zone in (3, 4):
                    map_.zone -= 2
                    unit.rect.y = 768 - (unit.speed + unit.rect.height + 16)
                    globals_.current_bg_sound = ["", True]
                    initialization.update_quest(map_.zone)

    # Manages events for when a key is released.
    elif event.type == KEYUP:
        unit = globals_.player_unit
        unit.stop()