Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame" screen.fill command" ,where do i place it?

Tags:

python

pygame

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

...

like image 329
SHREEJIT MISHRA Avatar asked Dec 28 '25 16:12

SHREEJIT MISHRA


1 Answers

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.

like image 111
Rabbid76 Avatar answered Dec 30 '25 05:12

Rabbid76



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!