保存游戏的高分? [英] Saving the highscore for a game?

查看:54
本文介绍了保存游戏的高分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 pygame 在 python 中制作了一个非常简单的游戏.分数基于玩家达到的任何级别.我将级别作为名为 score 的变量.我想在游戏开始或结束时显示顶级.

I have made a very simple game in python using pygame. The score is based on whatever level the player reached. I have the level as a variable called score. I want to display the top level at the start or end of the game.

我会更乐意显示不止一个分数,但是我看到的所有其他线程都太复杂了,我无法理解,所以请保持简单:我是初学者,只需要一个分数.

I would be even more happy to display more than one score, but all of the other threads I have seen were too complicated for me to understand, so please keep it simple: I'm a beginner, only one score is necessary.

推荐答案

我推荐你使用 搁置.例如:

I recommend you use shelve. For example:

import shelve
d = shelve.open('score.txt')  # here you will save the score variable   
d['score'] = score            # thats all, now it is saved on disk.
d.close()

下次打开程序时使用:

import shelve
d = shelve.open('score.txt')
score = d['score']  # the score is read from disk
d.close()

它将从磁盘读取.如果需要,您可以使用此技术以相同的方式保存分数列表.

and it will be read from disk. You can use this technique to save a list of scores if you want in the same way.

这篇关于保存游戏的高分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