Question

I have made a simple racing game in python with pygame, easygui, and shelve. I finished the game and it worked great both from idle and from my .desktop file. After that i decided to attempt to add a highscore system. The system works great from idle, but from the .desktop file i get an error "fatal python error: (pygame parachute) segmentation fault." I would really like to leave the highscore system in the game so any help would be appreciated.`

if hero.colliderect(finishline):
    starttimer=0
    running=0
    d=shelve.open('highscores.txt')
    highscore=d['highscore']
    if time<highscore:
        d=shelve.open('highscores.txt')
        d['highscore']=time
        player=easygui.enterbox(msg=("Congratulations you have set the new highscore of "+str(time)+ " please enter your name"))
        d['player']=player
        d.close
    if time>highscore:
        d=shelve.open('highscores.txt')
        player=d['player']
        highscore=d['highscore']
        d.close
        easygui.msgbox(msg=("Congratulations you have finished with a time of "+str(time)+" The highscore is "+str(highscore)+ "set by "+player))

`

Was it helpful?

Solution

I see a couple of small errors in your code but I am not sure wether this is the cause of the segfault, try this:

from contextlib import closing

if hero.colliderect(finishline):
    starttimer = running = 0
    with closing(shelve.open('highscores.txt')) as d:
        highscore = d['highscore']
        if time < highscore:
            d['highscore'] = time
            player = easygui.enterbox(msg=("Congratulations you have set a new highscore of {0}. Please enter your name: ".format(time)))
            d['player'] = player
        else:
            player = d['player']
            easygui.msgbox(msg=("Congratulations you have finished with a time of {0}. The highscore is {1} set by {2}".format(time, highscore, player)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top