1

So I am writing an adventure game, and once you have managed to get out of the first room, I want to open up the next "room" which is a second python file. How do I do this? I have tried:

import os
os.system("start "+"agroom2.pyw")

but this doesn't seem to work. So how do you open a different python file, in python?

4
  • 1
    If the file is in the same directory, you can import its contents and run its methods. Commented Dec 21, 2015 at 18:48
  • 1
    Check out some python tutorials and especially for import because what you're trying to do is almost certainly not what you want. Commented Dec 21, 2015 at 18:48
  • ps - agroom2._py_ doesn't work nor does anything else. Believe me, I've tried Commented Dec 21, 2015 at 18:48
  • Assuming that this is a console-based text game, you want to use .py files and not .pyw. .pyw extension is used for GUI programs that don't use a console window. Commented Dec 21, 2015 at 18:52

3 Answers 3

2

Perhaps, rather then running the script from outside of python, you should import your game rooms into your main script. You can call functions from other scripts in the same directly using import.

import room1, room2, room3`

...

if room1.status == 'Complete':
   room2.start

...

if room2.status == 'Complete':
   room3.start 

Investigate how to use import for this purpose here: Python Import Tutorial

Sign up to request clarification or add additional context in comments.

Comments

1

Importing rooms is probably the easiest answer. One way to do this is to

  • create a directory for your game to live in
  • make an empty file called __init__.py. This tells python that importable files live in this directory
  • run your main script from this directory and import stuff.

Here's a quick example

in dir ag:

__init__.py (empty)

start.py

import agroom1
import agroom2

print("The start of the game")

while True:
    choice = input("next? > ")
    if choice == '1':
        agroom1.main()
    if choice == '2':
        agroom2.main()
    if choice == 'quit':
        break;

agroom1.py

def main():
    print("In room 1")

agroom2.py

def main():
    print("In room 2")

Comments

0

Ok, I feel a bit of a fool. I have worked it out and it is, oh so simple. You literally do:

import agroom2

As a test I wrote print("Hi") and on the file I imported it into Hi appeared in the output after all the other output. So there you go!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.