1 Replies - 6375 Views - Last Post: 26 January 2011 - 12:06 PM Rate Topic: -----

#1 Guest_John*


Scores with Python!

Posted 26 January 2011 - 12:03 PM

Hi I want to store all the scores in a .txt file or .dat file and when the person will go into my pygame score menu it will just show the scores from that .txt file. I have it working and im using a list with tuple [('John', 900)]. The problem is it wont save? It just stores the most recent score and when I close my game go read the text file it only has the most recent score saved. How would I be able to save the scores in the txt file. Not many people are going to play this its just so I can get better at programming. My code here just shows my functions for adding score into the txt file. Thanks!

def get_scores():
    scores = open('scoreboard.dat','r')
    print scores.read()

def add_score(HighScores):
    sf = open('scoreboard.dat', 'wb')

global HighScores
HighScores = [('Paul',20)]
global HighScore
HighScore = (HighScoresName,Score)
print HighScores

Is This A Good Question/Topic? 0

Replies To: Scores with Python!

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 827
  • View blog
  • Posts: 2,231
  • Joined: 23-December 08

Re: Scores with Python!

Posted 26 January 2011 - 12:06 PM

change this line from sf = open('scoreboard.dat', 'wb')

to sf = open('scoreboard.dat', 'a')

The 'w' means write... or truthfully overwrite. The 'a' means to append new data to the end.

What I would personally do is read all the highscores into a list. Then, when it's time to close the game, write all the highscores back to the file. Read from the file once, write to the file once. This simplifies it, but that's just my opinion. If you do like that, you will be using 'w' as opposed to 'a'.

One more thing:
Global variables are bad, don't use them :)

This post has been edited by atraub: 26 January 2011 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1