如何在pygame中为RPG制作文本框/状态框?
我需要为我的角色扮演游戏(RPG)创建一个文本框、消息框或状态框等。我已经做了一个文本框的类,但它似乎不太好用。虽然它能显示表面,但却不显示任何文字。
代码如下:
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)
1 个回答
5
嗯,抱歉。没事了。我自己找到了问题。之前我写的是:
self.rect.center = (0,0)
其实应该是
self.rect.top = 0 ; self.rect.left = 0
不过还是谢谢你。:D