Trying to move image around screen, but "animation" is choppy

Viewed 29

If I hit a key it won't show the new position until I let off it. It almost teleports to the new spot instead of seeing it move to it. I previously made a square to move around with no issue. Tried looking at that code and doing the something but I'm missing something.

import pygame
import sys

pygame.init()

window = 900, 700
screen =  pygame.display.set_mode(window)
title = pygame.display.set_caption("ASTEROOOIDDD")
clock = pygame.time.Clock()



x = 340
y = 280
step = 1

background_surface = pygame.image.load('asteroid/images/background.jpg')
spaceship_surface =  pygame.image.load('asteroid/images/ship.png')
spaceship_surface = pygame.transform.scale(spaceship_surface, (80,80))



while True:
    
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

        screen.blit(background_surface,(0,0))
        screen.blit(spaceship_surface,(x,y))

    
    
    key_input = pygame.key.get_pressed()   
    if key_input[pygame.K_LEFT]:
        x  -= step
    if key_input[pygame.K_UP]:
        y -= step
    if key_input[pygame.K_RIGHT]:
        x += step
    if key_input[pygame.K_DOWN]:
        y += step
     


    spaceship_surface.blit(spaceship_surface, (x,y))
    pygame.display.update()
    clock.tick(30)
2 Answers

You just need to bring your screen.blit outside the event handler loop.

while True:

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

    screen.blit(background_surface, (0, 0))
    screen.blit(spaceship_surface, (x, y))

    key_input = pygame.key.get_pressed()
    if key_input[pygame.K_LEFT]:
        x -= step
    if key_input[pygame.K_UP]:
        y -= step
    if key_input[pygame.K_RIGHT]:
        x += step
    if key_input[pygame.K_DOWN]:
        y += step

    spaceship_surface.blit(spaceship_surface, (x, y))
    pygame.display.update()
    clock.tick(30)

It is a matter of Indentation. The display needs to be updated in every frame. (At least in every frame where something changes). The typical PyGame application loop has to:

import pygame
import sys

pygame.init()

window = 900, 700
screen =  pygame.display.set_mode(window)
title = pygame.display.set_caption("ASTEROOOIDDD")
clock = pygame.time.Clock()
x = 340
y = 280
step = 1

background_surface = pygame.image.load('asteroid/images/background.jpg')
spaceship_surface =  pygame.image.load('asteroid/images/ship.png')
spaceship_surface = pygame.transform.scale(spaceship_surface, (80,80))

while True:
    
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

    # INDENTATION
    #<--|
    screen.blit(background_surface,(0,0))
    screen.blit(spaceship_surface,(x,y))
 
    key_input = pygame.key.get_pressed()   
    if key_input[pygame.K_LEFT]:
        x  -= step
    if key_input[pygame.K_UP]:
        y -= step
    if key_input[pygame.K_RIGHT]:
        x += step
    if key_input[pygame.K_DOWN]:
        y += step
     
    spaceship_surface.blit(spaceship_surface, (x,y))
    pygame.display.update()
    clock.tick(30)
Related