Example #1
0
def ingame(settings, running, win, again):
    """
    loops that are active in game

    settings loop allowing for visual changes

    game loop allowing game to be played

    :param settings: bool; settings loop is running
    :param running: bool; game loop is running
    :param win: int; player that is currently winning
    :param again: bool; game is datting from beginning after current game ends
    :return: int, bool; updated bool for loop management
    """

    vo.refresh_loc_coords(
        vo.get_window_size(vo.get_screen_size()[0],
                           vo.get_screen_size()[1])[2],
        vo.get_window_size(vo.get_screen_size()[0],
                           vo.get_screen_size()[1])[3], lg.get_zustand())
    # displays the menu on the game window
    vo.draw_screen(lg.get_zustand(), lg.get_background(), lg.get_language(),
                   sh.get_ship(), resource_path, lg.get_task_number())

    while settings or running:
        # ------
        # settings loop
        settings, running, win, again = setting(settings, running, win, again)

        # ------
        # game loop
        settings, running, win, again = game_loop(settings, running, win,
                                                  again)

    return win, again
Example #2
0
def video_resize(size, ship=[[[0]]], draw=True):
    vo.set_screen_size(size[0], size[1])  # refreshes window size
    # refreshes location of visible assets and the game itself
    vo.refresh_loc_coords(
        vo.get_window_size(size[0], size[1])[2],
        vo.get_window_size(size[0], size[1])[3], lg.get_zustand())
    if draw:
        # displays the menu on the game window
        vo.draw_screen(lg.get_zustand(), lg.get_background(),
                       lg.get_language(), ship, resource_path,
                       lg.get_task_number(), lg.get_settings())
Example #3
0
def set_player(play):
    """
    refreshes current palyer and executes enemy's move

    :param play: int; which player has to complete a move next (0 player, 1 enemy)
    """
    global player
    global move
    player = play  # refreshes current player
    if player == 1:  # checks whether this is the enemy's move
        move[get_dif_number()] += 1
        # displays the game on the game window
        vo.draw_screen(lg.get_zustand(), lg.get_background(),
                       lg.get_language(), sh.get_ship(), resource_path,
                       lg.get_task_number())
        time.sleep(0.75)  # waits 2 seconds to simulate the enemy thinking
        _do_enemy_move()  # executes enemy's move
        # displays the game on the game window
        vo.draw_screen(lg.get_zustand(), lg.get_background(),
                       lg.get_language(), sh.get_ship(), resource_path,
                       lg.get_task_number())
    player = 0  # sets the player back to the player
Example #4
0
def game_loop(settings, running, win, again):
    """
    game loop, allowing for game to be played

    :param settings: bool; settings loop will be running
    :param running: bool; game is running
    :param win: int; player that is currently winning
    :param again: bool; game will be starting again after this game
    :return: bool, bool, int, bool; bools for loop management
    """
    def set_settings():
        """
        activates settings loop allowing for visual changes to be made by the user
        """
        # updates bools to start settings loop
        nonlocal settings
        nonlocal running
        running = False
        settings = True
        lg.set_aufgabe("main")  # opens main page in menu
        lg.set_zustand("settings")  # sets current loop to current loop

    while running:  # game loop allowing the game to be played

        for event in pg.event.get(
        ):  # goes through every input occuring while game window is in focus

            if event.type == MOUSEBUTTONDOWN:  # button on mouse was pressed
                # gets the size of one virtual field
                field_size = vo.get_window_size(vo.get_screen_size()[0],
                                                vo.get_screen_size()[1])[2]
                button = lg.do_button_mouse_ingame(event.pos[0], event.pos[1],
                                                   field_size,
                                                   vo.get_buttons()[1], gaf(),
                                                   resource_path, save_game)
                try:
                    # gets coordiantes of the clicked field, whether a field was hit, and the hit field
                    xcoord, ycoord, hit_check, xcoord_2, ycoord_2 = button
                except ValueError:
                    # instead puts control values for hit fields if none were hit
                    xcoord, ycoord, hit_check, xcoord_2, ycoord_2 = (-1, -1,
                                                                     False, -1,
                                                                     -1)
                    if button == "end":
                        settings = running = False
                        win = 1
                    elif button == "settings":
                        set_settings()
                except TypeError:
                    # instead puts control values for hit fields if none were hit
                    xcoord, ycoord, hit_check, xcoord_2, ycoord_2 = (-1, -1,
                                                                     False, -1,
                                                                     -1)
                if hit_check:
                    # executes hit if a field was hit
                    do_hit_player_input(xcoord_2, ycoord_2)
                if -1 not in pl.get_last_click():  # checks for control value
                    vo.unclick(pl.get_last_click()
                               )  # unclicks previously clicked field
                pl.set_angeklicktes_feld(xcoord,
                                         ycoord)  # refreshes clicked field
                # displays the game on the game window
                vo.draw_screen(lg.get_zustand(), lg.get_background(),
                               lg.get_language(), sh.get_ship(), resource_path,
                               lg.get_task_number(), lg.get_settings())
                if running:
                    running, win = sh.check_ship_status(
                    )  # checks whether all ships of one player have been destroyed

            elif event.type == KEYDOWN:  # button on keyboard was pressed
                if event.key == K_ESCAPE:  # escape button was pressed
                    set_settings()
                else:
                    try:
                        # gets coordinates of the selected field
                        xcoord, ycoord = lg.do_button_ingame_keyboard(
                            event.key, resource_path)
                    except TypeError:
                        # sets coordinates to -1 when no field is selected yet
                        xcoord, ycoord = -1, -1

                    if xcoord != -1 and ycoord != -1:
                        # clicks a new field
                        hit_small_field(1, xcoord, ycoord, resource_path,
                                        lg.get_sound_volume(),
                                        lg.get_language())
                        do_hit_player_input(
                            xcoord, ycoord)  # hits a field from a player input

                # displays the game on the game window
                vo.draw_screen(lg.get_zustand(), lg.get_background(),
                               lg.get_language(), sh.get_ship(), resource_path,
                               lg.get_task_number(), lg.get_settings())
                running, win = sh.check_ship_status(
                )  # checks whether all ships of one player have been destroyed

            elif event.type == VIDEORESIZE:  # checks whether the size of the window got adjusted
                video_resize(event.size, sh.get_ship())

            elif event.type == pg.QUIT:  # checks whether the wndow has been closed
                # ends the program
                settings = running = again = False
                win = 2  # sets win to 2, what is used to mark that no player has won the game

    return settings, running, win, again
