2017-03-05 8 views
0

:私はコンソールに「負荷」と入力するとリスト(空)

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() 

は、何もdisplayelementsのために出力されません。そこで、リストに何かが含まれているかどうかを確認しようとしました。もしprint(displayelements)を実行してcopy.copy()が働いていれば、それが表示されます。 discoveredelementsに何かが含まれているかどうかを確認しました: ['Air '、' Earth '、' Fire '、' Water '] なぜcopy.copy()が機能しないのですか?

はEDIT:displayelementsはまだ空のリストである、

global discoveredelements 
discoveredelements = [] 

コピーはまだ動作しません: 私はグローバルとしてdiscoveredelementsを宣言しました。あなたが宣言しなければならない機能のグローバルVARに割り当てる

+0

ところで、 'gameloop'がループではありません - それは、再帰を使用しています。 1000回の移動後にゲームがクラッシュします。 – TigerhawkT3

+0

@ TigerhawkT3ああ、私には感謝してくれてありがとう: –

答えて

0

は、グローバルです:

discoveredelements = [] 
def func(): 
    # a new var is created in the scope of this function 
    discoveredelements = [1,2,3,4] 
func() 
print (discoveredelements) 



discoveredelements = [] 
def func(): 
    # declare as global 
    global discoveredelements 
    # assign to the global var 
    discoveredelements = [1,2,3,4] 
func() 
print (discoveredelements) 
+0

私はそれをグローバル変数として宣言しても、何も印刷しません(displayelements) –

+0

宣言しましたか? * de 'mainmenu'関数? – johantenbroeke

+0

ああ、私は笑っていない、今すぐ返信いただきありがとうございます! –