0

I am new user of Stack Overflow, and my problem is if I run the code it will give me the error, the error says:

DISPLAYSURF.blit((catImg, dogImg), (catx, caty, dogx, dogy))

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

how do I fix this problem? Your answer will be highly appreciated! Thank you. :)

I try to google it, but their answer is different than mine.

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Cat and Dog running.')

WHITE = (255, 255, 255)
catImg = pygame.image.load('cat.png')
dogImg = pygame.image.load('dog.png')
catx = 50
caty = 50
dogx = 25
dogy = 25
direction = 'right'

running = True
while running:
DISPLAYSURF.fill(WHITE)

if direction == 'right':
    catx += 5
    dogx += 5
    if catx == 250 and dogx == 250:
        direction = 'down'
elif direction == 'down':
    caty += 5
    dogy += 5
    if caty == 225 and dogy == 225:
        direction = 'left'
elif direction == 'left':
    catx -= 5
    dogx -= 5
    if catx == 10 and dogx == 10:
        direction = 'up'
elif direction == 'up':
    caty -= 5
    dogy -= 5
    if caty == 10 and dogy == 10:
        direction = 'right'

DISPLAYSURF.blit((catImg, dogImg), (catx, caty, dogx, dogy))

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

pygame.display.update()
fpsClock.tick(FPS)
louisfischer
  • 1,968
  • 2
  • 20
  • 38

1 Answers1

1

Your only problem is that you're trying to blit two images to the surface with one call. Separating the blits into two different calls will fix your issue:

# can't blit two different images with one call
# DISPLAYSURF.blit((catImg, dogImg), (catx, caty, dogx, dogy))

#instead, use two calls
DISPLAYSURF.blit(catImg, (catx, caty))
DISPLAYSURF.blit(dogImg, (dogx, dogy))

Also, you might want to resize the images you're inputting. As you have it, your code will take the images at their native resolution, meaning they may be too big or small. You can do this with pygame.transform.scale(img, imgSize) like so:

#also, you might want to resize the images used:
imgSize = (300, 300)
catImg = pygame.transform.scale(pygame.image.load('cat.png'), imgSize)
dogImg = pygame.transform.scale(pygame.image.load('dog.png'), imgSize)

Source: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit

kfu02
  • 31
  • 3