【问题标题】:Sprites not appearing in pygame on raspberry pi树莓派上的 pygame 中没有出现精灵
【发布时间】: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 添加相同的精灵,每一帧inInventoryTrue。尝试在游戏循环之前使用它们,并且只在游戏循环中使用 inventoryGroup.draw(s)。不知道它是否能解决问题,但它似乎会导致问题。
  • @TedKleinBergman 这并不能解决问题。在完整的游戏中,我有一个名为 newInventory 的变量,它打开每次按下按钮,然后在游戏循环中绘制屏幕并将自身设置为 false,因此它只添加一次精灵。我将它用于小版本,但它不能解决问题。
  • 问题中的版本是否存在同样的问题?

标签: python raspberry-pi pygame


【解决方案1】:

我必须同意。这是解决问题的万无一失的方法,也是规避处理渲染问题的最简单方法。这是我的一些没有精灵的代码:

 import pygame

pygame.init()
game_display = pygame.display.set_mode((800, 800))


# fixed variables at the start
x_pos = 400
y_pos = 400
current_speed = 15

def jump_coords(y_position, speed):
    if speed >= 0:
        #to move up, reduce the y-coordinate
        y_position -= speed
    return y_position

game_exit = False

# main loop
while not game_exit: 
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                y_pos = jump_coords(y_pos, current_speed)
                # 1 represents gravity value
                current_speed -= 1

    rect_one = pygame.Rect(x_pos, y_pos, 10, 10)  
    pygame.draw.rect(game_display, (255, 0, 0), rect_one)
    pygame.display.update()`enter code here`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多