I'm having quite a silly problem here: after finishing a game, I
wanted to add a highscore table. I already have all the code for this
table, which properly handles a certain number of sorted entries,
saves and loads data to a file and does all that a classic highscore
table should do. I'm still learning Python, so the code may not be too
pythonic, I fear... but I leave it here just in case someone is even
more lost than me :)

It's quite easy to use, although you have to draw it yourself.  You
create an instance, specifying the number of entries in the table.
Then it attempts to load an existing table from disk, creating a table
full of dummy records if no file is found. When the game ends,
check_score should be called to check if the user's score is high
enough to enter the table...

...and here's where I have my problem. If it is a new record, the user
should enter her name. But how? I was playing with a text widget like
those in text_input.py, with its layout and caret for text input...
But I just can't control it properly. I don't get it, it is something
mystical for me, that 'caret' thing. I can't find what I'm doing
wrong, but nothing appears on screen when I type. So, my question is:
does anyone know a self-contained solution to handle text input for
something as simple as registering your name? Otherwise I think I'll
create an object with its own keyboard handlers and control everything
myself :(


Here's the code for the high score table, just in case someone needs
it:

class HighScoreTable():
    """ Manage a highscore table with max_elems. It is stored in a
file called highscore.txt.
    If the file does not exist, a new table is created with dummy
records. """

    def __init__ (self, max_elems=5):
        self.max_elems = max_elems
        self.highscore_list = []
        self.load()

    def check_score(self, score):
        """ Check if score is high enough to appear in the table """
        if score > self.highscore_list[-1][0]:
            return True
        else:
            return False

    def insert_record(self, record):
        """ Insert a new record, where record is a tuple (score, name)
"""
        self.highscore_list.append(record)
        self.highscore_list.sort(key = lambda x: x[0], reverse = True)
        if len(self.highscore_list) > self.max_elems:
            self.highscore_list.pop()
        self.save()

    def save(self):
        """ Save scores to a text file """
        file = open('highscores.txt', 'w')
        for score in self.highscore_list:
            file.writelines( str(score[0]) + " " + score[1])

    def load(self):
        """ Load scores from a text files, if it exists, or create a
table full of dummy records """
        file = open('highscores.txt',"r");
        if file != None:
            lines = file.readlines();
            for i in lines:
                thisline = i.split(" ");
                score = int(thisline[0])
                name = thisline[1]
                self.insert_record((score, name))
           self.max_elems = len(self.highscore_list)
        else:
            for i in range(0, self.max_elems):
                self.highscore_list.append((0, "Nobody"))
            self.save()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to