Search code examples
pythonpygametuplesblit

Blitting a pygame.Surface stored in a tuple using PyGame


When trying to blit an image stored in a tuple I get the following error:

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

I've looked around for similar questions and haven't found any with this problem exactly.

Here is the source code that I used to reproduce the error:

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()

So my question is: How do I blit an image stored in a tuple?


Solution

  • Thats not how you use a method. You should write:

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

    Pygame seems to get very confused when you use the method as a function. (Normally you have to give the instance that the method concerns as first argument then.)

    Why do you need screen? Because blit is a Block Image Transfer. It goes from one image to another.