2013-05-27 146 views
0

我已經用pygame,easygui和shelve在python中製作了一個簡單的賽車遊戲。我完成了遊戲,並且從閒置和從我的.desktop文件中運行都很好。之後,我決定嘗試添加高分系統。該系統在閒置狀態下工作良好,但從.desktop文件中,我收到了一個錯誤「致命的python錯誤:(pygame降落傘)分段錯誤。」我真的想留在遊戲中的排行榜系統,因此任何幫助將是appreciated.`Shelve導致pygame致命錯誤?

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

`

回答

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

謝謝這麼多即時通訊很高興有這個功能回到我的遊戲。 –