【问题标题】:Shelve causes pygame fatal error?搁置导致pygame致命错误?
【发布时间】:2013-05-27 15:29:37
【问题描述】:

我用 python 制作了一个简单的赛车游戏,里面有 pygame、easygui 和 shelve。我完成了游戏,它在空闲和我的 .desktop 文件中都运行良好。之后我决定尝试添加一个高分系统。系统在空闲时工作得很好,但是从 .desktop 文件中我得到一个错误“致命的 python 错误:(pygame parachute)分段错误。”我真的很想将高分系统留在游戏中,因此我们将不胜感激。`

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

`

【问题讨论】:

    标签: python pygame shelve easygui


    【解决方案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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多