简体   繁体   English

pygame中的文字

[英]Blitting text in pygame

Im making a game to practice mygame and im creating a highscore screen but i cant seem to properly blit the text how i want 我正在制作一个游戏来练习mygame并创建了高分屏幕,但我似乎无法正确地改变文本的显示方式

here is the highscore method 这是高分方法

def high_screen(self):
    screen.blit(background,(0,0))
    myfont = pygame.font.SysFont("impact", 20)
    scorefile = open('highscores.txt', 'r')
    highscores = scorefile.read()
    label = myfont.render((highscores), 1, (0,0,0))
    screen.blit(label, (0, 0))

    self.back = pygame.image.load('resources/screen/back.png')
    self.back_r = self.back.get_bounding_rect()
    self.back_r.x,self.back_r.y = (100,600)
    screen.blit(self.back,(100, 600))
    screen.blit(self.player,(self.mouse_pos))
    if self.player_r.colliderect(self.back_r)and pygame.mouse.get_pressed()[0]:
        self.state = 1

this ggets the highscores from a .txt file and blits them but it blits them on one line when i want each score blitted about 100 pixels down from the one above it 这会从.txt文件中获取高分,并对其进行blit处理,但是当我希望将每一个乐谱从其上方的bs中向下偏移约100像素时,它将bs排成一行

so how can i make it so that it splits the text from the file and blits each score 100 pixels down? 所以我该怎么做,以便它从文件中拆分文本并将每个分数降低100像素?

Thank You 谢谢

-Christian Careaga -克里斯蒂安·卡雷加(Christian Careaga)

Here's an example of multiline text https://stackoverflow.com/a/15516132/341744 这是多行文字的示例https://stackoverflow.com/a/15516132/341744

多行文字

You could make a file for each score and then simply do something like: 您可以为每个分数制作一个文件,然后只需执行以下操作:

screen.blit(label1, (0, 0))
screen.blit(labe2, (0, 100))
screen.blit(labe3, (0, 100))
#etc

if your score is in a file which suppose looks like this: 如果您的分数在一个看起来像这样的文件中:


score1 - 10 得分1-10

score2 - 23 得分2-23

score3 - 34 得分3-34


then you can use this to separate the scores into different texts 然后您可以使用它将乐谱分成不同的文本

scorelist = [text.split('\n') for text in open('score.txt','r')]
##when you read the file from python you will get this string : score1 - 10\nscore2 - 23\nscore3 - 34

then to bit this onto a surface, use this code: 然后将此代码咬到表面上,请使用以下代码:

for i in enumerate(scorelist,1):
    surface.blit(i[1],(100,100+i[0]*100))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM