使用 pygame 生成玩家精灵图像时出现问题

Problems generating Player sprite image with pygame

我正在尝试 运行 本教程 https://opensource.com/article/17/12/game-python-add-a-player 中的以下 pytho pygame 代码。 在更正我在这个问题 'Player' object has no attribute 'rect 上发布的缩进后,我尝试 运行 下面的 python 代码,然后是名为 images 的本地文件夹中的文件树,如下所示。 但是我在游戏中看不到hero.png。我也试试 .

文件夹图片:

stage.png: https://imgur.com/YyiEJ0q hero.png: https://opengameart.org/sites/default/files/spellun-sprite.png

我的代码:

import pygame  # load pygame keywords
import sys     # let  python use your file system
import os      # help python identify your OS

'''
Create class for player
'''

class Player(pygame.sprite.Sprite):
    def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.images = []
      img = pygame.image.load(os.path.join('images','hero.png')).convert()
      self.images.append(img)
      self.image = self.images[0]
      self.rect.x = 40
      self.rect.y = 30
#cria a classe para o player

'''
Objects
'''

# put Python classes and functions here

'''
Setup
'''
worldx = 260
worldy = 220

fps   = 40  # frame rate
ani   = 4   # animation cycles
clock = pygame.time.Clock()
pygame.init()
#BUG REMOVIDO: BUG É ".convert()" da linha "backdrop = pygame.image.load(os.path.join('images','stage.png').convert())"
world    = pygame.display.set_mode([worldx,worldy])
backdrop = pygame.image.load(os.path.join('images','stage.png'))
backdropbox = world.get_rect()

BLUE  = (25,25,200)
BLACK = (23,23,23 )
WHITE = (254,254,254)

main = True

# put run-once code here

'''
Main Loop
'''

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

        if event.type == pygame.KEYDOWN:
            if event.key == ord('q'):
                pygame.quit()
                sys.exit()
                main = False
world.blit(backdrop, backdropbox)
# put game loop here

pygame.display.flip()
clock.tick(fps)

您必须将属性 .rect 添加到 Player:

class Player(pygame.sprite.Sprite):
    def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.images = []
      img = pygame.image.load(os.path.join('images','hero.png')).convert()
      self.images.append(img)
      self.image = self.images[0]
      self.rect = self.image.get_rect() # <----
      self.rect.x = 40
      self.rect.y = 30

创建播放器实例。创建一个 pygame.sprite.Group and add palyer:

player = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

使用draw()绘制组中的所有精灵。 draw 利用 Sprites 的 imagerect 属性,绘制 Group 中的所有 Sprites:

all_sprites.draw(world)

尊重 Indentation:

while main == True:

    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()
            main = False

        if event.type == pygame.KEYDOWN:
            if event.key == ord('q'):
                pygame.quit()
                sys.exit()
                main = False

    # draw background
    world.blit(backdrop, backdropbox)

    # draw scene
    all_sprites.draw(world)

    # update display
    pygame.display.flip()
    clock.tick(fps)