TypeError: 'pygame.Surface' object is not

2019-07-26 03:04发布

问题:

I have this issue when I would like to go the Game Over screen.

Traceback (most recent call last):
  File "C:\Users\MO\Desktop\Twerk\ballbounce_changed.py", line 215, in <module>
   game()
File "C:\Users\MO\Desktop\Twerk\ballbounce_changed.py", line 191, in game
  text("Game Over",30,white,300)
TypeError: 'pygame.Surface' object is not callable

This the section of the code for the exist screen:

while finish == True:
 screen.blit(menu,[0,0])
 text("Game Over",30,white,300)
 text("Instructions",310,white)
 text("-----------------------------------------------------",320,white)
 text("Avoid the the enemies",340,white)
 text("Last as long as you can!",360,white)
 text("Press space to start",420,white)

# display.update()
pygame.display.update()

 for event in pygame.event.get():
 if event.type == pygame.QUIT:
   repeat = False

 if event.type == pygame.KEYDOWN:
   if event.key == pygame.K_SPACE:
     repeat = True

if repeat == False:
pygame.quit()

else:
 game()

game()

If I remove the text within the game over screen, it does work. I as soon as I introduce text I get the above error message

(indention is not correct) I have the full code with inden here http://pastebin.com/VBkhX4kt

Thank you

回答1:

The error is because on line 93 you override the binding of the variable text from a function to whatever font.render() returns.

93: text = font.render('Starting Twerk... ', True, (100,100,100))

Thus, later on, when you call text(...) it's not calling the function you defined earlier, it's trying to treat text as a callable (which it is not, since it is now a pygame.Surface object).

The solution would be to alter that line and not override your text function.



标签: python pygame