Example #5
0
def setting(settings, running, win, again):
    """
    settings loop in game, allowing for settings to change that only effect the game visually

    :param settings: bool; settings loop is running
    :param running: bool; game loop will be running
    :param win: int; player that is currently winning
    :param again: bool; game will be starting again after this game
    :return: bool, bool, int, bool; updated bools for loop management
    """
    num = 0
    other_num = 0
    while settings and lg.get_zustand(
    ) != "end":  # settings loop enabling setting changes in game

        for event in pg.event.get(
        ):  # goes through every input occuring while game window is in focus

            if event.type == MOUSEBUTTONDOWN:  # mouse button was clicked
                # gets size of one virtual field
                field_size, orientation = vo.get_window_size(
                    vo.get_screen_size()[0],
                    vo.get_screen_size()[1])[2:]
                # executes feature of pressed button if there is one
                button = lg.do_button_start(event.pos[0], event.pos[1],
                                            field_size,
                                            vo.get_buttons()[0], resource_path)
                if button == "end":
                    settings = running = False
                    win = 1
                if lg.get_aufgabe(
                ) == "volume":  # checks whether a slider is on the screen
                    for slider in vo.get_slides():  # goes through every slider
                        try:
                            if slider.button_rect.collidepoint(
                                    event.pos
                            ):  # checks whether the slider is pressed
                                slider.hit = True  # sets hit to true, so that it will be moved
                        except AttributeError:
                            pass
                # displays the menu on the game window
                vo.draw_screen(lg.get_zustand(), lg.get_background(),
                               lg.get_language(), sh.get_ship(), resource_path,
                               lg.get_task_number(), lg.get_settings())
                other_num = num  # refreshes control number used to fix bug with sliders

            elif event.type == MOUSEBUTTONUP and num != other_num + 1:  # checks whether a mouse button was lifted
                for slider in vo.get_slides():  # goes through every slider
                    slider.hit = False  # sets hit to false, so that it won't be moved
                # displays the menu on the game window
                vo.draw_screen(lg.get_zustand(), lg.get_background(),
                               lg.get_language(), sh.get_ship(), resource_path,
                               lg.get_task_number(), lg.get_settings())

            elif event.type == VIDEORESIZE:  # window size has been adjusted
                video_resize(event.size, sh.get_ship())

            elif event.type == pg.QUIT:  # ueberprueft, ob das Fenster geschlossen wurde
                # ends the program
                settings = running = again = False
                win = 2

        if lg.get_aufgabe(
        ) == "volume":  # checks whether a slider is on the screen
            for slider in vo.get_slides():  # goes through every slider
                if slider.hit:  # checks, whether it is hit
                    # moves the slider and thus changes the value of it
                    slider.move(
                        vo.get_window_size(vo.get_screen_size()[0],
                                           vo.get_screen_size()[1])[2])
                    # displays the volume settings on the game window
                    vo.draw_screen(lg.get_zustand(), lg.get_background(),
                                   lg.get_language(), sh.get_ship(),
                                   resource_path, lg.get_task_number(),
                                   lg.get_settings())

        elif lg.get_aufgabe() == "stats":
            update_statistics()
            # displays the statistics on the game window
            vo.draw_screen(lg.get_zustand(), lg.get_background(),
                           lg.get_language(), sh.get_ship(), resource_path,
                           lg.get_task_number(), lg.get_settings())
            lg.set_aufgabe("statistics")

        if lg.get_zustand() == "ingame":  # game has been started
            settings = False  # continues the game
            running = True
        # continues counting, used to fix a bug instantly having a mousebuttonup event after mousebuttondown
        num += 1
        if num > 254:
            num = 0

        try:
            music.set_volume(lg.get_music_volume()
                             )  # adjusts the volume of the music playing
        except NameError:  # music has not been found
            pass

    return settings, running, win, again
