TypeError:参数1必须是pygame.Surface,而不是元组

时间:2019-05-14 13:59:15

标签: python-3.x pygame-surface

我是Stack Overflow的新用户,我的问题是,如果我运行代码,它将给我错误,错误提示:

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

TypeError:参数1必须是pygame.Surface,而不是元组

如何解决此问题?您的回答将不胜感激!谢谢。 :)

我尝试用Google搜索它,但是他们的答案与我的不同。

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)

1 个答案:

答案 0 :(得分:1)

您唯一的问题是,您正试图通过一次调用将两张图像拉伸到表面。将blit分为两个不同的呼叫将解决您的问题:

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

此外,您可能需要调整输入图像的大小。如您所愿,您的代码将以原始分辨率拍摄图像,这意味着它们可能太大或太小。您可以像这样使用pygame.transform.scale(img, imgSize)进行此操作:

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

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