Ok。小さなテキストベースの 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 が対応するコード ブロックから開始します。実際のコードは、書き手と (おそらく) 読み手にとっても大変な作業です。そのため、同じ環境をシミュレートし、同じ問題を再現する次のコードを作成しました。
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"