Interactive Python: Classes and Objects
Congratulations on completing construction Pong. Now, I am going to ask you to rearrange your ‘Pong’ program so that it uses classes and objects. That is a relatively big step forward, so before we do that, let’s return to the Interactive Python text and complete chapters
“Classes and Objects: the Basics”
and “Classes and Objects: Digging a Little Deeper”
.
Be sure to complete the
‘Exercises’
at the end of each chapter. Once you are finished with IP, return to this tutorial. There are no ‘Class Assignments’
for the ‘Classes and Objects’
chapters.
RPG
Now that we have a bit of background and experience coding using object-oriented programming(OOP) principles, let’s apply those skills in a small case study. We are going to take a look at the beginning of a text-based RPG that you may have wrote yourself if you followed the “Gaming Adventure” extension to the “Choose Your Own Adventure”
lab. Download the code here and run it several times. Become familiar with what the program does and how it does it. You may run into unfamiliar code. A quick Google search should clear up any confusion. Once you fully understand the program, answer the following questions in IP under ‘Class Assignments’
:
- Why are Hero's statistics variables declared as global rather than local?
- In the 'check_input' function, what does 'try and except' do? **hint: delete only the except portion and try to break the program's by entering a string or just hitting enter.
- What exceptions is the 'except' looking for? **hint: http://goo.gl/I1kchH
- Why is the program using the function 'print_delay' instead of 'print'?
- Why do we need to say 'global var_name', where 'var_name' is the global variable's name, at the beginning of each function in which the global variable is used?
**hint: http://goo.gl/qQzCBk
Now Try:
Complete all the “loose ends” in the game. Be sure to use all of the unused Hero statistics properties.
RPG Objectify
From here on forward, I will be using the term ‘objectify’ which will signify the process of rearranging procedural code(code written without the use of classes and objects) using the principles of object-oriented programming(OOP).
One thing I would like to make clear before moving forward is that code written using OOP is EXACTLY like code written procedurally.
Every program that uses classes and objects can be written without classes and objects and vice versa.
There is no magic happening when using classes and objects.
Code written using OOP is simply a rearrangement of how code is processed similar to the difference between code written with and without functions. If you compare the control flow of a program that is written procedurally and one that is written using OOP, it will become clear that they execute the exact same set of instructions in the exact same order.
Checking For Understanding
A simple check to tell if you understand OOP is to ask yourself the following question: Can I imagine how this program using OOP would be written without? On the flipside, if you are looking at code that is written procedurally and can’t imagine how it would be written using OOP, then you need some clarification and please ask me for guidance.
Remember that when applying OOP principles, a class should be created for each logical object. This means that if something can be logically categorized you should create a separate object for it. In our case, we should have a Hero class. If the RPG game was more fully developed, there would also be an Enemy class. There is some room for interpretation here on when to create a new class. On one end of the spectrum you can classify every component of a category when only one is necessary, and on the other end you group concepts when they should be broken down into separate classes.
Looking at our case study, we can see that it is very simple and at the moment there should be only one class: the Hero class. You can make an argument that there should also be a Weapons class and a Hero Statistics class, however at the moment our program is simple enough that a single Hero class will suffice. Again the lines are blurred here, but remember that using OOP principles should make the programmer’s life easier, not more difficult. The general rule is that you should start with a single class and once one portion of that class becomes too complex, it should be broken down into sub-classes.
Ok, let’s objectify the code found in the RPG game. Start by creating a Hero class using the following class structure:
class Hero(): def __init__(self): # Initialize the Hero’s variables. def update_energy(self, energy): # Update the Hero’s energy def equip(self, weapon): # Equip the Hero’s weapon def update_gil(self, gil): # Update the Hero’s gold.Once you have written the class for Hero, use the class by creating an instance of our class in your RPG game. Remember that an instance of a class is called an object. Your game should run exactly as it did when written procedurally, however instead of initializing Hero’s statistical variables, you will be creating a Hero object. Instead of using local variables to keep track of Hero, you will be using the Hero object’s properties and methods to update Hero’s statistics. When Hero loses energy in the story, the ‘update_energy’ method should be invoked which changes the object's ‘energy’ property. When Hero equips a weapon, the ‘equip’ method should be invoked which changes the ‘hero_weapon’ property and so on.
Now Try
Now, rewrite the RGP code so that it uses OOP principles. Once you are finished, remember to turn in your objectified RPG code on IP in the ‘Class Assignments’ page.
rubric
can be found here.