【问题标题】:How to make a TextBox/Status Box in pygame for an rpg?如何在 pygame 中为 rpg 制作文本框/状态框?
【发布时间】:2011-06-19 10:15:44
【问题描述】:

我需要为我的 rpg 创建一个 TextBox / MessageBox /StatusBox 等。我做了一个文本框 类,但它似乎不起作用。它会破坏表面,但不显示任何文本。

代码:

class TextBox(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.initFont()
        self.initImage()
        self.initGroup()
        self.setText(['a','b'])

    def initFont(self):
        pygame.font.init()
        self.font = pygame.font.Font(None,3)

    def initImage(self):
        self.image = pygame.Surface((200,80))
        self.image.fill((255,255,255))
        self.rect = self.image.get_rect()
        self.rect.center = (0,0)        

    def setText(self,text):
        tmp = pygame.display.get_surface()
        x_pos = self.rect.left+5
        y_pos = self.rect.top+5

        for t in text:
            x = self.font.render(t,False,(0,0,0))
            tmp.blit(x,(x_pos,y_pos))
            x_pos += 10

            if (x_pos > self.image.get_width()-5):
                x_pos = self.rect.left+5
                y_pos += 10

    def initGroup(self):
        self.group = pygame.sprite.GroupSingle()
        self.group.add(self)

【问题讨论】:

  • 如果您找到自己问题的答案,您应该将其作为答案发布,以便您可以将问题标记为已回答。我认为您必须在提出问题后等待 24 小时才能执行此操作。
  • 很抱歉。我打算这样做,但不得不等待 24 小时,所以我进行了编辑。现在完成了。
  • 酷。你也可以勾选你自己的问题。 ;)
  • 哈哈!我做到了!没有任何痛苦就获得了额外的声誉! :D
  • :D 好吧,不要认为它是“作弊”。它是这样设计的。即使您没有帮助其他人,您仍然在问答数据库中添加了一个带有有效解决方案的新条目,因此您应该得到一点声誉。

标签: python text pygame


【解决方案1】:

嗯,对不起。没关系。我自己发现了问题。我写道:

 self.rect.center = (0,0)

它应该在哪里

 self.rect.top = 0 ; self.rect.left = 0

无论如何,谢谢。 :D

【讨论】:

  • 使用:self.rect.topleft = (0, 0)
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
相关资源
最近更新 更多