Search code examples
pygamepython-3.8

Why is the sprite not moving?


I am currently programming a pygame game where you move a spaceship around the screen. Currently, I have got to the part where I made the spaceship and tried to make it move. However, when I try to move the spaceship around, the spaceship doesn't move!

Here is my current code:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 500))
screen.fill((255, 255, 255))


class Spaceship(pygame.sprite.Sprite):
    def __init__(self, s, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.screen = s
        self.x, self.y = x, y
        self.image = pygame.image.load("C:/eqodqfe/spaceship.png")
        self.image = pygame.transform.scale(self.image, (175, 175))
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)

    def update(self):
        self.rect.center = (self.x, self.y)


spaceship = Spaceship(screen, 400, 400)
screen.blit(spaceship.image, spaceship.rect)

running = True
while running:
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
            running = False

    key = pygame.key.get_pressed()
    if key[pygame.K_a]:
        spaceship.x -= 5
    elif key[pygame.K_d]:
        spaceship.x += 5
    elif key[pygame.K_w]:
        spaceship.y += 5
    elif key[pygame.K_s]:
        spaceship.y -= 5

    spaceship.update()
    pygame.display.update()

What is wrong with my current code?


Solution

  • You have to draw the Sprties in the application loop:

    clock = pygame.time.Clock()
    running = True
    while running:
        
        # handle events
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
                running = False
    
        key = pygame.key.get_pressed()
        if key[pygame.K_a]:
            spaceship.x -= 5
        elif key[pygame.K_d]:
            spaceship.x += 5
        elif key[pygame.K_w]:
            spaceship.y -= 5
        elif key[pygame.K_s]:
            spaceship.y += 5
    
        # update the position of the object
        spaceship.update()
    
        # clear the display 
        screen.fill((255, 255, 255))
    
        #  draw the object
        screen.blit(spaceship.image, spaceship.rect)
    
        # update the display
        pygame.display.update()
    
        # limit frames per second 
        clock.tick(60)
    

    The typical PyGame application loop has to:


    Furthermore I suggest to use a pygame.sprite.Group:

    pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.

    The former delegates the to the update mehtod of the contained pygame.sprite.Sprites - you have to implement the method. See pygame.sprite.Group.update():

    Calls the update() method on all Sprites in the Group [...]

    The later uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

    Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

    spaceship = Spaceship(screen, 400, 400)
    all_sprites = pygame.sprite.Group()
    all_sprites.add(spaceship)
    
    clock = pygame.time.Clock()
    running = True
    while running:
        clock.tick(60)
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
                running = False
    
        key = pygame.key.get_pressed()
        if key[pygame.K_a]:
            spaceship.x -= 5
        elif key[pygame.K_d]:
            spaceship.x += 5
        elif key[pygame.K_w]:
            spaceship.y += 5
        elif key[pygame.K_s]:
            spaceship.y -= 5
    
        all_sprites.update()
    
        screen.fill((255, 255, 255))
        all_sprites.draw(screen)
        pygame.display.update()