python 一次显示一个字符

标签 python pygame

所以我正在重新创建口袋妖怪黄色的一部分(试图使其尽可能接近原始版本)并且现在两天我正在寻找一种智能且有效的方法来渲染和显示一个字符的字符串文本框中的时间,就像口袋妖怪游戏一样!(顺便说一句,我正在使用 pygame 和 python)。有谁知道有什么方法可以实现这一目标吗?我尝试了很多方法,但每次渲染一个角色时,它们之间总是没有足够的空间。 抱歉问了这么长的问题! 干杯, 亚历克斯

(编辑)感谢大家的关注!!! 我不确定我是否知道在这里显示我的代码的正确方法,如果我应该 将其复制粘贴到此处或上传到保管箱或其他地方..

(edit2) 只是为了澄清一下,我使用大小为 28 的字体,所以我现在尝试渲染字符的方式是创建一个列表,其中每个元素都具有格式 (character_to_render,x_pos_to_render,y_pos_to_render) 。下一个字符将为 (character2_to_render,x_pos_to_render + 28,y_pos_to_render)。但以这种方式解决问题,某些字符之间的空间不足,而另一些字符则没有问题。

(编辑 3):感谢大家的回答!仔细观察模拟器后,我注意到渲染字符之间的间距不足也很明显!所以我会在我的项目中忽略这个问题!干杯,祝你有美好的一天!

最佳答案

好的,这是我迄今为止想到的最佳解决方案。

您希望能够显示一个字符串,但您只想一次显示一个字符。对于字符串,您可以执行类似 string[0:len(string)] 的操作,它将返回整个字符串。所以我在想什么,如果我错了,请纠正我,但假设你将 FPS 降低了几秒钟,或者如果你不想这样做,因为你仍然想接受用户输入以跳过文本。

因此,您有 while 循环,并检查文本是否正在显示。如果是,您想要向屏幕上显示的字符串添加一个新字母。我建议对屏幕上显示的文本使用一个类。

surf = pygame.Surface(80, 80)
Text = TextBoxString("Hello World")
font = pygame.font.SysFont("Arial", 18)
while true:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        elif event.type == MOUSEBUTTONUP:
            Text.showAll()
    surf.fill((0,0,0))
    text = font.render(Text.currentString, (0,0,0))
    surf.blit(text, (0,0))
    Text.addOn()


class TextBoxString:
    def __init__(self, string):
        #string that you will be dealing with
        self.totalString = string
        self.currentString = string[0]
        #how many characters you want shown to the screen
        self.length = 0
        #this means that every four times through your 
        #while loop a new char is displayed
        self.speed = 4
    def addOn(self) #adds one to the loop num and then checks if the loop num equals the speed
        self.loopNum += 1
        if self.loopNum == self.speed:
            self.length += 1
            self.loopNum=0
        self.currentString = totalString[0: self.length]
    def showAll(self):
        self.length = len(self.totalString)
        self.currentString = [0: self.length]

关于python 一次显示一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221611/

相关文章:

python - 是否可以为 pygame.Rect 对象创建一个新的可分配属性?

python - 从 html 文本中提取标签信息

python - Numpy:沿特定 Axis 的外和

python - 如何在 Python 上安装 "pip"的 psycopg2?

python - 读取 tfrecord : DecodeError: Error parsing message

python - 在 pygame 上更新和显示图像

python - 在python中复制文件名中包含特定字符串的文件

python - 用pygame制作的游戏可以提交到steam吗?

python - 类型错误 : 'pygame.Surface' object is not callable and pygame window crashes

python - 我可以在模块中使用程序中创建的对象吗?