为什么精灵不动?

Why is the sprite not moving?

我目前正在编写一个 pygame 游戏,您可以在其中围绕屏幕移动宇宙飞船。目前,我已经到了制作宇宙飞船并试图让它移动的部分。但是,当我尝试移动飞船时,飞船却没有移动!

这是我当前的代码:

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()

我当前的代码有什么问题?

您必须在应用程序循环中绘制 Sprties:

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)

典型的 PyGame 应用程序循环必须:


此外,我建议使用 pygame.sprite.Group:

pygame.sprite.Group.draw() and pygame.sprite.Group.update()pygame.sprite.Group.

提供的方法

前者委托给包含pygame.sprite.Sprites - you have to implement the method. See pygame.sprite.Group.update()update方法:

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

后者使用包含的pygame.sprite.Spriteimagerect属性来绘制对象——你必须确保pygame.sprite.Sprite具有所需的属性。见 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()