【问题标题】:Image not showing up on pygame screen图像未显示在 pygame 屏幕上
【发布时间】:2021-04-21 15:51:10
【问题描述】:

我是第一次使用 pygame 制作自己的游戏,而且我正在使用精灵。我正在尝试将图像粘贴到屏幕上,但它不起作用。它显示的只是一个空白的白色屏幕。 代码如下:

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
class SpriteSheet(object):
    def __init__(self, file_name):
        self.sprite_sheet = pygame.image.load(file_name).convert()
    def get_image(self, x, y, width, height):
        image = pygame.Surface([width, height]).convert()
        image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
        image.set_colorkey(BLACK)
        return image

class Bomb(pygame.sprite.Sprite):
    change_x =0
    change_y = 0
    def __init__(self):
        image = pygame.Surface([256, 256]).convert()
        sprite_sheet = SpriteSheet("Bomb_anim0001.png")
        image = sprite_sheet.get_image(0, 0, 256, 256)

pygame.init()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
done = False
  
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    
    screen.fill(WHITE)
    bomb  = Bomb()
    clock.tick(60)
    pygame.display.flip()
pygame.quit()

这是图片: Click on this link.

我们将不胜感激任何帮助。谢谢!

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    你必须blitscreen上的图像Surface

    但还有更多工作要做。在Bomb 类中添加rect 属性:

    class Bomb(pygame.sprite.Sprite):
        
        def __init__(self, x, y):
            super().__init__()
            sprite_sheet = SpriteSheet("Bomb_anim0001.png")
            self.image = sprite_sheet.get_image(0, 0, 256, 256)
            self.rect = self.image.get_rect(topleft = (x, y) 
            self.change_x = 0
            self.change_y = 0
    

    创建一个pygame.sprite.Group 并将bomb 添加到draw屏幕上Group中的所有精灵:_

    bomb  = Bomb(100, 100)
    all_sprites = pygame.sprite.Group()
    all_sprites.add(bomb)
    
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        
        screen.fill(WHITE)
        all_sprites.draw(screen)
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    

    pygame.sprite.Group.draw()pygame.sprite.Group.update()pygame.sprite.Group提供的方法。

    前者将update 方法委托给包含的pygame.sprite.Sprites - 你必须实现该方法。见pygame.sprite.Group.update()

    在组中的所有 Sprite 上调用 update() 方法 [...]

    后者使用包含的pygame.sprite.Sprites 的imagerect 属性来绘制对象-您必须确保pygame.sprite.Sprites 具有所需的属性。见pygame.sprite.Group.draw():

    将包含的 Sprite 绘制到 Surface 参数。这将Sprite.image 属性用于源表面,并使用Sprite.rect。 [...]

    【讨论】:

    • 给我一个错误-“self.rect = self.image.get_rect() AttributeError: 'Bomb' object has no attribute 'image'”。我该怎么办?
    • 哈哈,真不敢相信我错过了!但看起来错误不能让我一个人呆着 XD 现在我明白了:File "C:\Users\<Name>\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 159, in add_internal self.__g[group] = 0 AttributeError: 'Bomb' object has no attribute '_Sprite__g'
    • 我的代码中有这个,但它给了我错误,所以我删除了它。你能简单解释一下它的作用吗?非常感谢你的帮助,我花了几个小时试图解决这个问题。
    • @PianoPianist 用于调用基类pygame.sprite.Sprite的构造函数。见super()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多