1

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?

Alan Bagel
  • 818
  • 5
  • 24

1 Answers1

2

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Thanks! I understand now. That really helped me understand pygame better. – Alan Bagel Mar 19 '21 at 17:46
  • 1
    (don't forget to add a call to delay between frames in the eventloop, otherwise this takes 100% CPU) – jsbueno Mar 19 '21 at 17:46
  • 1
    @jsbueno No never use `delay`, but use [`pygame.time.Clock.tick()`](https://www.pygame.org/docs/ref/time.html) – Rabbid76 Mar 19 '21 at 17:49
  • Wait, in the group part, I only wanted one player. But if I was going to create an enemy class, the group part would definitely help. Thanks again! – Alan Bagel Mar 19 '21 at 17:53
  • 1
    @JiuJiu Yes of course, the group is intended for the use with more than 1 object However, there is [`pygame.sprite.GroupSingle`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.GroupSingle). – Rabbid76 Mar 19 '21 at 17:57
  • Oh, so GroupSingle is a group with only one thing in it. Does it have mostly the same methods as Group? Should I use GroupSingle over no group at all? – Alan Bagel Mar 19 '21 at 18:09
  • 1
    @JiuJiu GroupSingle is rarely used. However it may be useful for functions like [`pygame.sprite.groupcollide`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.groupcollide). It's up to you and depends on your need. I just wanted to point out that there is. – Rabbid76 Mar 19 '21 at 18:12
  • 1
    OK, thanks! (This is also my first post.) – Alan Bagel Mar 19 '21 at 18:19
  • 1
    @JiuJiu Thank you. You're welcome. – Rabbid76 Mar 19 '21 at 18:19
  • 1
    yes - `pygame.time.clock.tick` - sorry, I was going to write "a delay" in the previous comment, but intending you to talk about this. – jsbueno Mar 19 '21 at 19:44
  • 2
    @jsbueno At least I added the code to the answer. However, I cannot give a full tutorial in one answer. The answer has been far too long now. – Rabbid76 Mar 19 '21 at 19:47
  • Oh, that's okay. – Alan Bagel Mar 20 '21 at 17:45