0

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))

`

4

1 回答 1

1

我在您的代码中看到几个小错误,但我不确定这是否是段错误的原因,试试这个:

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)))
于 2013-05-27T17:05:24.693 回答