【问题标题】:Having Trouble Blitting Image From Dictionary从字典中删除图像时遇到问题
【发布时间】: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


【解决方案1】:

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

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

其中source 必须是一个表面。但是,这假设 blit 正在以 pygame.Surface instance 作为接收者的方式被调用。相反,您从它的 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

【讨论】:

  • 好的,没有出错。但屏幕上也没有出现任何内容
  • @1SDANTheMan 那是因为您在对精灵进行 blitting 后正在清除屏幕。 :~)
  • 5 ...我不敢相信我没有注意到这一点。 Le Facepalm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多