I have put the classes in different files - how do I import a class from another file into another class?
Hello @GK77
I think this thread in Stack overflow explain this very well.
I hope this will give you the help you need.
Thanks for that, I think I have that working now, the problem is: can I import a class into an if statement and make it run?
hello @GK77
I tried to make a good example to show this.
The f-string in the print statement requires python3).
First the class that is imported:
# This file:
# fruit.py
class Fruit(object):
def into_basket(self): # This method..
return 'apples'
Then the class to use this:
# This file:
# fruit_basket.py
from fruit import Fruit
class Basket(object):
def fill_basket(self):
if d.into_basket() == 'apples': # appears here..
return d.into_basket() # and here
else:
print(f'there are no {d.into_basket()} in here. ') # and here again.
d = Fruit()
e = Basket()
print(e.fill_basket())
It is a little bit silly and over easy. But it is working.
See the comments in code for explanation how it goes into next class.
I hope this explain how to use other classes in the âmain classâ
I am working on my game, and I have got each room in a different class, and each class in a different file. I canât work out how to move from room to room eg.
if yes (#go to this room)
elif (#go to this room)
etc
Hello @GK77
You need at least have the Engine and Map class in one file.
The rooms can be imported.
Build one at the time.
Alright, so first thing is a bit of terminology clean up. This is a class:
class A(object):
pass
class B(object):
pass
But, these are objects of those classes:
one_a = A()
one_b = B()
You want to know how to move between objects, not classes. Think of a class like the blueprints to one of those fabricated house. You donât move into the blueprints. You move into one of the houses that was built from the blueprints. You would say, âHow do I move from house #455 on Lowell Lane to house #344 on Franklin Ave?â You donât ask, âHow do I move from the blueprints for these houses to the blueprints for a cathedral.â
Hopefully that analogy makes sense. Now, to move based on the game thereâs the enter
method:
class Kitchen(object):
def enter(self):
print("Enter Kitchen")
class Bedroom(object):
def enter(self):
print("Enter Bedroom")
lower_kitchen = Kitchen()
upper_kitchen = Kitchen()
master_bedroom = Bedroom()
kids_room = Bedroom()
Letâs say I have a house with these rooms in it. If I want to go into the master_bedroom I do this:
master_bedroom.enter()
Now, letâs say I want someone who is in the master_bedroom to go into the lower_kitchen. Then, I have two choices:
- Inside the Bedroom.enter I just call lower_kitchen.enter().
- I make a map, and each room returns the name of the next room.
Now, with that information go study the code again. Also, try making a 1 room game with the engine working then just add your room. Build up from mine, donât start fresh with your own design.
Thanks a lot for this explanation @zedshaw.
I still want to fully understand everything in this game.
And this is one of the last missing pieces in the jig saw puzzle.
The row: âmaster_bedroom.enter()â
just made that piece come into place.
I knew I needed it. But it just wasnât clear why until now.
My picture cleared considerably with this analogy.
I also would like to know if all the room having âSceneâ inside brackets will inherit exit(1) from the Scene class?
CentralCorridor(Scene):
Yes, if you have (Scene) then this class picks up everything from Scene. Think of the class in () as saying, âI am making a new type of Scene that has all the same things EXCEPT:â Then the code you add is the difference.
Thanks Zed, I got my game working, and I was moving from object to object, when I started getting this error every time I tried to run the game:
from MsOffice import MsOffice
ImportError: cannot import name âMsOfficeâ
I donât understand why this suddenly happened
If you made the MsOffice.py file then youâll have to make sure itâs in the right location and that you can import it. If this is from a module you installed with pip then make sure you have activated your virtualenv so you have access to it.
MsOffice is in the correct location, I havenât moved it. I think the problem started when I tried to ask âDo you want to play again?â and tried to take the player back to the original object
Ok, so how are you trying to figure this out? Are you just staring at the code or are you debugging it? Debugging means printing out the variables, or you can try using this:
Iâve tried using a debugger, but it just stops at the first line, so does the game
Iâve got the game working again, I think I was having problems with circular imports.
I ask âDo you want to play again?â
If the answer if yes, I need to take the player back to the beginning of the game - the first object, I am not sure how to do this, if I type "(objectname).enter() in the if statement after âyesâ, I get a name not defined error
Well you have to make sure that variable is available there. Also why are you doing (objectname).enter() instead of objectname.enter()?
I have tried to import the object, but then I canât start the game at all, as I get an
cannot import name âŚ
error
Attach a screenshot here. But remember these rules:
- If you put FooBar class inside mymodule.py, then your import is:
from mymodule import FooBar
- If you canât do
ls mymodule.py
and list it, then you are not in the right directory. - If you put mymodule.py in a directory, like
theapp/mymodule.py
, then your import is now:
from theapp.mymodule import FooBar
4. Just like #2 if you canât do ls theapp/mymodule.py
then it wonât work. You have to be in the directory that lets you do read/write that file. The mistake everyone makes is:
cd theapp # WRONG! NO NO NO NO NO NO NO NO NO NO NO!
python ../code.py
Do not cd into the directories. Stay above them.
Iâve uploaded 3 screenshots of the 3 files that are interlinked.
Everything works, I just canât get from the BondGirl object to the MoneyPenny object, when the player decides to play again.
Great, glad you got it working. Did you know you can do an account for free at https://github.com/ and then post your whole project there.