我做了一个功能齐全的主菜单,一个按钮带你去游戏,另一个按钮带你去指令页。在
然而,在我的说明页上,我只能写一句话。如果我添加多行,它显示在中间的方块。在
我想实现的是在屏幕上写一个多行的段落来显示一些指令、游戏信息等
这是我目前掌握的相关代码:
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def ParagraphText(text, font):
ParagraphSize = (600,500)
FontSize = font.get_height()
ParagraphSurf = pygame.Surface(ParagraphSize)
ParagraphSurf.fill(WHITE)
ParagraphSurf.set_colorkey(WHITE)
SplitLines = text.splitlines()
CentreText = (ParagraphSize[1] - len(SplitLines)*(FontSize + 1)//2)
for idx, line in enumerate(SplitLines):
currentTextline = font.render(text, False, (0, 0, 0))
currentPostion = (0, idx * FontSize + CentreText)
ParagraphSurf.blit(currentTextline, currentPostion)
return ParagraphSurf, ParagraphSize
def Instructions():
paragraph = """Your are the last surviving rhino. Your horn
is worth millions! Right now you are trapped in a zoo and
waiting to be slaughtered for your horn. But you can't give
up! Escape from the tribesmen and zoo security so you can
enjoy your life as a free being once again"""
screen.blit(pygame.image.load("background0.jpg").convert(), (0,0))
InstructionsFont = pygame.font.SysFont("elephant",15)
TextSurf, TextRect = text_objects("Instructions", InstructionsFont)
TextRect.center = ((screen_width/2),(screen_height/6))
screen.blit(TextSurf, TextRect)
ParagraphText(paragraph,InstructionsFont)
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
Buttons("BACK",100,500,120,50,TURQUOISE,DARK_TURQUOISE,"back")
pygame.display.update()
clock.tick(15)
^{} 对象的^{} 方法只能blit单行文本,如documentation所述。在
要解决此问题,您必须调整
text_objects()
函数,将传入的文本字符串的每一行分别绘制到适当的位置:现在是时候实施这一点了:
如果要使文本中的每一行居中,请不要将
^{pr2}$currentPostion
的x
-坐标设置为0
,而是使用以下计算:或右对齐:
1)请注意,在呈现一行时,将
antialias
参数设置为true
,可能会因为段落表面的设置颜色键而产生不良影响!我希望这对你有帮助:)
相关问题 更多 >
编程相关推荐