Pygame中的整洁打印说明

时间:2014-04-30 17:19:13

标签: python-2.7 fonts pygame

我只是想在游戏开始时使用pygame显示说明。当然,我可以通过并指定每个单独行的位置,但我正在尝试有效地执行此操作。每次我搜索一个简单的方法,有人只做一行,所以他们不需要改变y位置。我的问题是尽管我尝试过,每一行都在同一个y位置打印。我打印了n以确保它正在改变。我不明白为什么这个职位不会改变它。另外,我在我的centerx位置添加了-225,因为由于某种原因,该函数使该值偏离中心。我可能错了,我不确定。任何帮助表示赞赏。

textlist = [instructionstitle, line1, line2, line3, line4, line5, line6]
for i in textlist:
    textpos = i.get_rect()
    textpos.centerx = background.get_rect().centerx - 225
    n = textlist.index(i)
    textpos.centery = 25*n + 25
    def show(i):
        screen.blit(i, [textpos.centerx, textpos.centery])
def instructions():
    screen.blit(background, [0, 0])
    for i in textlist:
        show(i)
    pygame.display.flip()
    time.sleep(10)

1 个答案:

答案 0 :(得分:0)

你的show()函数在for循环中,原因我不明白。通过这样做,你在textpos变量中,在for循环的最后一次运行中设置为textpos的值。所以,是的,当然,y位置不会更新!你不需要两个for循环,一个用于设置textpos,另一个用于调用show()。您需要的是一个简单的重新排列,以便正确更新您的textpos变量。

def show(i):    
    textpos = i.get_rect()    
    textpos.centerx = background.get_rect().centerx - 255
    n = textlist.index(i)
    textpos.centery = 25*n + 25
    screen.blit(i, [textpos.centerx, textpos.centery]) 

instructions()函数可以按原样工作。在这里,我假设您的文本列表包含使用pygame Font模块呈现的表面,即类似这样的

fntInst = pygame.font.Font(None, 50) instructionstitle = fntInst.render('Instructions', False, black)

而且,据我所知,中心位置没有移动。您为文本曲面设置的位置是从曲面的左上角渲染的位置。因此,如果您希望文本位于中心,则需要做的是获取使用Font生成的曲面大小。您可以执行fntInst.size('Instructions')之类的操作,并从centerx值中减去此大小。固定的255不会使所有线都居中,因为每条线的长度会有所不同,而是所有的线都会对齐。