如何删除/替换pygame中的文本 [英] How to remove/replace text in pygame

查看:403
本文介绍了如何删除/替换pygame中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 pygame 还很陌生,我遇到了我找不到答案的第一个问题..

I'm fairly new to pygame and ive hit my first stump which I cannot find an answer for..

在 blitting 文本之后,然后更改相同变量的字符串,游戏不是用新文本替换原始文本,而是将两个文本重叠..?

After blitting text, then changing the string for the same variable, the game instead of replacing the original text with the new, overlaps the two texts..?

推荐答案

您必须先擦除旧文本.Font.render 创建的表面是普通的表面.一旦 Surface 被 blit,它的内容就成为目标表面的一部分,你必须操纵 目标 表面来擦除源表面上的任何 blit.

You have to erase the old text first. Surfaces created by Font.render are ordinary surfaces. Once a Surface is blit, its contents become part of the destination surface, and you have to manipulate the destination surface to erase whatever was blit from the source surface.

擦除目标表面的一种方法是将背景表面 blit 到其上.背景表面是目标表面没有任何类似文本或精灵的样子.另一种方法是用纯色填充表面:

One way to erase the destination surface is to blit a background surface onto it. The background surface is what the destination surface would look like without anything like text or sprites on it. Another way is to fill the surface with a solid color:

# pygame initialization goes here

screen = pygame.display.get_surface()
font = pygame.font.Font(None, 40)

font_surface = font.render("original", True, pygame.Color("white"));
screen.blit(surface, (0, 0))

screen.fill(pygame.Color("black")) # erases the entire screen surface
font_surface = font.render("edited", True, pygame.Color("white"));
screen.blit(surface, (0, 0))

这篇关于如何删除/替换pygame中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