好的。我正在设计一个基于文本的小型 RPG,但是,我需要让玩家能够保存游戏。我已经通过使用 pickle 模块成功地做到了这一点,但我试图让玩家能够通过使用这个我称之为“storypointe”的变量回到故事情节中的前一个点。基本上它会像这样工作:
if storypointe == 0:
#Story, story, stuff happens here...
storypointe += 1
if storypointe == 1:
#More story, more story, more stuff happens here....
然后我会腌制变量storypointe,当游戏加载时(意味着使用pickle.load从我腌制到的任何文件中获取玩家统计数据和故事点),理想情况下它会从故事点对应的任何代码块开始。实际代码对作者和(也许)读者来说工作量太大,所以我编写了以下代码来模拟相同的环境并复制相同的问题。
storypointe = 0
jump = 0
spin = 0
dive = 0
roar = 0
savefile = "C:\Users\Sammu\The Folder\databin.txt"
import pickle, sys
def save ():
with open(savefile, 'w') as savebin:
actions = [jump, spin, dive, roar, storypointe]
pickle.dump (actions, savebin)
def load ():
with open(savefile, 'r') as loadbin:
actions2 = pickle.load (loadbin)
print actions2
jump = actions2[0]
spin = actions2[1]
dive = actions2[2]
roar = actions2[3]
storypointe = actions2[4]
#Begin the code#
gameIO = raw_input ('Would you like to load previous game?\n>>> ')
if gameIO in ['yes', 'load', 'load game', 'Yes', 'Load', 'Load game']:
load ()
if storypointe == 0:
action = raw_input ('Would you like to jump, spin, dive or roar?\n>>> ')
if action in ['jump', 'Jump']:
jump += 1
print jump
if action in ['spin', 'Spin']:
spin += 1
print spin
if action in ['dive', 'Dive']:
dive += 1
print dive
if action in ['roar', 'Roar']:
roar += 1
print roar
storypointe += 1
if storypointe == 1:
print "\n\nYou have progressed to the next stage"
save ()
所以如果storypointe等于actions2[4],那么这一定意味着它应该等于1。但这里的问题是它总是跟随第一个代码块,从
action = raw_input ('#yadayadayada')
代替:
print "You have progressed to the next stage"