Пример #1
0
def main(game):
    """
    Main game logic.

    :param game: Game object
    :return:
    """
    airplane = Airplane(game.get_airplane_img(), 200, 200)
    obstacles = [Obstacle(game.get_obstacle_img(), 700)]
    window = game.get_window()
    clock = game.get_clock()
    font = game.get_font()

    # Define position of sliding background images
    bg_x = 0
    bg_x2 = game.get_window_width()

    # Start game
    run = True
    while run:
        is_jump = False
        clock.tick(FPS)  # Limit game loop to 30 iterations per second

        # Process user input from keyboard
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # Close the game window
                run = False
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    airplane.jump()
                    is_jump = True
        if not is_jump:
            airplane.move()

        # Check if airplane has fallen off screen
        if airplane.get_y() <= 0 or airplane.get_y() >= game.get_window_height(
        ):
            # Game over. Go to game over screen
            run = False
            game.set_game_in_session(False)
            return

        # Generate obstacle
        add_obstacle = False
        for obstacle in obstacles:
            if obstacle.collide(airplane):
                # Airplane hit obstacle. Go to game over screen
                run = False
                game.set_game_in_session(False)
                return
            if not obstacle.get_passed(
            ) and obstacle.get_x() < airplane.get_x():
                # Obstacle has been successfully passed, set flag to generate new obstacle
                obstacle.set_passed(True)
                add_obstacle = True
            obstacle.move()
        if add_obstacle:
            # Generate a new obstacle and increment score by 1
            game.increment_score()
            obstacles.append(Obstacle(game.get_obstacle_img(), 700))

        # Update game window with changes
        draw_window(window, airplane, obstacles, game.get_bg_img(), bg_x,
                    bg_x2, font, game.get_score())

        # Sliding background
        bg_x -= 2
        bg_x2 -= 2
        if bg_x < -game.get_window_width():
            bg_x = game.get_window_width()
        if bg_x2 < -game.get_window_width():
            bg_x2 = game.get_window_width()