IHateCoding
IHateCoding

Reputation: 99

pygame code thinks im putting in a string but it should be a surface

Im doing some computer science course work and am trying to find a frame for making a gif this is what I have at the moment but I cannot figure out whats wrong. Im pretty new to coding (only been coding for about a month) so any bonus advice would be appreciated.

import pygame
import sys

pygame.init()


class Animation(pygame.sprite.Sprite):

    def __init__(self, images, time_interval):
        super(Animation, self).__init__()
        self.images = images
        self.image = self.images[0]
        self.time_interval = time_interval
        self.index = 0
        self.timer = 0

    def update(self, seconds):
        self.timer += seconds
        if self.timer >= self.time_interval:
            self.image = self.images[self.index]
            self.index = (self.index + 1) % len(self.images)
            self.timer = 0


def create_images():
    images = []
    for i in xrange(6):
        image = pygame.Surface((256, 256))
        image.fill((255 - i * 50, 255, i * 50))
        images.append(image)
    return images


def main():
    images = ('assets\\image\\earth_troll\\idle_1.png',
              'assets\\image\\earth_troll\\idle_3.png',
              'assets\\image\\earth_troll\\idle_5.png',
              'assets\\image\\earth_troll\\idle_7.png',
              'assets\\image\\earth_troll\\idle_9.png')  # Or use a list of your own images.
    a = Animation(images, 0.25)

    # Main loop.
    while True:
        seconds = clock.tick(FPS) / 1000.0  # 'seconds' is the amount of seconds each loop takes.

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        a.update(seconds)
        screen.fill((0, 0, 0))
        screen.blit(a.image, (250, 100))
        pygame.display.update()


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()
    FPS = 60
    main()

I'm getting the error

TypeError: argument 1 must be pygame.Surface, not str

but I cannot figure out why.

Upvotes: 2

Views: 40

Answers (1)

Rabbid76
Rabbid76

Reputation: 210880

The list images contains filenames rather than pygame.Surface objects. Use pygame.image.load() to load the images and generate pygame.Surface objects:

filenames = ('assets\\image\\earth_troll\\idle_1.png',
             'assets\\image\\earth_troll\\idle_3.png',
             'assets\\image\\earth_troll\\idle_5.png',
             'assets\\image\\earth_troll\\idle_7.png',
             'assets\\image\\earth_troll\\idle_9.png')
images = [pygame.image.load(name) for name in filenames]

The filenames can be generated dynamically. For instance:

filenames = ['assets\\image\\earth_troll\\idle_' + str(i) + '.png' 
              for i in [1, 3, 5, 7, 9]]
images = [pygame.image.load(name) for name in filenames]

or

filenames = ['assets\\image\\earth_troll\\idle_' + str(1 + i*2) + '.png'
             for i in range(5)]
images = [pygame.image.load(name) for name in filenames]

Upvotes: 1

Related Questions