【发布时间】:2015-05-17 23:22:15
【问题描述】:
所以我正在尝试使用 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
【问题讨论】:
-
这不是您的问题的原因,但在您的图像加载文件中,您应该使用
os.path.join(WastelandSprites, "WastelandBasic.png")(并且缩进搞砸了)。
标签: python python-2.7 dictionary pygame sprite