问题
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