问题描述
好的,所以我正在使用 pygame 并且我正在尝试制作一个系统,当角色碰到屏幕区域的一侧时,会出现一些大文本并说“你崩溃了”。但是当我尝试代码并撞到墙上时,什么也没有发生,我在 shell 中收到错误
error: Traceback (most recent call last):
File "D:\pygame test.py",line 88,in <module>
game_loop()
File "D:\pygame test.py",line 84,in game_loop
crash()
File "D:\pygame test.py",line 44,in crash
message_display('You Crashed')
File "D:\pygame test.py",line 34,in message_display
gamedisplay.blit(TextSurface)
TypeError: argument 1 must be pygame.Surface,not pygame.Rect.
def message_display (text):
LargeText = pygame.font.Font('freesansbold.ttf',115)
TextRect = text_objects(text,LargeText)
TextSurface = text_objects(text,LargeText)
TextRect.center = ((display_width/2),(display_height/2))
pygame.display.flip()
gamedisplay.blit(TextSurface)
非常感谢。
解决方法
猜测您正在研究 this site 上的示例。
所以你的 text_objects
函数看起来像:
def text_objects(text,font):
textSurface = font.render(text,True,black)
return textSurface,textSurface.get_rect()
然后您需要更改您的 message_display
函数以处理来自 text_objects()
的两个返回:
def message_display (text):
LargeText = pygame.font.Font('freesansbold.ttf',115)
TextSurface,TextRect = text_objects(text,LargeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurface)
# this next line should probably only be called in your main loop
pygame.display.flip()