问题描述
嗨!我想为我正在制作的游戏制作一个高分。我怎么可能这样做?
谢谢
Hi! I want to make a highscore for a game i am making. How might i do that?
Thanks
推荐答案
你可能会做很多事。告诉我们更多关于获得高分的信息。意味着(你的意思是想办法记住前5个高分吗?)。
You might do that many ways. Tell us more about what "make a high score" means (do you mean make a way to remember the top 5 high scores over time?).
这里,我使用元组列表。此列表可以按分数排序,然后进行pickle以将其保存到磁盘。 Pickle文档在python 2.4 docs的3.14.3节中。
>>> hiscores = [(300," John")]
>>>得分= 200
>>> player =" Jane"
>>> hiscores.append((得分,玩家))
>>>打印hiscores
[(300,''John''),(200,''Jane'')]
>>> hiscores.sort()
>>>打印hiscores
[(200,''Jane''),(300,''John'')]
>> ;>进口泡菜
>>>得分=开放(得分,w)
>>> pickle.dump(hiscores [-10:],分数)
>>> scores.close()
>>>得分=开放(得分,r)
>>> oldscores = pickle.load(scores)
>>> scores.close()
>>>打印oldscores
[(200,''Jane''),(300,''John'')]
>>>
Here, I use a list of tuples. This list can be sorted by score, then pickled to save it to disk. Pickle documentation is in section 3.14.3 of python 2.4 docs.
>>> hiscores = [(300, "John")]
>>> score = 200
>>> player = "Jane"
>>> hiscores.append((score, player))
>>> print hiscores
[(300, ''John''), (200, ''Jane'')]
>>> hiscores.sort()
>>> print hiscores
[(200, ''Jane''), (300, ''John'')]
>>> import pickle
>>> scores = open("scores", "w")
>>> pickle.dump(hiscores[-10:], scores)
>>> scores.close()
>>> scores = open("scores", "r")
>>> oldscores = pickle.load(scores)
>>> scores.close()
>>> print oldscores
[(200, ''Jane''), (300, ''John'')]
>>>
这篇关于高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!