如何解决文字没有出现 Pygame 没有错误
我有一个使用 pygame 制作的游戏,但显然文字不起作用。控制台也没有错误。这是我的代码:
font = pygame.font.SysFont("monospace",55)
def text_screen(text,color,x,y):
screen_text = font.render(text,True,color)
gameWindow.blit(screen_text,(x,y))
# rest of code [...]
# then where i need text;
if abs(snake_x - food_x) < 5 and abs(snake_y - food_y) < 5:
score +=1*10
print("score: ",score)
text_screen("score: " + str(score * 10),red,5,5)
pygame.display.update()
food_x = random.randint(20,screen_width / 2)
food_y = random.randint(20,screen_height / 2)
解决方法
要使文本永久化,您需要在每一帧绘制它,而不仅仅是在检测到碰撞时绘制一次。检测到碰撞时渲染文本并设置变量 (score_surf
)。设置变量后,在应用程序循环中绘制文本:
score_surf = None
while True:
# [...]
if abs(snake_x - food_x) < 5 and abs(snake_y - food_y) < 5:
score += 10
print("Score: ",score)
score_surf = font.render("Score: " + str(score),True,red)
food_x = random.randint(20,screen_width / 2)
food_y = random.randint(20,screen_height / 2)
# [...]
if score_surf != None:
gameWindow.blit(score_surf,(5,5))
# [...]
pygame.display.update()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。