0

The code below causes the game title of my application to flicker, what is causing this issue? When the the bottom of the screen is reached, the scrolling stops and the game title is supposed to fade in. The title continuously fades in once it reaches full opacity.

# Run the game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update the position of the image
    image_y += scroll_speed
    
    # If the image has moved off the top of the screen, reset its position to the bottom
    if image_y < -meadow_image.get_height():
        image_y = screen_height
    image = pygame.image.load("game_title.png")
    # If the image has reached the bottom of the screen, stop updating the position
    if image_y + meadow_image.get_height() < screen_height:
        scroll_speed = 0
        image.set_alpha(0)

        # Set up a clock to control the frame rate
        clock = pygame.time.Clock()

        # Fade in the image over the course of 500 frames
        for i in range(300):
            # Set the alpha value of the image based on the frame number
            image.set_alpha(i)

            # Check if the alpha value has reached 255
            if image.get_alpha() == 255:
                # Skip the loop if the alpha value has reached 255
                break

            # Display the image on the screen
            screen.blit(image, (545, 300))
            pygame.display.flip()

            # Wait for one frame
            pygame.time.delay(1)

    # Draw the image at the updated position
    screen.blit(meadow_image, (0, image_y))
 
    # Update the display
    pygame.display.update()
okra
  • 7
  • 5

1 Answers1

0

Either call flip() or update() once per frame. Never twice unless you know what you're doing. Those signal when you want the frame to be drawn to the screen. You are drawing your frame to the screen twice; once before you draw the text and once after. Hence why half the time the text isn't there. For more info on update() and flip(), see: https://stackoverflow.com/a/29317382/6273251

Random Davis
  • 6,662
  • 4
  • 14
  • 24