Example #1
0
def run_game():  # This plays when you start up main.py

    pygame.init()  # Initialize/open pygame
    gameDisplay = pygame.display.set_mode(
        (DISPLAY_W, DISPLAY_H))  # Set boundries for the game window
    pygame.display.set_caption('Learn to fly')  # Title of the game

    running = True  # Set running to true to use the running loop
    bgImg = pygame.image.load(BG_FILENAME)  # Load background image
    pipes = PipeCollection(gameDisplay)
    pipes.create_new_set()
    bird = Bird(gameDisplay)

    label_font = pygame.font.SysFont("monospace",
                                     DATA_FONT_SIZE)  # Choose font and size

    clock = pygame.time.Clock()  # Pygame has a clock which can be used
    dt = 0  # delta time
    game_time = 0  # BeginTime = 0
    num_iterations = 1

    # From here the loop starts, all of the above will not play again
    while running:  # While the variable running is true

        dt = clock.tick(
            FPS
        )  # The difference in time takes the frames per second into account
        game_time += dt  # Add delta time to total time

        gameDisplay.blit(
            bgImg, (0, 0))  # Draw background image from the upper right corner

        for event in pygame.event.get():  # If something happens
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:  # When a key is pressed, put running to false, which stops the loop
                if event.key == pygame.K_SPACE:
                    bird.jump()
                else:
                    running = False

        pipes.update(dt)  # update the drawn pipe
        bird.update(dt, pipes.pipes)  # update bird and give list of pipes

        if bird.state == BIRD_DEAD:  # every time the bird dies, create new game, but increase iterations
            pipes.create_new_set()
            game_time = 0
            bird = Bird(gameDisplay)
            num_iterations += 1

        update_data_labels(gameDisplay, dt, game_time, num_iterations,
                           label_font)  # While running, update the text
        pygame.display.update(
        )  # Draw all of what is called on top of the layers that are there (30 times per second, FPS)
Example #2
0
def run_game():

    pygame.init()
    gameDisplay = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
    pygame.display.set_caption('Learn to fly')

    running = True
    bgImg = pygame.image.load(BG_FILENAME)
    pipes = PipeCollection(gameDisplay)
    pipes.create_new_set()

    label_font = pygame.font.SysFont("monospace", DATA_FONT_SIZE)

    clock = pygame.time.Clock()
    dt = 0
    game_time = 0

    while running:

        dt = clock.tick(FPS)
        game_time += dt

        gameDisplay.blit(bgImg, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                running = False

        update_data_labels(gameDisplay, dt, game_time, label_font)
        pipes.update(dt)

        pygame.display.update()
def run_game():

    pygame.init()
    gameDisplay = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
    pygame.display.set_caption('Learn to fly')

    running = True
    bgImg = pygame.image.load(BG_FILENAME)
    pipes = PipeCollection(gameDisplay)
    pipes.create_new_set()
    bird = Bird(gameDisplay)

    label_font = pygame.font.SysFont("monospace", DATA_FONT_SIZE)

    clock = pygame.time.Clock()
    dt = 0
    game_time = 0
    num_iterations = 1

    while running:

        dt = clock.tick(FPS)
        game_time += dt

        gameDisplay.blit(bgImg, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    bird.jump()
                else:
                    running = False

        pipes.update(dt)
        bird.update(dt, pipes.pipes)

        if bird.state == BIRD_DEAD:
            pipes.create_new_set()
            game_time = 0
            bird = Bird(gameDisplay)
            num_iterations += 1

        update_data_labels(gameDisplay, dt, game_time, num_iterations,
                           label_font)
        pygame.display.update()
Example #4
0
def run_game():
    pygame.init()
    gameDisplay = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
    pygame.display.set_caption('Learn to fly')

    running = True
    bgImg = pygame.image.load(BG_FILENAME)
    pipes = PipeCollection(gameDisplay)
    pipes.create_new_set()
    birds = BirdCollection(gameDisplay)

    label_font = pygame.font.SysFont('monospace', DATA_FONT_SIZE)

    clock = pygame.time.Clock()
    dt = 0
    game_time = 0
    record_time = 0
    num_iterations = 1

    while running:
        dt = clock.tick(FPS)  # synchronizes change in time with FPS
        game_time += dt

        gameDisplay.blit(bgImg, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False

        pipes.update(dt)
        num_alive = birds.update(dt, pipes.pipes)

        if num_alive == 0:
            if game_time > record_time:
                record_time = game_time
            pipes.create_new_set()
            game_time = 0
            birds.evolve_population()
            num_iterations += 1

        update_data_labels(gameDisplay, dt, game_time, record_time,
                           num_iterations, num_alive, label_font)
        pygame.display.update()