简体   繁体   中英

How to execute code in python to send the terminal to another file

I'm writing a text based adventure game in python and I can't hold all of the dialog choices in one file. I'm making the choices in int(input("blah")) and if statements.

Basically I give 3 choices, and tell them to choose a number, 1-3. If they choose 1, the choice that matches one shows on the terminal, and so on. Now that I've made one level of dialog I can't hold two levels.

For example: I give them 3 choices. I write an if statement so if they input 1 in the terminal, 'Blah' comes on the screen. Then, from that, I give 3 more choices.

I cant do this though, because it will just execute the code at the end of the statements. I need to be able to make different documents so when they choose a different option, I can handle the load of data they entered, and they can have more then one level of adventure gameplay.

Edit: Is there any way this is possible?

The code looks a little like this:

choice1 = int(input("Choose a number "))

 if choice1 == 1:
     cho1_1()
 if choice1 == 2:
     cho1_2()
 if choice1 == 3:
     cho1_3()

 def cho1_3():
      print ("'Say anything like that again to me and the only thing you'll see is the damp ceiling of my cellars.'")

I think, the good solution is to create your own data files for each level. Those files should contain following objects:

  • Rooms
  • Itmes
  • Enemies
  • Quests
  • etc...

Room object will hold some instances of Items, Enemies...etc

For example if you enter to some room, and got dialog "Choose your direction" Depend on direction selection, your game will load other room with it's own data. When you moving to next level just load new data file.

So what you have to do first if to carefully design you level data file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM