while True:
print "\n---------"
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
Why do we need self in “def init(self, scene_map)”?
Why do we put self in “self.scene_map = scene_map”?
In def play(self):? Why not just have it def play()?
First a_map become a instance of Map class with ‘central_corridor’ as argument
Then a_game become a instance of Engine class with a_map as argument.
At last the instance (a_game) is connected with the attribute (play)
a_game.play()
In the class method play you can see a while loop.
As long as the conditions are True the game will go on.
It starts with:
‘central_corridor’
If you dont get killed it continues with:
laser_weapon_armory’
the_bridge’
escape_pod’
It is death and finished that will change the conditions to False and it stops the loop.
I realise now (after a cup of coffe) that this was perhaps not answer to your question.
I hope it is somewhat useful anyway.
I think I saw more of your code before.
If so I think there was a missing return statement after the while loop.
Or in the loop. I am still at work, so I cannot give you more than this at the moment…
Thanks, yes I did but then I figured it out so I removed it. I have a hard time understanding the Engine class, I have read the explanation in the book but still don’t get it.
Okay, so we define a function. I don’t understand init fully after googling but I am content with knowing you need it if you define a function inside a class. Then we have self which is like a placeholder which is also just needed otherwhise it doesn’t work.
Why do we set self.scene_map to scene_map? What happens when we do that?
So, I think the first problem is you have to figure out what scene_map is and where it comes from. Try this out:
Get yourself a piece of paper and a pencil or pen.
Now, find every variable in the script and write down where it’s created. You’d look for where it’s set = to something, and “where” should be something like the line number, then the code its in. For example: scene_map | line 25 | script. If something was in a class function it’d be another_var | line 300 | MyClass.thefunc.
Next, add to that line what class or type that variable is. So, you’d say scene_map | line 25 | script | class Map.
Now that you know where every variable is created, you can go back to studying your code. When you hit a spot you don’t understand, the first thing to ask is “where are these variables coming from AND what are they.” In this case, you say “scene_map is a Map class coming from line X in the main script.”
Finally, say what you’re doing to that variable. In this case you say, “I’m assigning it to the self.scene _map variable.”
Ok, so you have list of variables and where they come from right? Now, later in that class you start usingself.scene_map to do stuff. You have your list of every variable right? So you can then go look in your list and see, “Oh, self.scene_map comes from scene_map that was defined at the bottom of the script, so that means self.scene_map is a Map.”
Now, why do you need self? Imagine I have this code:
class Apple():
def eat(self):
print("You ate it.")
x = Apple()
y = Apple()
j = Apple()
I just made 3 apples, x, y, and j. Each of those are completely separate things, and the Apple() call simply is a way to craft those using the class Apple “template”. After this they run on their own and mostly don’t interact. Ok, so what if I did this:
x.eat()
How does the Apple.eat function know that it’s working on x and not y or j? Try to answer that and it should help you figure out what self does. Another clue is that self == x. But if you were to do y.eat() then self == y.