Highscore

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MaxLindquist
    New Member
    • Oct 2006
    • 24

    Highscore

    Hi! I want to make a highscore for a game i am making. How might i do that?

    Thanks
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by MaxLindquist
    Hi! I want to make a highscore for a game i am making. How might i do that?

    Thanks
    You might do that many ways. Tell us more about what "make a high score" means (do you mean make a way to remember the top 5 high scores over time?).

    Comment

    • MaxLindquist
      New Member
      • Oct 2006
      • 24

      #3
      Yes.... I mean.... i want to make my program save the top 10 scores and names in some way....

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by MaxLindquist
        Yes.... I mean.... i want to make my program save the top 10 scores and names in some way....
        Here, I use a list of tuples. This list can be sorted by score, then pickled to save it to disk. Pickle documentation is in section 3.14.3 of python 2.4 docs.

        >>> hiscores = [(300, "John")]
        >>> score = 200
        >>> player = "Jane"
        >>> hiscores.append ((score, player))
        >>> print hiscores
        [(300, 'John'), (200, 'Jane')]
        >>> hiscores.sort()
        >>> print hiscores
        [(200, 'Jane'), (300, 'John')]


        >>> import pickle

        >>> scores = open("scores", "w")
        >>> pickle.dump(his cores[-10:], scores)
        >>> scores.close()

        >>> scores = open("scores", "r")
        >>> oldscores = pickle.load(sco res)
        >>> scores.close()
        >>> print oldscores
        [(200, 'Jane'), (300, 'John')]
        >>>

        Comment

        • MaxLindquist
          New Member
          • Oct 2006
          • 24

          #5
          Thanks! What does pickle do?

          Comment

          • MaxLindquist
            New Member
            • Oct 2006
            • 24

            #6
            OK.... i think i get it now... thanks

            Comment

            • MaxLindquist
              New Member
              • Oct 2006
              • 24

              #7
              How would you do it if you used a lists? Like:
              score=list()
              name=list()

              in some way?

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by MaxLindquist
                Thanks! What does pickle do?
                Like the name suggests, it preserves you data for later (on the disk).

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by MaxLindquist
                  How would you do it if you used a lists? Like:
                  score=list()
                  name=list()

                  in some way?
                  Yes;
                  these are the same:
                  score = [] # an empty list
                  score = list() # list constructor called with no arguments

                  Comment

                  • MaxLindquist
                    New Member
                    • Oct 2006
                    • 24

                    #10
                    Originally posted by bartonc
                    Here, I use a list of tuples. This list can be sorted by score, then pickled to save it to disk. Pickle documentation is in section 3.14.3 of python 2.4 docs.

                    >>> hiscores = [(300, "John")]
                    >>> score = 200
                    >>> player = "Jane"
                    >>> hiscores.append ((score, player))
                    >>> print hiscores
                    [(300, 'John'), (200, 'Jane')]
                    >>> hiscores.sort()
                    >>> print hiscores
                    [(200, 'Jane'), (300, 'John')]


                    >>> import pickle

                    >>> scores = open("scores", "w")
                    >>> pickle.dump(his cores[-10:], scores)
                    >>> scores.close()

                    >>> scores = open("scores", "r")
                    >>> oldscores = pickle.load(sco res)
                    >>> scores.close()
                    >>> print oldscores
                    [(200, 'Jane'), (300, 'John')]
                    >>>


                    I tried this code... I created at textfile, called "score.txt" next to my program, and i changed score and player to raw_inputs. But if i run it a second time, the information from previous time isn't saved.

                    Comment

                    • MaxLindquist
                      New Member
                      • Oct 2006
                      • 24

                      #11
                      This is the code:

                      hiscores = [(300, "John")]
                      score=raw_input ("Score?")
                      player=raw_inpu t("Name?")
                      hiscores.append ((score, player))
                      print hiscores

                      hiscores.sort()
                      print hiscores



                      import pickle

                      scores=open("sc ores", "w")
                      pickle.dump(his cores[-10:], scores)
                      scores.close()

                      scores = open("scores", "r")
                      oldscores = pickle.load(sco res)
                      scores.close()
                      print oldscores




                      For some reason this doesn't work.

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by MaxLindquist
                        This is the code:

                        For some reason this doesn't work.
                        Hi, Max. Will you post all of you code please. Only this time, read "Posting Guidelines" in the panel on the right (while you are posting) or in the very first thread on this forum to learn how to USE CODE TAGS so that you post looks like this:


                        Code:
                        hiscores = [(300, "John")]
                        score=raw_input("Score?")
                        player=raw_input("Name?")
                        hiscores.append((score, player))
                        print hiscores
                        
                        hiscores.sort()
                        print hiscores
                        
                        
                        
                        import pickle
                        
                        scores=open("scores", "w")
                        pickle.dump(hiscores[-10:], scores)
                        scores.close()
                        
                        scores = open("scores", "r")
                        oldscores = pickle.load(scores)
                        scores.close()
                        print oldscores
                        Thank you,
                        Barton

                        Comment

                        • MaxLindquist
                          New Member
                          • Oct 2006
                          • 24

                          #13
                          Actually that was all of my code... I just want that part of the program to work, then i'm gonna put it in my big program, the game i am making...

                          But this is my code:

                          Code:
                          hiscores = [(300, "John")]
                          score=raw_input("Score?")
                          player=raw_input("Name?")
                          hiscores.append((score, player))
                          print hiscores
                          
                          hiscores.sort()
                          print hiscores
                          
                          
                          
                          import pickle
                          
                          scores=open("scores", "w")
                          pickle.dump(hiscores[-10:], scores)
                          scores.close()
                          
                          scores = open("scores", "r")
                          oldscores = pickle.load(scores)
                          scores.close()
                          print oldscores

                          It is almost the same code you wrote. I just changed in the beginning. The problem is that this doesn't work... and i don't understand why...

                          Thank you very much for you help!

                          /Max

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Thanks for learning to use code tags! This helps a lot. Now I see what you have done because I only look at code in code tags.
                            What you need to do AFTER you have your pickle file is READ it first.


                            Code:
                            import pickle
                            
                            scores = open("scores", "r")
                            # READ the pickled data then add to it.
                            hiscores = pickle.load(scores)
                            scores.close()
                            print hiscores
                            
                            # Use int() to get a number from raw_input()
                            score=int(raw_input("Score?"))
                            
                            player=raw_input("Name?")
                            hiscores.append((score, player))
                            print hiscores
                            
                            hiscores.sort()
                            print hiscores
                            
                            scores=open("scores", "w")
                            pickle.dump(hiscores[-10:], scores)
                            scores.close()
                            
                            scores = open("scores", "r")
                            oldscores = pickle.load(scores)
                            scores.close()
                            print oldscores
                            Hope that makes sense,
                            Barton


                            Originally posted by MaxLindquist
                            Actually that was all of my code... I just want that part of the program to work, then i'm gonna put it in my big program, the game i am making...

                            But this is my code:

                            It is almost the same code you wrote. I just changed in the beginning. The problem is that this doesn't work... and i don't understand why...

                            Thank you very much for you help!

                            /Max

                            Comment

                            • bartonc
                              Recognized Expert Expert
                              • Sep 2006
                              • 6478

                              #15
                              Originally posted by MaxLindquist
                              I tried this code... I created at textfile, called "score.txt" next to my program, and i changed score and player to raw_inputs. But if i run it a second time, the information from previous time isn't saved.

                              The file is created by open(). Whatever name is in ("filename", "w") is the file that will be used. So if you want, you can use open("score.txt "), but the data in the file won't make much sense. The "r" and "w" are read and write mode, respectively.

                              Comment

                              Working...