我用pygame,easygui和shelve在python中制作了一个简单的赛车游戏。我完成了游戏,它从空闲和我的.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))
`
答案 0 :(得分: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)))