python设计游戏人物的储存结构_Python游戏保存文件

I'm writing a game in Python and have come to the point where I need a way of saving the players progress. I was thinking of having the script create a text file and write a couple of lines (for storing stat variables, and the room number) when the player exits the game. But I also need it to check if that file exits on startup and, if so, apply the stored values to the corresponding variables. Can anybody help?

解决方案

Two different issues here:

What to store in the file and how to store it

The logic of when to store and when to load

The first issue is simpler to address as it's more generic. You have several good options here: one is using Python's ConfigParser class for Windows .ini - like configuration files. Alternatively you can use pickle to simply dump some sort of a configuration/settings data structure (may be a nested dict). Then there's the built-in SQLite binding. There are other options - it all depends on the level of complexity you want.

The second issue is more specific to your application. You can try to open the config file on startup and if it's there, read its contents. Later, you can periodically store the settings/progress into it. A word of advice: keep the complete set of settings in a consistent data structure at all times - even if only part of the settings are in the config file when you're reading it (for some reason), have default values for your settings.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值