无法从字典中复制图像
Having Trouble Blitting Image From Dictionary
所以我正在尝试使用 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
所以我稍微研究了一下代码并在谷歌上搜索了一个小时左右,但我不明白为什么会出现错误。我想这是因为我无法将图像保存在字典中,但我不确定。有人对如何修复它有任何想法吗?
我的主文件:
导入 pygame
从启动导入 LoadTextures
pygame.init()
#Sets the color White
WHITE = (255, 255, 255)
#Sets screen size Variable
size = (900, 900)
#Sets Screen Size
screen = pygame.display.set_mode(size)
#Sets name of Game
pygame.display.set_caption("Growth of Deities")
closeWindow = False
clock = pygame.time.Clock()
Sprites = LoadTextures.Load()
while not closeWindow:
#Repeat while game is playing
for event in pygame.event.get():
#Close Window if you close the window
if event.type == pygame.QUIT:
closeWindow = True
#Logic Code
#Rendering Code
pygame.Surface.blit(Sprites["TileWastelandBasic"], (0, 0))
#Clear Screen
screen.fill(WHITE)
#Update Screen
pygame.display.flip()
#Set Tick Rate
clock.tick(60)
#Closes Game
pygame.quit()
我的图像加载文件:
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
问题不是因为你的字典。 blit的签名是
blit(source, dest, area=None, special_flags = 0) -> Rect
其中 source
必须是曲面。但是,这假定 blit
正在使用 pygame.Surface 实例 作为接收者调用。相反,您从 class 调用 blit
函数,这意味着它的签名实际上是
blit(self, source, dest, area=None, special_flags = 0) -> Rect
其中 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
所以我正在尝试使用 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
所以我稍微研究了一下代码并在谷歌上搜索了一个小时左右,但我不明白为什么会出现错误。我想这是因为我无法将图像保存在字典中,但我不确定。有人对如何修复它有任何想法吗?
我的主文件: 导入 pygame 从启动导入 LoadTextures pygame.init()
#Sets the color White
WHITE = (255, 255, 255)
#Sets screen size Variable
size = (900, 900)
#Sets Screen Size
screen = pygame.display.set_mode(size)
#Sets name of Game
pygame.display.set_caption("Growth of Deities")
closeWindow = False
clock = pygame.time.Clock()
Sprites = LoadTextures.Load()
while not closeWindow:
#Repeat while game is playing
for event in pygame.event.get():
#Close Window if you close the window
if event.type == pygame.QUIT:
closeWindow = True
#Logic Code
#Rendering Code
pygame.Surface.blit(Sprites["TileWastelandBasic"], (0, 0))
#Clear Screen
screen.fill(WHITE)
#Update Screen
pygame.display.flip()
#Set Tick Rate
clock.tick(60)
#Closes Game
pygame.quit()
我的图像加载文件:
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
问题不是因为你的字典。 blit的签名是
blit(source, dest, area=None, special_flags = 0) -> Rect
其中 source
必须是曲面。但是,这假定 blit
正在使用 pygame.Surface 实例 作为接收者调用。相反,您从 class 调用 blit
函数,这意味着它的签名实际上是
blit(self, source, dest, area=None, special_flags = 0) -> Rect
其中 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