Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    save high scores?


    is there anyway to save highscores to a game for example after you finish? i mean so if you set a score for a game, then exit out and come back in the score is still there. i know when you exit out of python all of the variables are reset (at least to my knowledge) but is there anyway around this?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    You can't save the variables the way you want to. I would suggest saving them in a text file, if you want to make it look somewhat more professional, you can store them in .tmp files in the temporary files directory on the users computer.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10
    you can create a datastructure to hold the variable and pickle it . Unpickle them next time the game is played.

    Comments on this post

    • netytan agrees
    • CyBerHigh agrees
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ahh, I didn't even think of that. However it might be easier to store it in a file. But it's up to him.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Originally Posted by †Yegg†
    However it might be easier to store it in a file.
    Eh, if you pickle something, it is stored in a file

    Regards, mawe
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    text file is probably the easiest, because it is not hard to open one t the start og the game adn close it at the end.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'd also go with Pickel simply because if you more than one value its the easiest solusion; you don't need to parse the file yourself and you can store any Python object .

    http://python.org/doc/2.4.1/lib/module-pickle.html

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64

    Red face


    Originally Posted by wx_uab
    you can create a datastructure to hold the variable and pickle it . Unpickle them next time the game is played.
    How about some actual code?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    pretty straight forward. Just use pickle to store the veribles or objects. The nicest thing abotu pickle is that you can save anymost everything in python with it. Including veribles, lists, dictionarys, truples, classes, and everything else you can think of

    some simple use of it is like:

    Code:
    >>> import pickle
    >>> a_List = [42, 52, 5552, 002, 332, "hee"]
    >>>print a_List
    >>> file = open("hmm", "w")
    >>> pickle.dump(a_List, file)
    >>> file.close()
    >>> file = open("hmm", "r")
    >>> a_List2 = pickle.load(file)
    >>> file.close()
    >>>print a_List2
    so its quit simple. bascily keep with that syntax. And to not store a dump to a file you can use dumps() witch will just return a string of the pickled object. Then to load that make sure you use loads() instead of load()

    i would recommend cPickle tho if you are looking for more speed, you use it EXACTLY the same way and it is like 1000x faster. It is just pickle writen in c
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    it can save classes how would that help?

    It is already in the source code
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    well, i guess it doesn't exactly save the class but the object or instance of the class. That way you don't lose veribles and such.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    oh okay so it saves the instance of the class and then you can run it again and you have the instance again cool
  24. #13
  25. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    alright i'm not really understanding this and its because i have no good background of programming... now what does the ("hmm","w") and ("hmm","r") do in the file = open?
    here it all is again.
    Code:
    >>> import pickle
    >>> a_List = [42, 52, 5552, 002, 332, "hee"]
    >>>print a_List
    >>> file = open("hmm", "w")
    >>> pickle.dump(a_List, file)
    >>> file.close()
    >>> file = open("hmm", "r")
    >>> a_List2 = pickle.load(file)
    >>> file.close()
    >>>print a_List2
  26. #14
  27. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Originally Posted by lw22
    alright i'm not really understanding this and its because i have no good background of programming... now what does the ("hmm","w") and ("hmm","r") do in the file = open?
    here it all is again.
    Code:
    >>> import pickle
    >>> a_List = [42, 52, 5552, 002, 332, "hee"]
    >>>print a_List
    >>> file = open("hmm", "w")
    >>> pickle.dump(a_List, file)
    >>> file.close()
    >>> file = open("hmm", "r")
    >>> a_List2 = pickle.load(file)
    >>> file.close()
    >>>print a_List2
    "hmm" is the filename, you could replace it with something more serious like "a_list.dat".
  28. #15
  29. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    whats the "w" and "r" mean then... i've tried changeing those but then it wouldn't work.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo