Blitting text in pygame

天涯浪子 提交于 2019-12-12 04:36:03

问题


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

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

so how can i make it so that it splits the text from the file and blits each score 100 pixels down?

Thank You

-Christian Careaga


回答1:


Here's an example of multiline text https://stackoverflow.com/a/15516132/341744




回答2:


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



回答3:


if your score is in a file which suppose looks like this:


score1 - 10

score2 - 23

score3 - 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))


来源:https://stackoverflow.com/questions/17181813/blitting-text-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!