I have built a game that logs and saves the users score, using pickle, to a text file. When their lives are used up, they enter their name and their name and score are saved to a text file. Currently, if the "High Scores" section is selected on the main menu the high scores are simply printed in the python shell (or CMD if they're using that). I would like to create a separate window for just displaying the high scores. The window would simply display the scores and would refresh every time it is opened.
Currently I have the code to load the pickled file and create a new window. If I enter static text it works fine but when I try to display the text file contents I get the following error:
Traceback (most recent call last): File "C:\LearnArabic\Program\Test1.py", line 22, in textsurface = myfont.render(high_scores, False, (0, 0, 0)) TypeError: text must be a unicode or bytes
Here is my code:
import pygame
from operator import itemgetter
import pickle
pygame.font.init()
high_scores = []
with open("C:\\LearnArabic\\HighScores\\HighScores.txt", 'rb') as f:
high_scores = pickle.load(f)
#Background color
background_color = (255,255,255)
(width, height) = (400, 500)
HighScoreScreen = pygame.display.set_mode((width, height))
pygame.display.set_caption('High Scores')
HighScoreScreen.fill(background_color)
#Displaying text on window
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(high_scores, False, (0, 0, 0))
HighScoreScreen.blit(textsurface,(0,0))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
running = False
Is there a different function from render that would allow me to display the results in a tabular form?
I'm relatively new to programming and am using python 3. Thanks for the help!
You could blit the highscores onto another surface and then blit this surf onto the screen. To blit the highscore list, use a for loop and enumerate the list, so that you can multiply the y-offset by
i
. To toggle the highscore surface, you can just add a variablehighscores_visible = False
and then dohighscores_visible = not highscores_visible
, and in the main loop checkif highscores_visible: # blit the surf
(press 'h' to update and toggle the highscore table in the example below). Of course you need to make sure that the names and highscores fit onto the surface.Regarding the
TypeError
, you can't pass a list tomyfont.render
only a string or byte string, so you have to convert the list, e.g.str(high_scores)
. However, if you just convert thehigh_scores
list into a string before you pass it, pygame will render the whole list as one line. You need to use a for loop if you want several lines of text.