5 Replies - 1496 Views - Last Post: 26 January 2011 - 02:22 PM Rate Topic: -----

#1 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Help! With Getting Data!

Posted 25 January 2011 - 06:27 PM

Hey, I have this problem I have encountered. So I have a HighScore List and let me just show you the code first and then explain it.
def GOMenu():
    
    font1 = pygame.font.Font(None, 130)
    font2 = pygame.font.Font(None, 50)
    text2 = font1.render("GAME OVER", True, (255,0,0))
    text3 = font2.render(str(Score), True, (255,255,255))
    text4 = font2.render("Final Score:", True, (255,255,255))
    screen = pygame.display.set_mode((800,600),0,32)
    screen.blit(text2, (120,200))
    screen.blit(text4, (250, 300))
    screen.blit(text3, (500, 300))
    pygame.display.update()
    pygame.time.delay(5000)
    
     

    class Name(wx.Frame): 
        def __init__(self,parent,id): 

            box=wx.TextEntryDialog(None,'What is your name?','High Score Names','Enter Name Here')#Gives you a dialog box, the second parameter is the quesion, the third is the title of the box, the fourth is the default text#
            if box.ShowModal()==wx.ID_OK: #So if the user clicks cancel then it wont store in the variable otherwise if you didnt you would get it stored#
                HighScoreName=box.GetValue()#So if its ok then it will store it in answer
                global HighScores
                HighScores = {'Paul':9000000}
                HighScores[HighScoreName] = Score
                print HighScores

                

    if __name__=='__main__':       
         app=wx.PySimpleApp() 
         frame=Name(parent=None,id=-1) 
         app.MainLoop()






def ScoreMenu():
    font = pygame.font.Font(None, 50)
    text = font.render(HighScores, True, (255,255,255))
    screen = pygame.display.set_mode((800,600),0,32)

    pygame.display.update()


So I have a GOMenu function which is the screen you see when you die. Then I used wxpython to make a window so I can get the players name and store it with the persons score in a dictionary. Now in my main menu I have "Start Game, Scores<< When the person goes to that scores i want it to show a list of all the high scores in the past 30 days which I know how to acomplish that but my problem is I cannot get the data from the Class which you have to make with wxpython into this function. I want to get the HighScores dicitonary from GOMenu() so I can use it for my display of scores. Thanks! Ill be happy to answer any questions to help solve this problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Help! With Getting Data!

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Help! With Getting Data!

Posted 25 January 2011 - 06:58 PM

A classic scoping issue.

The way I would solve this is by creating a separate class whose sole job is to be keeper of the high scores. This class would load data from a file (perhaps a pickle?) when instantiated, report the data in a dictionary/ordered list, and update the score file when saved.

Each time you want to access the high scores, you would simply need to instantiate the class and call the methods.

It might look something like this:
import os
import pickle

class ScoreBoard:
    def __init__(self):
        if os.exists('scoreboard.dat'):
            sf = open('scoreboard.dat', 'r')
            self.scores = pickle.load(sf)
            sf.close()
        else:
            # My implementation is a list of tuples
            self.scores = []

    def get_scores(self):
        return self.scores

    def add_score(self, name, score):
        self.scores.append((name, score))
        sf = open('scoreboard.dat', 'w')
        pickle.dump(self.scores, sf)
        sf.close()

...

sb = ScoreBoard()
scores = sb.get_scores()
for score in scores:
    print score[0], score[1]

...

sb = ScoreBoard()
sb.add_score('Motoma', 9001)


Was This Post Helpful? 2
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Help! With Getting Data!

Posted 25 January 2011 - 07:10 PM

Is the problem with the highscore table itself, as Motoma suggested or with the wxPython?

    if __name__=='__main__':       
         app=wx.PySimpleApp() 
         frame=Name(parent=None,id=-1) 
         app.MainLoop()



Your application is PySimpleApp() and you declared the variable "frame" but you didn't connect the two.

You have a custom frame (an instance of wx.Frame):

    class Name(wx.Frame): 
        def __init__(self,parent,id): 

            box=wx.TextEntryDialog(None,'What is your name?','High Score Names','Enter Name Here')#Gives you a dialog box, the second parameter is the quesion, the third is the title of the box, the fourth is the default text#
            if box.ShowModal()==wx.ID_OK: #So if the user clicks cancel then it wont store in the variable otherwise if you didnt you would get it stored#
                HighScoreName=box.GetValue()#So if its ok then it will store it in answer
                global HighScores
                HighScores = {'Paul':9000000}
                HighScores[HighScoreName] = Score
                print HighScores


You could use a custom application (an instance of wxApp):

class MyApplication(wx.wxApp): # or wx.PySimpleApp it will inherit the same basic behaviour.
    def __init__(self):
        frame = Name(parent=None,id=-1)
        frame.Show()


and then run the application:

    if __name__=='__main__':       
         app = myApplication()
         app.MainLoop()


Admittedly my wxPython is rusty but I believe it is correct. I've never used it within pygame before, can you be a bit more specific about the problem you are having?


Edit: After some research it seems you can declare the frame separately, it is just not how I learnt it. You definitely need to .Show() the frame though.

This post has been edited by Simown: 25 January 2011 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Re: Help! With Getting Data!

Posted 25 January 2011 - 08:01 PM

Motoma's way will work great and I can use it but let me try to explain what the problem is. I want to know this so if I ever encounter his again i know what to do. So I have the GOMenu function which gives me a pygame window that says prints "Game Over" and the final score. Then i use pygame.time.wait() to wait 5 seconds and then over the window a WxPython window shows up and asks the user for his name. Then i link HighScoreName=box.get_value() to store his name into that variable. Then I make a dictionary "HighScores" storing the names as the keys and the score as the value. Now ScoreMenu() is just a function to list all the scores in a pygame window. Now I need to get the HighScore dictionary from the GOMenu() in the class for wxpython. I tried making the HighScore dictionary global but it said its not defined. Now im wondering how I can get data from HighScore variable which is a dictionary to go into my function ScoreMenu(). Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Help! With Getting Data!

Posted 26 January 2011 - 12:46 PM

You have a couple of options:
1. Have HighScores be a global variable:

import wx
import pygame

HighScores = {'Motoma' : 42}

def GOMenu():
...



2. Pass the HighScore list around to the different functions that need it:
def ScoreBoard(hs):
    font = pygame.font.Font(None, 50)
    text = font.render(hs, True, (255,255,255))
    screen = pygame.display.set_mode((800,600),0,32)
    pygame.display.update()

...
    # Somewhere in your code
    ScoreBoard(HighScores)


Was This Post Helpful? 0
  • +
  • -

#6 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Re: Help! With Getting Data!

Posted 26 January 2011 - 02:22 PM

Thanks man! You really helped me alot! This site is great.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1