Example #6
0
def begins(begin, request, running, settings, win, again):
    """
    loop running at the beginning of the game

    shows a menu,
    settings can be changed that impact the next game and are saved for future games

    :param begin: bool; setings loop is running
    :param request: bool; request loop will run
    :param running: bool; game loop will run
    :param settings: bool; settings loop in game will run
    :param win: int; current winner of the game
    :param again: bool; game is repeated from the begin loop to allow for another game to b played
    :return: bool, bool, bool, int, bool; updated bools for loop management
    """
    global start_time
    # displays the menu on the game window
    vo.refresh_loc_coords(
        vo.get_window_size(vo.get_screen_size()[0],
                           vo.get_screen_size()[1])[2],
        vo.get_window_size(vo.get_screen_size()[0],
                           vo.get_screen_size()[1])[3], lg.get_zustand())
    vo.draw_screen(lg.get_zustand(), lg.get_background(), lg.get_language(),
                   [[[0]]], resource_path, lg.get_task_number(),
                   lg.get_settings())
    num = 0
    other_num = 0
    while begin and lg.get_zustand(
    ) != "end":  # begin loop is running and settings can be changed

        for event in pg.event.get(
        ):  # goes through every input occuring while game window is in focus

            if event.type == MOUSEBUTTONDOWN:  # mouse button was clicked
                # gets size of one field and orientation
                field_size, orientation = vo.get_window_size(
                    vo.get_screen_size()[0],
                    vo.get_screen_size()[1])[2:]
                # executes feature of pressed button if there is one
                button = lg.do_button_start(event.pos[0], event.pos[1],
                                            field_size,
                                            vo.get_buttons()[0], resource_path)
                if button == "end":
                    begin = settings = running = request = again = False
                # displays the menu on the game window
                if lg.get_zustand() != "ingame":
                    vo.draw_screen(lg.get_zustand(), lg.get_background(),
                                   lg.get_language(), [[[0]]], resource_path,
                                   lg.get_task_number(), lg.get_settings())
                if lg.get_aufgabe(
                ) == "volume":  # checks whether a slider is on the screen
                    for slider in vo.get_slides():  # goes through every slider
                        try:
                            if slider.button_rect.collidepoint(
                                    event.pos
                            ):  # checks whether the slider is pressed
                                slider.hit = True  # sets hit to true, so that it will be moved
                        except AttributeError:
                            pass
                    other_num = num  # refreshes control number used to fix bug with sliders

            elif event.type == MOUSEBUTTONUP and num != other_num + 1:  # checks whether a mouse button was lifted
                for slider in vo.get_slides():  # goes through every slider
                    slider.hit = False  # sets hit to false, so that it won't be moved
                # displays the menu on the game window
                vo.draw_screen(lg.get_zustand(), lg.get_background(),
                               lg.get_language(), [[[0]]], resource_path,
                               lg.get_task_number(), lg.get_settings())

            elif event.type == VIDEORESIZE:  # window size has been adjusted
                video_resize(event.size)

            elif event.type == pg.QUIT:  # window was closed
                # ends the program
                running = begin = settings = request = again = False
                win = 2

        if lg.get_aufgabe() == "stats":
            update_statistics()
            # displays the statistics on the game window
            vo.draw_screen(lg.get_zustand(), lg.get_background(),
                           lg.get_language(), [[[0]]], resource_path,
                           lg.get_task_number(), lg.get_settings())
            lg.set_aufgabe("statistics")

        elif lg.get_aufgabe(
        ) == "volume":  # checks whether a slider is on the screen
            for slider in vo.get_slides():  # goes through every slider
                if slider.hit:  # checks, whether it is hit
                    # moves the slider and thus changes the value of it
                    slider.move(
                        vo.get_window_size(vo.get_screen_size()[0],
                                           vo.get_screen_size()[1])[2])
                    # displays the volume settings on the game window
                    vo.draw_screen(lg.get_zustand(), lg.get_background(),
                                   lg.get_language(), [[[0]]], resource_path,
                                   lg.get_task_number(), lg.get_settings())

        # continues counting, used to fix a bug instantly having a mousebuttonup event after mousebuttondown
        num += 1
        if num > 254:
            num = 0

        if lg.get_zustand() == "request":  # game has been started
            begin = False  # starts the game

        try:
            music.set_volume(lg.get_music_volume()
                             )  # adjusts the volume of the music playing
        except NameError:  # music could not be found
            pass

    return request, running, settings, win, again