I do not fully understand classes. I have read the python documentation and several other tutorials. I get the basic gist of it but don’t understand the nuance. For instance in my code here:
class whiteroom():
""" Pick a door: red, blue, green, or black. """
do = raw_input("> ")
if "red" in do:
print "You entered the red room."
elif "blue" in do:
print "You entered the blue room."
elif "green" in do:
print "You entered the green room."
elif "black" in do:
print "You entered the black room."
else:
print "You sit patiently but slowly begin to stave. You're running out of time."
return whiteroom()
game = whiteroom()
game
(original codepad)
I would like to return the class whiteroom. Which is, either not possible, or not being done correctly. If you could clear up how to return a class or how to “link” two classes together so that whiteroom repeats on the else and the other rooms (which would be classes) are returned when called that would be awesome.
Also I’m super shaky on __init__ and am still not really sure what its purpose is. Everyone keeps telling me that it “initializes”, which I’m sure it does, but that doesn’t seem to be helping my brain out.
Functions are very different from classes. It looks like you took a function and just changed the
deftoclass. I guess that mostly works in your case, but it’s not how classes are supposed to go.Classes contain functions (methods) and data. For example, you have a ball:
Now we have a
Ballclass. How can we use it?It doesn’t look very useful. The data is where it could be useful:
Alright, cool, but what’s the advantage over a global variable? If you have another
Ballinstance, it will remain independent:And
ball1remains independent:Now what about that
bouncemethod (function in a class) we defined?The
bouncemethod caused it to modify thevelocitydata of itself. Again,ball1was not touched:Application
A ball is neat and all, but most people aren’t simulating that. You’re making a game. Let’s think of what kinds of things we have:
So let’s make a room. Rooms have names, so we’ll have some data to store that:
And let’s make an instance of it:
Spiffy. This turns out not to be all that useful if you want different rooms to have different functionality, though, so let’s make a subclass. A subclass inherits all functionality from its superclass, but you can add more functionality or override the superclass’s functionality.
Let’s think about what we want to do with rooms:
And how do we do that?
How it’s responded do depends on the room, so let’s make the room handle that with a method called
interact:Now let’s try interacting with it:
Your original example featured moving between rooms. Let’s use a global variable called
current_roomto track which room we’re in.1 Let’s also make a red room.1. There’s better options besides global variables here, but I’m going to use one for simplicity.
Now let’s try that:
Exercise for the reader: Add code to
WhiteRoom‘sinteractthat allows you to go back to the red room.Now that we have everything working, let’s put it all together. With our new
namedata on all rooms, we can also show the current room in the prompt!You might also want to make a function to reset the game:
Put all of the class definitions and these functions into a file and you can play it at the prompt like this (assuming they’re in
mygame.py):To be able to play the game just by running the Python script, you can add this at the bottom:
And that’s a basic introduction to classes and how to apply it to your situation.