从字典中快速传输图像时遇到问题

2024-06-26 01:41:45 发布

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

所以我试着开始使用Pygame,但是我在网上找到的所有教程都只使用了一个文件。我反复思考如何在一个函数中加载所有图像。决定把它们保存在字典里。问题是,当我试图从字典中粘贴图像时,我得到以下错误:

Traceback (most recent call last):
  File "J:\Growth of Deities\Main.py", line 32, in <module>
pygame.Surface.blit(Sprites["TileWastelandBasic"], (0, 0))
TypeError: argument 1 must be pygame.Surface, not tuple

所以我花了一点时间在google上搜索了一个小时左右的代码,但我不明白为什么会出错。我想是因为我不能在字典里保存图像,但我不确定。有人知道怎么修理吗?在

我的主文件: 导入pygame 从启动导入LoadTextures 游戏机初始化()

^{pr2}$

我的图像加载文件:

import pygame
import os, sys


def Load():
    Sprites = {}

WastelandSprites = 'Assets\Textures\Tile Sprites\Wasteland'

    Sprites["TileWastelandBasic"] = pygame.image.load(os.path.join(WastelandSprites + "\WastelandBasic.png")).convert_alpha()
    Sprites["TileWastelandBasic"] = pygame.transform.scale(Sprites["TileWastelandBasic"], (50, 50)).convert_alpha()

    return Sprites

Tags: 文件函数图像importalphaconvert字典os
1条回答
网友
1楼 · 发布于 2024-06-26 01:41:45

问题不是因为你的字典。blit的签名是

blit(source, dest, area=None, special_flags = 0) -> Rect

其中source必须是曲面。但是,这假设blit是用一个游戏。表面实例作为接收器。相反,您将从其调用blit函数,这意味着它的签名是有效的

^{pr2}$

其中self也必须是曲面。您可以通过将呼叫更改为来解决您的问题

pygame.Surface.blit(screen, Sprites["TileWastelandBasic"], (0, 0))

但我建议你用更地道的

screen.blit(Sprites["TimeWastelandBasic"], (0, 0))

相反。在

参见:http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit

相关问题 更多 >