收到错误'TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method'


import random 
import sys
import pygame
from pygame.locals import *
#global var for the games
FPS = 32
SCREENWIDTH = 289
SCREENHEIGHT = 511
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
GROUNDY = SCREENHEIGHT * 0.8
GAME_SPRITE = {}
GAME_SOUND = {}
PLAYER = 'imgs/bird.png'
BACKGROUND = 'imgs/background.png'
PIPE = 'imgs/pipe.png'

def welcomeScreen():

playerx = int(SCREENWIDTH/5)
playery = int((SCREENHEIGHT - GAME_SPRITE['player'].get_height())/2)
messagex = int((SCREENWIDTH - GAME_SPRITE['message'].get_width())/2)
messagey = int(SCREENHEIGHT*0.13)
basex = 0
while True:
for event in pygame.event.get():
# if user clicks on cross button, close the game
if event.type == QUIT or (event.type==KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# If the user presses space or up key, start the game for them
elif event.type==KEYDOWN and (event.key==K_SPACE or event.key == K_UP):
return
else:
SCREEN.blit(GAME_SPRITE['background'], (0, 0))    
SCREEN.blit(GAME_SPRITE['player'], (playerx, playery))    
SCREEN.blit(GAME_SPRITE['message'], (messagex,messagey ))    
SCREEN.blit(GAME_SPRITE['base'], (basex, GROUNDY))    
pygame.display.update()
FPSCLOCK.tick(FPS)


if __name__ == '__main__':
pygame.init()
FPSCLOCK = pygame.time.Clock()
pygame.display.set_caption('Flappy Bird')
GAME_SPRITE['numbers'] = (
pygame.image.load('imgs/1.png').convert_alpha(),
pygame.image.load('imgs/2.png').convert_alpha(),
pygame.image.load('imgs/3.png').convert_alpha(),
pygame.image.load('imgs/4.png').convert_alpha(),
pygame.image.load('imgs/5.png').convert_alpha(),
pygame.image.load('imgs/6.png').convert_alpha(),
pygame.image.load('imgs/7.png').convert_alpha(),
pygame.image.load('imgs/8.png').convert_alpha(),
pygame.image.load('imgs/9.png').convert_alpha(),
pygame.image.load('imgs/0.png').convert_alpha()
)
GAME_SPRITE['pipe'] =( 
pygame.transform.rotate(pygame.image.load(PIPE).convert_alpha(),180),
pygame.image.load(PIPE).convert_alpha()
)
GAME_SPRITE['message'] = pygame.image.load('imgs/message.png').convert_alpha()
GAME_SPRITE['base'] = pygame.image.load('imgs/base.png').convert_alpha()

GAME_SOUND['crash'] = pygame.mixer.Sound('sound/crash.mp3')
GAME_SOUND['hit'] = pygame.mixer.Sound('sound/hit.mp3')
GAME_SOUND['swoosh'] = pygame.mixer.Sound('sound/swoosh.mp3')

GAME_SPRITE['background'] = pygame.image.load(BACKGROUND).convert
GAME_SPRITE['player'] = pygame.image.load(PLAYER).convert_alpha()

while True:
welcomeScreen()

第40行,welcomeScreenSCREEN.blit(GAME_SPRITE["背景"],(0,0((TypeError:参数1必须是pygame。曲面,非内置函数或方法我试着做了很多也在网上搜索过的事情,但没有得到答案。有人能帮我吗?

问题是这一行:

GAME_SPRITE['background'] = pygame.image.load(BACKGROUND).convert

有了这个任务,你的background就是一个函数。你需要这样称呼它:

GAME_SPRITE['background'] = pygame.image.load(BACKGROUND).convert()

GAME_SPRITE['background'] = pygame.image.load(BACKGROUND).convertconvert部分是函数还是方法?如果是这样,你就错过了后面的()

将该行更改为

GAME_SPRITE['background'] = pygame.image.load(BACKGROUND).convert()

,看看它是否达到了您的预期。

如果这解决了您的问题,那么问题是您已将convert函数分配给GAME_SPRITE['background'],而不是调用convert()并分配返回值。

相关内容

最新更新