使用 PyGame 将 pygame.Surface 存储在元组中

Blitting a pygame.Surface stored in a tuple using PyGame

当尝试 blit 存储在元组中的图像时,出现以下错误:

Traceback (most recent call last):
  File "/home/lyut/Projects/FruitMachine/test.py", line 40, in <module>
    if __name__ == '__main__': main()
  File "/home/lyut/Projects/FruitMachine/test.py", line 34, in main
    draw_initial_gems()
  File "/home/lyut/Projects/FruitMachine/test.py", line 11, in draw_initial_gems
    pygame.Surface.blit(gems[0], (212, 100))
TypeError: argument 1 must be pygame.Surface, not tuple

我四处寻找类似的问题,但没有找到完全符合这个问题的问题。

这是我用来重现错误的源代码:

import pygame

gems = [
    pygame.image.load("graphics/Gems/1.png"),
    pygame.image.load("graphics/Gems/2.png"),
    pygame.image.load("graphics/Gems/3.png"),
]


def draw_initial_gems():
    pygame.Surface.blit(gems[0], (212, 100))
    pygame.Surface.blit(gems[1], (340, 100))
    pygame.Surface.blit(gems[2], (468, 100))


def main():
    pygame.init()
    clock = pygame.time.Clock()

    print type(gems[1]) # returns: <type 'pygame.Surface'>

    size = [800, 600]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("test")

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return


        screen.fill((255, 255, 255))

        draw_initial_gems()

        pygame.time.tick(30)

    pygame.quit()

if __name__ == '__main__': main()

所以我的问题是:如何 blit 存储在元组中的图像?

那不是你使用方法的方式。你应该写:

gems[0].blit(screen, (x, y))

Pygame 将方法用作函数时似乎会很困惑。 (通常你必须给出方法关注的实例作为第一个参数。)

为什么需要屏幕?因为blit是一个Block ImageTransfer。它从一张图片到另一张图片。