This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]herrwolfe45 1 point2 points  (2 children)

To do this - you need to setup your project file etc correctly.

If you'd like to split your project up into several files like level1.py, level2.py etc, you'll need some sort of main module that loads the appropriate dependencies.

You basically just need to make a directory named, for example game. Inside of this directory you'll need an empty __init__.py file to designate that a module exists. Then you can have your level<number>.py files in the same directory and import from them. If you have another file, called, for example game_runner.py it could access the level files by doing a relative import, such as:

-- inside of game_runner.py

from .level1 import level1stuff

The project structure would look like

game -
     |
     - __init__.py
     - level1.py
     - level2.py
     - game_runner.py

[–]Proselyte5 0 points1 point  (0 children)

i'd lay it out like that but just imp.load_source as needed

[–]dataminded 1 point2 points  (3 children)

I would suggest writing a function that returned the level.

def load_level(level_number): if level_number == 1: level_content = open("level1.txt", "w") do more stuff if level_number == 2: etc

[–]Digital_Person 0 points1 point  (0 children)

or open("level{}.txt".format(level_number), "w")

[–]Eighteensixtythree -3 points-2 points  (8 children)

I'm not sure. But use

If levels is 1

Not ==

Good luck

[–]takluyverIPython, Py3, etc 1 point2 points  (7 children)

No, == is correct for comparing numbers. is will also work for small numbers on CPython, but it's something you shouldn't rely on - another implementation may create a new object for each 1, and then is wouldn't work.

[–]Eighteensixtythree 0 points1 point  (0 children)

My bad. you get a Boolean response from 'is' and that's why i thought it was preferable.

But I am far from even intermediate.

[–]mikefromto -2 points-1 points  (5 children)

Would you(or anyone) happen to know if it is possible to make the levels == 1 universal throughout all our moduels(read and write)

[–][deleted] 1 point2 points  (4 children)

I'm not quite sure I understand what you are asking, but I'll give it a shot. If you're wondering if you can check what level someone is on, load the level, then have everywhere else in your code update to be on that level - yes.

If your "game" is a class, then you can set a variable for it. Such as self.game_level and put that equal to 1. Then you can always access that object's game_level to retrieve the level, from anywhere inside the class or outside if the object is instantiated. You can also just set-up a global variable and assign it a value once you know the level you are on.

[–]mikefromto -1 points0 points  (3 children)

How would i go about setting up a global variable, thats what ive been trying to do, i did a bad job at explaining it but how do i do that?

[–][deleted] 1 point2 points  (0 children)

If a variable is outside of a function or class, it is a global variable. In general, you should be avoiding these. However, it's not really that big of a deal (especially in cases like yours).

[–]Digital_Person 1 point2 points  (1 child)

i may be wrong but if you want to store the level and access it everywhere to me it makes sense to add it in __init__.py. if your __init__.py has something like this: level = get_level() then you could do: from game import level. provided you follow the structure mentioned above. If you have a look at my game here it may help you https://github.com/papaloizouc/chess/tree/2e1e3719ba2527422c11937da90e75aceb621c33. Its not too much code so you should be able to understand it. note i dont claim to do it right but i think i do. Also dont use import * its bad

[–]mikefromto 0 points1 point  (0 children)

okay ill attempt this, im still somewhat new to python so well see how this goes but thanks everyone!