精灵不会出现在树莓皮上的pygame中

2024-10-05 10:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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()

Tags: rectimageselfaddbaseifpngwidth
1条回答
网友
1楼 · 发布于 2024-10-05 10:02:15

我不得不同意。这是解决问题的简单方法,也是避免处理渲染问题的最简单方法。以下是我的一些没有精灵的代码:

 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`

相关问题 更多 >

    热门问题