Question

First of all, I'm still relatively new to programming, so please go easy on me.

I'm programming an arcade-like game with Python2.7 and I want to implement a leaderboard that can store, say, 10 pairs of username and score achieved. To give you an idea, it should look more or less like this:

1. AAA 100
2. BBB 10
3. CCC 1

...And so on. This way, it should be able to keep track of 10 scores.

When a player finishes the game and tops a score on the leaderboard, he should be prompted to enter a name with a maximum of 15 characters and the score should be saved. Furthermore, the code for the game should allow the player to look at the leaderboard from the start menu, i.e. the highscores should be possible to visualize upon clicking on "Leaderboard".

I've tried searching for a way to do this, but unfortunately the results either covered online leaderboards, only saved the highest ever reached score or were too complicated for me as a programming beginner to grasp. I also can't be sure whether any of those possiblities would allow for reading of the leaderboard in the game itself.

I'd much appreciate if someone could lend me a hand with this issue.

(In case I wasn't clear enough (I was tired when writing this), my only problem is opening and reading from/writing to a different file containing the leaderboard and displaying it in the game. I do not, however, want it to appear in the Shell.)

Edit:

pygame.font.init()
font = pygame.font.Font(None, 30)
readfile = open("leaderboard.txt", "r")
text = font.render(readfile, True, (27,100,225))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
screen.blit(text, textRect)

As suggested I tried the render command, and so far I've come up with this, but I get an error message:

Traceback (most recent call last):
File "L:\game.py", line 97, in <module>
text = font.render(open("leaderboard.txt", "r"), True, (27,100,225))
TypeError: text must be a string or unicode
Was it helpful?

Solution

Create a surface - and then use the pygame.font Module - and specifically the render() method to draw text onto the surface.

Then place the surface onto the display as usual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top