【发布时间】:2018-05-25 16:05:02
【问题描述】:
我有一个 pygame 游戏,它在我的笔记本电脑上运行良好,但是当我将它移到我的树莓派上时,很多精灵都没有出现。它们在那里是因为我仍然可以点击它们,但它们似乎并没有真正被绘制出来。我认为可能是因为 pi 无法处理数百个 png,所以我将它们全部压缩,仍然没有不同的结果。每次都不会出现相同的精灵。如果我重新启动脚本,甚至只是刷新屏幕,就会出现不同的图像。然后我把它画到 Adafruit 的电容 tft 屏幕上,结果还是一样。鼠标也是倒置的,这有点烦人,但我会发现这是一个不同的问题。精灵肯定在那里,因为我仍然可以单击它们,它们只是不可见,就像它无法加载图像一样,但它不会出错。
import pygame
class base_sprite(pygame.sprite.Sprite):
def __init__(self, color=(0,0,0), width=0, height=0, image=None,x=0,y=0, scale=None):
pygame.sprite.Sprite.__init__(self)
if "Surface" in type(image).__name__:
self.image = image
else:
self.image = pygame.image.load(image)
if scale != None:
self.image = pygame.transform.scale(self.image, (scale[0], scale[1]))
pygame.draw.rect(self.image, color, [5000000,5000000,width,height])
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
pygame.init()
width = 320
height = 240
s = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
gameGroup = pygame.sprite.Group()
inventoryGroup = pygame.sprite.Group()
back = base_sprite(width=320, height=240, image="images/back.png", x=0, y=0)
headBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=10, scale=[50, 50])
handBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=250, y=94, scale=[50, 50])
bodyBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=94, scale=[50, 50])
feetBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=178, scale=[50, 50])
spellBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=110, y=10, scale=[50, 50])
symbol = base_sprite(width=100, height=100, image="images/mageSmall.png", x=5, y=(height/2)-50)
gameGroup.add(back)
inInventory = False
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
inInventory = not inInventory
gameGroup.draw(s)
if inInventory:
inventoryGroup.add(back)
inventoryGroup.add(symbol)
inventoryGroup.add(headBorder)
inventoryGroup.add(handBorder)
inventoryGroup.add(bodyBorder)
inventoryGroup.add(feetBorder)
inventoryGroup.add(spellBorder)
inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicHat.png", x=180, y=10, scale=[50, 50]))
inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicThingToHitPeopleWith.png", x=250, y=94, scale=[50, 50]))
inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicShirt.png", x=180, y=94, scale=[50, 50]))
inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicShoes.png", x=180, y=178, scale=[50, 50]))
inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicBook.png", x=110, y=10, scale=[50, 50]))
inventoryGroup.draw(s)
clock.tick(60)
pygame.display.flip()
【问题讨论】:
-
如果没有minimal reproducible example(也可能是屏幕截图),很难判断是什么导致了问题。
-
@TedKleinBergman 这是问题 gif 的链接imgur.com/a/z43v0Y3。如您所见,并非所有按钮都在前几个屏幕中加载,一旦我超过了,每次切换库存时都会加载不同的精灵。对不起,我没有包含任何代码,我很笨。我会很快添加的。
-
看起来你正在向
inventoryGroup添加相同的精灵,每一帧inInventory是True。尝试在游戏循环之前使用它们,并且只在游戏循环中使用inventoryGroup.draw(s)。不知道它是否能解决问题,但它似乎会导致问题。 -
@TedKleinBergman 这并不能解决问题。在完整的游戏中,我有一个名为 newInventory 的变量,它打开每次按下按钮,然后在游戏循环中绘制屏幕并将自身设置为 false,因此它只添加一次精灵。我将它用于小版本,但它不能解决问题。
-
问题中的版本是否存在同样的问题?
标签: python raspberry-pi pygame