我正在使用Python创建文本冒险,并且在角色创建期间,一旦所有条目完成,游戏就应该将条目写入state()并保存游戏。在说出game_loop()函数中可见的内容之后,它所做的就是继续创建字符。
代码:
# -*- coding: iso-8859-1 -*-
# Codename Spacefaerer
import time
import random
import sys
import os
import jsonpickle
savegame_file = 'savegame.json'
game_state = dict()
# Standard Settings
settings_display = []
on = 314159265
off = 562951413
mainmenu = on
# Functions
def game_instructions(args):
print "In Spacefaerer there is an amalgamation of commands to execute either at your command station or while on land.\n"
print "Move: A command to move from block to block along a landscape. You will be told what directions you can move.\n"
print "This applies to North, South, East, West, North-East, North-West, South-East, and South-West.\n"
print "Use: A command to use an object either within your block, or within your inventory.\n"
print "Attack: A command to attack an enemy or friendly AI.\n"
print "Look: A command to look at an object or direction along a landscape. The same directions used in Move are applicable.\n"
print "Combine: A command to combine objects that could potentially go together. This is only possible at a workbench.\n"
print "Take: A command to take an object within your block.\n"
print "\n=== Layout of Commands ==="
print "Move: move n,s,e,w,ne,nw,se,sw\n"
print "Use: use item, use item on object\n"
print "Attack: attack object\n"
print "Look: look n,s,e,w,ne,nw,se,sw\n"
print "Combine: combine object object\n"
print "Take: take object\n"
def print_slowly(text):
for c in text:
print c,
sys.stdout.flush()
time.sleep(0.5)
def credits_slow(text):
for c in text:
print c,
time.sleep(0.1)
def roll_credits(args):
credits_slow("CODENAME: SPACEFAE†RER")
time.sleep(1)
credits_slow("\nA Text Adventure")
time.sleep(1)
print "\nDesigned and Developed by Angus Gardner"
time.sleep(1)
print "\nPowered by Python 2.7.3"
def save_game():
""" Save the current game state to a predefined location """
global game_state
with open(savegame_file, 'w') as savegame:
savegame.write(jsonpickle.encode(game_state))
def load_game():
""" Load a saved game state out of a predefined location """
with open(savegame_file, 'r') as savegame:
state = jsonpickle.decode(savegame.read())
return state
def pickup_item(text):
pinventory.append(text)
def drop_item(text):
pinventory.remove(text)
def die(text):
print "Sadly, your adventures have come to an end, my fellow Spacefaerer."
print "Your death was caused by", text
print "Returning you to the main menu."
game = off
mainmenu = on
def char_create():
ccmenu = True
while ccmenu == True:
cc_setspecies = str(raw_input("What species are you?\nIf you need help, type help <species>\nHuman, Eunisian, or Aur\n>"))
if cc_setspecies == 'help Human':
print """
Humans are primates of the family Homidae, and only current extant species of the genus Homo, which travels bipedally.
Humans originate from Earth, a planet in the Orion Arm of the Milky Way Galaxy.
They have a very well developed brain, and are uniquely adept on Earth at using systems of symbolic communication such as
language and art. Humans create complex social structures composed of many cooperating groups such as families and kinships.\n"""
continue
elif cc_setspecies == 'help Eunisian':
print """
Eunisians are the first common extant species of the genus Ax'thern. They travel tripedally.
Eunisians originate from M'thronop, a planet in the supercluster of the Andromeda galaxy.
They have adapted from their very high pressure atmosphere a larger anatomical structure, with a well developed and protected
brain within their heads. They have an open ended social structure, and reproduce often with no strong emotional attachments
between one another.
Eunisians communicate among one another telepathically, but have learned in the past 0.15Ma years to use language. They best
work with logic and architectural design, producing massive and strong structures built to last centuries.\n"""
continue
elif cc_setspecies == 'help Aur':
print """
The Aur is a species split from the Eunisians, evolving and splitting off within the past ten thousand years. They travel bipedally.
They have developed an environment that is closer to Earth's atmosphere, allowing their bodies to develop much longer and thinner.
Their overall ability is close to that of a Eunisian.\n"""
continue
else:
pass
cc_setname = str(raw_input("What is your name?\n>"))
if cc_setspecies == 'Human':
player = Human(cc_setname, 'Human', 100, 3, 3, 3, 'Hands', 'Captains Uniform', 'none', [], 0, 1, 120)
elif cc_setspecies == 'Eunisian':
player = Eunisian(cc_setname, 'Eunisian', 100, 3, 3, 3, 'Hands', 'Captains Uniform', 'none', [], 0, 1, 120)
elif cc_setspecies == 'Aur':
player = Aur(cc_setname, 'Aur', 100, 3, 3, 3, 'Hands', 'Captains Uniform', 'none', [], 0, 1, 120)
else:
print "Try again."
continue
state = dict()
state['players'] = [player]
return state
global game_state
game_cont = str(raw_input("Continue? (Y/N)\n>"))
if game_cont == 'Y':
save_game()
main()
elif game_cont == 'N':
continue
else:
print "Try again."
continue
def new_game():
""" Starts a new game. """
print "A gust of wind blows by the large, buried structure."
time.sleep(1)
print "Several panels of steel peel off, slamming into the ground and waking you up."
time.sleep(1.5)
print "You sit, lean forwards and notice a mirror across from you."
time.sleep(1)
print "What do you look like?\n"
char_create()
def game_loop():
""" Main game. """
global game_state
print "We made it here!"
ccmenu = False
def main():
""" Check if a savegame exists. If it doesn't,
initialize the game with standard settings."""
global game_state
if not os.path.isfile(savegame_file):
game_state = new_game()
else:
game_state = load_game()
game_loop()
# Locations
# Space Ships
# Weapons
# Armour / Clothing
# Items
# Gadgets
# Planets
# Galaxies
# Stars
# Species
class Human(object):
def __init__(self, name, species, health, strength, intelligence, endurance, weapon, armour, gadget, inventory, experience, level, credits):
self.name = name
self.species = "Human"
self.health = health
self.strength = strength
self.intelligence = intelligence
self.endurance = endurance
self.weapon = weapon
self.armour = armour
self.gadget = gadget
self.inventory = inventory
self.experience = experience
self.level = level
self.credits = credits
class Eunisian(object):
def __init__(self, name, species, health, strength, intelligence, endurance, weapon, armour, gadget, inventory, experience, level, credits):
self.name = name
self.species = "Eunisian"
self.health = health
self.strength = strength
self.intelligence = intelligence
self.endurance = endurance
self.weapon = weapon
self.armour = armour
self.gadget = gadget
self.inventory = inventory
self.experience = experience
self.level = level
self.credits = credits
class Aur(object):
def __init__(self, name, species, health, strength, intelligence, endurance, weapon, armour, gadget, inventory, experience, level, credits):
self.name = name
self.species = "Aur"
self.health = health
self.strength = strength
self.intelligence = intelligence
self.endurance = endurance
self.weapon = weapon
self.armour = armour
self.gadget = gadget
self.inventory = inventory
self.experience = experience
self.level = level
self.credits = credits
class Enemy(object):
def __init__(self, name, species, health, strength, intelligence, endurance, weapon, armour, gadget, inventory, level, credits):
self.name = name
self.species = species
self.health = health
self.strength = strength
self.intelligence = intelligence
self.endurance = endurance
self.weapon = weapon
self.armour = armour
self.gadget = gadget
self.inventory = inventory
self.level = level
self.credits = credits
class Friendly(object):
def __init__(self, name, species, health, strength, intelligence, endurance, weapon, armour, gadget, inventory, level, credits):
self.name = name
self.species = species
self.health = health
self.strength = strength
self.intelligence = intelligence
self.endurance = endurance
self.weapon = weapon
self.armour = armour
self.gadget = gadget
self.inventory = inventory
self.level = level
self.credits = credits
# Game
print "CODENAME: SPACEFAERER"
print "(C) Angus Gardner 2014, 2015, 2016 (CC-BY-NA)"
while mainmenu == on:
play_opt = str(raw_input("\nNew Game\nLoad Game\nHow to Play\nCredits\nLicense\nExit\n>"))
if play_opt == 'New Game':
game = on
mainmenu = off
elif play_opt == 'Load Game':
""" A command executes to load the savegame.json """
game = on
game_state = load_game()
mainmenu = off
elif play_opt == 'How to Play':
game_instructions(1)
elif play_opt == 'Credits':
roll_credits(1)
elif play_opt == 'License':
print "\nCODENAME: SPACEFAE†RER is licensed under the GNU General Public License v3.0"
print "\nCopyrighted under Creative Commons (CC-BY-NA)\n"
continue
elif play_opt == 'Exit':
print "\nThanks for playing!"
time.sleep(0.5)
sys.exit(1)
else:
print "\nTry again."
continue
while game == on:
if __name__ == '__main__':
main()
非常感谢任何帮助。
答案 0 :(得分:4)
我认为您必须向global ccmenu
和def game_loop()
添加def char_create()
,否则可以全局访问。
此外,由于您在完成“继续?”之前在return state
中执行char_create()
提示及其下方的代码将无法运行,包括拨打save_game()
。