3

i am lately facing difficulty in screen.fill command , my program runs fine but the characte is jittery and sometims wont appear on the screen ,i have tried placing the command from different location in code . it also sometimes leaves trails behind it
... import pygame

pygame.init()
pygame.display.init()
#creation of screen
screen = pygame.display.set_mode((800, 600))
#window title
# game.display.set_caption("space invaders")
#player coordinates
playerX = 360
playerY = 480
#player image
playerimg = pygame.image.load("spaceship.png")
#player change distance
playerX_change = 0

#drawing player on screen
def player(x, y):

pygame.display.flip()
pygame.display.update()
screen.blit(playerimg, (x, y))

#game loop
running = True

while running == True:
#event system
    for event in pygame.event.get():
        screen.fill((0, 0, 0))
#exit function
        if event.type == pygame.QUIT:
            pygame.quit()

            player(playerX, playerY)
#arrows key press detection or movement system
    if event.type == pygame.KEYDOWN:
        player(playerX,playerY)
        if event.key == pygame.K_LEFT:
            playerX_change = -0.3
        if event.key == pygame.K_RIGHT:
            playerX_change = 0.3

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

            playerX_change = 0
#movement
    playerX += playerX_change

...

1 Answers1

1

Clear the screen once, before grawing the objects on the screen. Actually you call screen.fill in the event loop. The event loop is executed once for each event. Move screen.fill from the event loop to the application loop. The application loop is executed once per frame.

while running == True:
    screen.fill((0, 0, 0))        # insert
#event system
    for event in pygame.event.get():
        #screen.fill((0, 0, 0))    # delete
#exit function
        if event.type == pygame.QUIT:
            pygame.quit()
    # [...]

Additionally update the display only once after drawing all the objects. See Why is the PyGame animation is flickering.

sloth
  • 99,095
  • 21
  • 171
  • 219
Rabbid76
  • 202,892
  • 27
  • 131
  • 174