Home > Mobile >  Flickering Sprite in Pygame
Flickering Sprite in Pygame

Time:12-22

I know lots of people have had issues with flickering images in pygame on here, but none of the responses have helped me. I am trying to make Space Invaders, however, the bullet flickers as it moves up the screen. Please try to help me, and thank you! I do not currently care about the size, position, or scale of the bullet, I know it does not look great, but I just want it to display properly! Below is the code:

import pygame
#import sys- might use later
import random

#Sets the starting values for screen, etc. of the playing space
pygame.init()
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Invaders")
play = True
clock = pygame.time.Clock()
blkColor = (0, 0, 0)

#Loads and sizes the alien and player ship(s)
playerShip = pygame.image.load('ship.png')
playerShip = pygame.transform.scale(playerShip, (50, 50))
playerX = 370
playerY = 520
alien = pygame.image.load('alien.png')
alien = pygame.transform.scale(alien, (35, 35))
alienX = random.randint(0, 750)
alienY = 0
move = 5
alienMove = 5
bullet = pygame.image.load('bullet.png')
bullet = pygame.transform.scale(bullet, (5, 100))
bulletX = 0
bulletY = 600
hit = False
fire = False
hitRangeMin = -35
hitRangeMax = 35
score = 0


def player():
    screen.blit(playerShip, (playerX, playerY))

def enemy():
    screen.blit(alien, (alienX, alienY))

def alienMovement():
    global alienX
    global alienY
    global alienMove
#Moves the alien across the screen; when it hits the edge, it shifts down one spot and goes the other direction
    alienX  = alienMove
    if alienX > 750:
        alienMove = -5
        alienY  = 35
    if alienX < 0:
        alienMove = 5
        alienY  = 35

def shoot(x, y):
    global fire
    global bulletY
    fire = True
    screen.blit(bullet, (x, y))
    pygame.display.flip()
    if bulletY < 0:
        fire = False
        bulletY = 550
    elif bulletY >= 0:
        fire = True

def gameOver(score):
    print('Will add score and display and stuff- does noo matter.')

# Keeps the game window open until exited
while not hit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

    player()
    enemy()

    key_input = pygame.key.get_pressed()
    if key_input[pygame.K_LEFT]:
        playerX -= move
    elif key_input[pygame.K_RIGHT]:
        playerX  = move

    if playerX > 800:
        playerX = 0
    if playerX < 0:
        playerX = 800

    #For shooting the bullet
    if key_input[pygame.K_SPACE]:
        bulletX = playerX   23
        shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
    if fire:
        shoot(bulletX, bulletY)
        bulletY -= 5


    screen.fill(blkColor)
    alienMovement()
    player()
    enemy()
    pygame.display.flip()
    clock.tick(60)

CodePudding user response:

The problem is caused by multiple calls to pygame.display.update(). An update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

Remove pygame.display.flip() from shoot:

def shoot(x, y):
    global fire
    global bulletY
    fire = True
    screen.blit(bullet, (x, y))
    
    # pygame.display.flip()            <--- DELETE
    
    if bulletY < 0:
        fire = False
        bulletY = 550
    elif bulletY >= 0:
        fire = True

And you have to clear the screen before drawing the bullet:

while not hit:
    # [...]

    screen.fill(blkColor)             # <--- INSERT

    if key_input[pygame.K_SPACE]:
        bulletX = playerX   23
        shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
    if fire:
        shoot(bulletX, bulletY)
        bulletY -= 5

    # screen.fill(blkColor)            <--- DELETE

    alienMovement()
    player()
    enemy()
    pygame.display.flip()
    clock.tick(60)
  • Related