使用PyGame模拟存储在元组中的pygame.Surface

时间:2016-03-15 13:49:07

标签: python pygame tuples blit

当尝试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存储在元组中的图像?

1 个答案:

答案 0 :(得分:1)

这不是你如何使用方法。你应该写:

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

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

为什么需要屏幕?因为blit是块图像传输。它从一个图像到另一个图像。