图像未显示在 pygame 屏幕上

问题描述

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

./cachesim -m 64 -s 4 -e 0 -b 4 -i addresses.txt -r lfu
addr size 64,sets: 16,lines: 1,block size: 16
Initializing Cache...
Mem for sets allocated.
Cache initialization complete.
123456789ABCDEF
Tag: 1234567  Set: B  Miss,no evict
23F0FFFA003464F
Tag: 23F0FFF  Set: 3  Miss,no evict
DC2387002FAFF
Tag: DC238  Set: 2  Miss,no evict
10F0F00FAC4A34F
Tag: 10F0F00  Set: 4  Miss,no evict
DC2387002FAFF
Tag: DC238  Set: 2  hit
602598F3AF2C12F
Tag: 602598F  Set: 2  Miss,evict
81F2319543FC32C
Tag: 81F2319  Set: F  Miss,no evict
10F0F00FAC4A34F
Tag: 10F0F00  Set: 4  hit
40F0F00FAC4A34F
Tag: 40F0F00  Set: 4  Miss,evict
602598F3AF2C12F
Tag: 602598F  Set: 2  hit
DC2387002FAFF
Tag: DC238  Set: 2  Miss,evict
123456789ABCDEF
Tag: 1234567  Set: B  hit
23F0FFFA003464F
Tag: 23F0FFF  Set: 3  hit
40F0F00FAC4A34F
Tag: 40F0F00  Set: 4  hit
602598F3AF2C12F
Tag: 602598F  Set: 2  Miss,evict
81F2319543FC32C
Tag: 81F2319  Set: F  hit
10F0F00FAC4A34F
Tag: 10F0F00  Set: 4  Miss,evict
10F0F00FAC4A34F
Tag: 10F0F00  Set: 4  hit
40F0F00FAC4A34F
Tag: 40F0F00  Set: 4  Miss,evict
602598F3AF2C12F
Tag: 602598F  Set: 2  hit
81F2319543FC32C
Tag: 81F2319  Set: F  hit
10F0F00FAC4A34F
Tag: 10F0F00  Set: 4  Miss,evict
40F0F00FAC4A34F
Tag: 40F0F00  Set: 4  Miss,evict
602598F3AF2C12F
Tag: 602598F  Set: 2  hit
Hits: 11,Misses: 13,evictions: 8
Total transfers: 24
Miss Rate: 54.17
Avg access time: 55 clk cycles
Total run time: 1320 clk cycles
Deallocating mem...
Mem deallocation completed.

这是图片Click on this link.

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

解决方法

您必须在 screen Surfaceblit 图像。

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

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,256,256)
        self.rect = self.image.get_rect(topleft = (x,y) 
        self.change_x = 0
        self.change_y = 0

创建一个 pygame.sprite.Group 并将 bomb 添加到 Groupdraw 屏幕上 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 提供的方法。

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

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

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

将包含的精灵绘制到 Surface 参数。这对源表面使用 Sprite.image 属性和 Sprite.rect。 [...]