Reputation: 3392
I want to animate a Sprite Worker
move horizontally from left side to the right. Also, I have another Sprite which is a static background image.
The code fails at line screen.blit(worker, (w_x,w_y))
with the error message:
TypeError: argument 1 must be pygame.Surface, not Worker
I understand that it's possible to only blit surfaces, not Worker
. But how can I put Worker
into surface in order to animate it?
I am very new to PyGame. Therefore any help will be highly appreciated.
Code:
import pygame, random
WHITE = (255, 255, 255)
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
SCREENWIDTH=1000
SCREENHEIGHT=578
class Worker(pygame.sprite.Sprite):
def __init__(self, image_file, location):
# Call the parent class (Sprite) constructor
# super().__init__()
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
pygame.init()
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("TEST")
#all_sprites_list = pygame.sprite.Group()
worker = Worker("worker.png", [0,0])
w_x = worker.rect.left
w_y = worker.rect.top
bg = Background('bg.jpg', [0,0])
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
screen.blit(pygame.transform.scale(bg.image, (SCREENWIDTH, SCREENHEIGHT)), bg.rect)
#Draw geo-fences
pygame.draw.rect(screen, GREEN, [510,150,75,52])
pygame.draw.rect(screen, GREEN, [450,250,68,40])
w_x += 5
screen.blit(worker, (w_x,w_y))
#Refresh Screen
pygame.display.update()
pygame.quit()
quit()
Upvotes: 1
Views: 492
Reputation: 20438
pygame.Surface.blit
takes another pygame.Surface
as the first argument, but you're passing a Worker
object. Just pass the image
attribute of the worker:
screen.blit(worker.image, (w_x,w_y))
You could also put your sprite objects into a pygame.sprite.Group
and blit all of the sprites with the draw
method.
Outside of the while loop:
sprite_group = pygame.sprite.Group()
# Add the sprites.
sprite_group.add(worker)
Inside the while loop:
sprite_group.update() # Call the `update` methods of all sprites.
sprite_group.draw(screen) # Blit the sprite images at the `rect.topleft` coords.
Upvotes: 1