我有以下代碼:列表不復制(空)
import sys
import os.path
import ConfigParser
import copy
import time
import colorama
from colorama import init
from colorama import Fore, Back, Style
init()
#init variables
discoveredelements = []
discoveredgroups = []
combo = []
savefile = ConfigParser.ConfigParser()
os.chdir(os.getcwd())
#init other stuff
class style:
BOLD = '\033[1m'
END = '\033[0m'
def combos():
#all combos
combo.append(('Air', 'Air', 'Wind'))
combo.append(('Earth', 'Earth', 'Pressure'))
combo.append(('Fire', 'Fire', 'Explosion'))
combo.append(('Water', 'Water', 'Sea'))
combo.append(('Air', 'Earth', 'Dust'))
combo.append(('Air', 'Fire', 'Energy'))
combo.append(('Air', 'Water', 'Steam'))
combo.append(('Earth', 'Fire', 'Lava'))
combo.append(('Earth', 'Water', 'Swamp'))
combo.append(('Fire', 'Water', 'Alcohol'))
def mainmenu():
print(style.BOLD + "ALCHEMY" + style.END)
print(style.BOLD + "Load Game" + style.END)
print(style.BOLD + "New Game" + style.END)
print(style.BOLD + "Exit" + style.END)
print("Type \"load\" or \"new\" or \"exit\" ")
mainmenuinput = raw_input()
if mainmenuinput == "exit":
sys.exit()
elif mainmenuinput == "load":
if os.path.exists('save.ini'):
savefile.read('save.ini')
discoveredelements = savefile.get('Elements','discoveredelements')
print("Game Loaded")
rungame()
else:
print("Save file not found, check file directory or start a new game.")
mainmenu()
elif mainmenuinput == "new":
if os.path.exists("save.ini"):
print("Current save file will be overwritten. Proceed?")
print("Y or N")
overwriteinput = raw_input()
if overwriteinput == "Y":
newgame()
rungame()
elif overwriteinput == "N":
mainmenu()
else:
newgame()
rungame()
def newgame():
save = open('save.ini','w')
#reset data
savefile.add_section('Elements')
savefile.add_section('Groups')
savefile.set('Elements','discoveredelements',"")
savefile.set('Groups','discoveredgroups',"")
#adds the default elements
discoveredelements.append("Air")
discoveredelements.append("Earth")
discoveredelements.append("Fire")
discoveredelements.append("Water")
savefile.set('Elements','discoveredelements',discoveredelements)
discoveredgroups.append("Air")
discoveredgroups.append("Earth")
discoveredgroups.append("Fire")
discoveredgroups.append("Water")
savefile.set('Groups','discoveredgroups',discoveredgroups)
savefile.write(save)
save.close()
print("Game Loaded")
def gameloop():
#actual gameplay
print("Type two elements (seperately) or \"list\" or \"hint\" or \"save\" or \"exit\"")
gameinput = raw_input()
if gameinput == "list":
displayelements = copy.copy(discoveredelements)
print(','.join(map(str, displayelements)))
gameloop()
elif gameinput == "hint":
if (time.time() - timerstart) >= 10:
print('hint')
timerstart = time.time()
gameloop()
else:
print("Hint is still on cooldown")
gameloop()
elif gameinput == "save":
savefile.set('Elements','discoveredelements',discoveredelements)
savefile.set('Groups','discoveredgroups',discoveredgroups)
print("Game saved")
elif gameinput == "exit":
savefile.read('save.ini')
savelist = savefile.get('Elements','discoveredelements')
if len(savelist) < len(discoveredelements):
print("Game not saved! Do you wish to exit without saving?")
print("Y or N")
overwriteinput = raw_input()
if overwriteinput == "Y":
mainmenu()
else:
gameloop()
else:
elementA = gameinput
elementB = raw_input()
if (elementA in discoveredelements) and (elementB in discoveredelements):
i = 0
created = 0
while True:
if (combo[i][0] == elementA and combo[i][1] == elementB) or (combo[i][1] == elementA and combo[i][0] == elementB):
print("You created " + combo[i][2])
discoveredelements.append(combo[i][2])
created = 1
break
i += 1
if i == len(combo):
break
if created == 0:
print("No elements created")
gameloop()
else:
print("Error, using non-existent or not yet discovered elements")
gameloop()
def rungame():
#initializing game
timerstart = time.time()
displayelements = copy.copy(discoveredelements)
print(','.join(map(str, displayelements)))
gameloop()
#game starts here
print(Style.RESET_ALL)
combos()
mainmenu()
當I型「負載」到控制檯中,沒有爲displayelements輸出。所以,我想看看如果列表中沒有任何內容(如果copy.copy()工作或不)做印刷(displayelements),並將其印刷[] 然後我檢查是否discoveredelements包含任何東西,做的: ['空氣」, '地球', '火', '水'] 爲什麼不是copy.copy()工作?
編輯: 我宣佈discoveredelements全球:
global discoveredelements
discoveredelements = []
的複製仍然不能正常工作,displayelements仍然是一個空列表。
順便說一句,'gameloop'不是一個循環 - 它使用遞歸。遊戲將在1000次移動後崩潰。 – TigerhawkT3
@ TigerhawkT3哦,我不知道,你告訴我:) –