Subscribe to Non Static        RSS Feed
***** 1 Votes

Object Oriented Programming: Python my way

Icon 1 Comments
This is a response to Object Oriented Programming, going forward known as OOP, as presented in a popular resource.

Probably one of the books most recommended to beginning Python programmers is Zed Shaw's Learn Python the Hard Way. I've probably recommended it myself. Overall, it's a good place to start... But the Exercise 43: Basic Object-Oriented Analysis and Design is rather hard for me to be enthusiastic about.

Indeed, it's so contrived that the earlier, non OOP version, Exercise 41: Gothons From Planet Percal #25 seems far superior to me.

Why is the OOP version confusing and what happened?

The point of an object, in OOP, is primarily to store data. A class is the blueprint of an object that will store data. The methods of an object should only exist to manipulate that object's data. An object that doesn't store data is really not an object at all, as every instance of the object's class would be identical and what's the point of that? The OOP program noted above has classes that don't store data, which doesn't seem to address the concept.

Of course, a class is also just another why to organize code. You will see stateless classes in other languages. C# even formalizes the design by offering a static class than may only have static methods. Of course, you can have "class variables" in most languages, but we aren't going there here. So, class as code container is valid, particularly in languages like Java were class is the top level container, but it is an idiom to get around a problem that Python doesn't have.

Curiously, Python is actually more object oriented than the beginner might realize. Everything in Python is an object! Numbers are objects, which is rare in most OOP languages. Functions are objects. Your python file implicitly contains a module, which is an object. A class essentially defines a custom object type, but Python programmers will often exhaust existing objects before resorting to making their own.

So, first, the Exercise 43 referenced above, with all the code in place, unlike the linked source:
from sys import exit
from random import randint

class Scene(object):
    def enter(self):
        print "This scene is not yet configured. Subclass it and implement enter()."
        exit(1)

class Engine(object):

    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

        # be sure to print out the last scene
        current_scene.enter()

class Death(Scene):

    quips = [
        "You died.  You kinda suck at this.",
         "Your mom would be proud...if she were smarter.",
         "Such a luser.",
         "I have a small puppy that's better at this."
    ]

    def enter(self):
        print Death.quips[randint(0, len(self.quips)-1)]
        exit(1)

class CentralCorridor(Scene):

    def enter(self):
        print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
        print "your entire crew.  You are the last surviving member and your last"
        print "mission is to get the neutron destruct bomb from the Weapons Armory,"
        print "put it in the bridge, and blow the ship up after getting into an "
        print "escape pod."
        print "\n"
        print "You're running down the central corridor to the Weapons Armory when"
        print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume"
        print "flowing around his hate filled body.  He's blocking the door to the"
        print "Armory and about to pull a weapon to blast you."

        action = raw_input("> ")

        if action == "shoot!":
            print "Quick on the draw you yank out your blaster and fire it at the Gothon."
            print "His clown costume is flowing and moving around his body, which throws"
            print "off your aim.  Your laser hits his costume but misses him entirely.  This"
            print "completely ruins his brand new costume his mother bought him, which"
            print "makes him fly into an insane rage and blast you repeatedly in the face until"
            print "you are dead.  Then he eats you."
            return 'death'

        elif action == "dodge!":
            print "Like a world class boxer you dodge, weave, slip and slide right"
            print "as the Gothon's blaster cranks a laser past your head."
            print "In the middle of your artful dodge your foot slips and you"
            print "bang your head on the metal wall and pass out."
            print "You wake up shortly after only to die as the Gothon stomps on"
            print "your head and eats you."
            return 'death'

        elif action == "tell a joke":
            print "Lucky for you they made you learn Gothon insults in the academy."
            print "You tell the one Gothon joke you know:"
            print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
            print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
            print "While he's laughing you run up and shoot him square in the head"
            print "putting him down, then jump through the Weapon Armory door."
            return 'laser_weapon_armory'

        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'


class LaserWeaponArmory(Scene):

    def enter(self):
        print "You do a dive roll into the Weapon Armory, crouch and scan the room"
        print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
        print "You stand up and run to the far side of the room and find the"
        print "neutron bomb in its container.  There's a keypad lock on the box"
        print "and you need the code to get the bomb out.  If you get the code"
        print "wrong 10 times then the lock closes forever and you can't"
        print "get the bomb.  The code is 3 digits."
        code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
        guess = raw_input("[keypad]> ")
        guesses = 0

        while guess != code and guesses < 10:
            print "BZZZZEDDD!"
            guesses += 1
            guess = raw_input("[keypad]> ")

        if guess == code:
            print "The container clicks open and the seal breaks, letting gas out."
            print "You grab the neutron bomb and run as fast as you can to the"
            print "bridge where you must place it in the right spot."
            return 'the_bridge'
        else:
            print "The lock buzzes one last time and then you hear a sickening"
            print "melting sound as the mechanism is fused together."
            print "You decide to sit there, and finally the Gothons blow up the"
            print "ship from their ship and you die."
            return 'death'



class TheBridge(Scene):

    def enter(self):
        print "You burst onto the Bridge with the netron destruct bomb"
        print "under your arm and surprise 5 Gothons who are trying to"
        print "take control of the ship.  Each of them has an even uglier"
        print "clown costume than the last.  They haven't pulled their"
        print "weapons out yet, as they see the active bomb under your"
        print "arm and don't want to set it off."

        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic you throw the bomb at the group of Gothons"
            print "and make a leap for the door.  Right as you drop it a"
            print "Gothon shoots you right in the back killing you."
            print "As you die you see another Gothon frantically try to disarm"
            print "the bomb. You die knowing they will probably blow up when"
            print "it goes off."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your blaster at the bomb under your arm"
            print "and the Gothons put their hands up and start to sweat."
            print "You inch backward to the door, open it, and then carefully"
            print "place the bomb on the floor, pointing your blaster at it."
            print "You then jump back through the door, punch the close button"
            print "and blast the lock so the Gothons can't get out."
            print "Now that the bomb is placed you run to the escape pod to"
            print "get off this tin can."
            return 'escape_pod'
        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"


class EscapePod(Scene):

    def enter(self):
        print "You rush through the ship desperately trying to make it to"
        print "the escape pod before the whole ship explodes.  It seems like"
        print "hardly any Gothons are on the ship, so your run is clear of"
        print "interference.  You get to the chamber with the escape pods, and"
        print "now need to pick one to take.  Some of them could be damaged"
        print "but you don't have time to look.  There's 5 pods, which one"
        print "do you take?"

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod escapes out into the void of space, then"
            print "implodes as the hull ruptures, crushing your body"
            print "into jam jelly."
            return 'death'
        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod easily slides out into space heading to"
            print "the planet below.  As it flies to the planet, you look"
            print "back and see your ship implode then explode like a"
            print "bright star, taking out the Gothon ship at the same"
            print "time.  You won!"

            return 'finished'

class Finished(Scene):

    def enter(self):
        print "You won! Good job."
        return 'finished'

class Map(object):

    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death(),
        'finished': Finished(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name)
        return val

    def opening_scene(self):
        return self.next_scene(self.start_scene)

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()



See the classes that have no reason to live? Consider when a class stores data versus when it just holds a method. If that method never actually calls self, you should consider it highly suspect. Also note the exit(1), a sure sign your program flow is buggered.

Working off that code, here is my version:
from sys import exit
from random import randint

# this holds valuable information about the current state of our game
# realistically, this is the only explicit object we need

class State(object):
    def __init__(self, location):
        # the is a function that takes state
        # other locations might manipulate this
        # though not directly
        self.__location = location

        # anything else, like an inventory, might go here
        self.armory_code = None
        self.armory_guesses = None

        self.pod_good = None
        self.pod_guess = None

    def set_next(self, location):
        self.__location = location

    def next(self):
        # helper method, not required but nice
        self.__location(self)

    def is_done(self):
        # we're done when we have nowhere to be
        return not self.__location


# the humble game loop
# not that much to it
def game_loop(state):
    while not state.is_done():
        state.next()

# all the places we can go, each takes the current state
def loc_death(state):
    quips = [
        "You died.  You kinda suck at this.",
         "Your mom would be proud...if she were smarter.",
         "Such a luser.",
         "I have a small puppy that's better at this."
    ]
    print quips[randint(0, len(quips)-1)]
    state.set_next(None)

# helper method, we die a lot
def die(state):
    state.set_next(loc_death)

# helper method, for input
def action_handler(state, actions):
    action = raw_input("> ")
    # an action is a name and a location
    # the name mapping to the user input
    # we could have made a class for this kind of thing
    # but tuples and convention makes for an elegant solution
    for (name, location) in actions:
        if action==name:
        state.set_next(location)
    print "DOES NOT COMPUTE!"



def loc_central_corridor_enter(state):
    def on_joke(state):
        print "Lucky for you they made you learn Gothon insults in the academy."
        print "You tell the one Gothon joke you know:"
        print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
        print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
        print "While he's laughing you run up and shoot him square in the head"
        print "putting him down, then jump through the Weapon Armory door."
        state.set_next(loc_laser_weapon_armory)
        
    def on_dodge(state):
        print "Like a world class boxer you dodge, weave, slip and slide right"
        print "as the Gothon's blaster cranks a laser past your head."
        print "In the middle of your artful dodge your foot slips and you"
        print "bang your head on the metal wall and pass out."
        print "You wake up shortly after only to die as the Gothon stomps on"
        print "your head and eats you."
        die(state)

    def on_shoot(state):
        print "Quick on the draw you yank out your blaster and fire it at the Gothon."
        print "His clown costume is flowing and moving around his body, which throws"
        print "off your aim.  Your laser hits his costume but misses him entirely.  This"
        print "completely ruins his brand new costume his mother bought him, which"
        print "makes him fly into an insane rage and blast you repeatedly in the face until"
        print "you are dead.  Then he eats you."
        die(state)

    def on_enter(state):
        print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
        print "your entire crew.  You are the last surviving member and your last"
        print "mission is to get the neutron destruct bomb from the Weapons Armory,"
        print "put it in the bridge, and blow the ship up after getting into an "
        print "escape pod."
        print "\n"
        print "You're running down the central corridor to the Weapons Armory when"
        print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume"
        print "flowing around his hate filled body.  He's blocking the door to the"
        print "Armory and about to pull a weapon to blast you."
        # our first action list
        # note, [(name, action), (name, action) ...]
        action_handler(state, [
            ("shoot!", on_shoot),
            ("dodge!", on_dodge),
            ("tell a joke", on_joke)
        ])
    state.set_next(on_enter)
    


def loc_laser_weapon_armory(state):
    def on_enter(state):
        print "You do a dive roll into the Weapon Armory, crouch and scan the room"
        print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
        print "You stand up and run to the far side of the room and find the"
        print "neutron bomb in its container.  There's a keypad lock on the box"
        print "and you need the code to get the bomb out.  If you get the code"
        print "wrong 10 times then the lock closes forever and you can't"
        print "get the bomb.  The code is 3 digits."
        state.armory_code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
        state.armory_guesses = 10

    def on_death(state):
        print "The lock buzzes one last time and then you hear a sickening"
        print "melting sound as the mechanism is fused together."
        print "You decide to sit there, and finally the Gothons blow up the"
        print "ship from their ship and you die."
        die(state)

    def on_success(state):
        print "The container clicks open and the seal breaks, letting gas out."
        print "You grab the neutron bomb and run as fast as you can to the"
        print "bridge where you must place it in the right spot."
        state.set_next(loc_the_bridge)


    def on_code(state):
        state.armory_guesses -= 1
        if state.armory_guesses==0:
            state.set_next(on_death)
        elif raw_input("[keypad]> ") == state.armory_code:
            state.set_next(on_death)

    if state.armory_code:
        state.set_next(on_code)
    else:
        state.set_next(on_enter)


def loc_the_bridge(state):
    def on_throw(state):
        print "In a panic you throw the bomb at the group of Gothons"
        print "and make a leap for the door.  Right as you drop it a"
        print "Gothon shoots you right in the back killing you."
        print "As you die you see another Gothon frantically try to disarm"
        print "the bomb. You die knowing they will probably blow up when"
        print "it goes off."
        die(state)

    def on_place(state):
        print "You point your blaster at the bomb under your arm"
        print "and the Gothons put their hands up and start to sweat."
        print "You inch backward to the door, open it, and then carefully"
        print "place the bomb on the floor, pointing your blaster at it."
        print "You then jump back through the door, punch the close button"
        print "and blast the lock so the Gothons can't get out."
        print "Now that the bomb is placed you run to the escape pod to"
        print "get off this tin can."
        state.set_next(loc_escape_pod)

    def on_enter(state):        
        print "You burst onto the Bridge with the netron destruct bomb"
        print "under your arm and surprise 5 Gothons who are trying to"
        print "take control of the ship.  Each of them has an even uglier"
        print "clown costume than the last.  They haven't pulled their"
        print "weapons out yet, as they see the active bomb under your"
        print "arm and don't want to set it off."
        action_handler(state, [
            ("throw the bomb", on_throw),
            ("slowly place the bomb", on_place)
        ]

    state.set_next(on_enter)


def loc_escape_pod(state):
    def on_good_guess(state):
        print "You jump into pod %s and hit the eject button." % guess
        print "The pod easily slides out into space heading to"
        print "the planet below.  As it flies to the planet, you look"
        print "back and see your ship implode then explode like a"
        print "bright star, taking out the Gothon ship at the same"
        print "time.  You won!"
        state.set_next(loc_finished)
        
    def on_bad_guess(state):
        print "You jump into pod %s and hit the eject button." % state.pod_guess
        print "The pod escapes out into the void of space, then"
        print "implodes as the hull ruptures, crushing your body"
        print "into jam jelly."
        die(state)

    def on_enter(state):
        print "You rush through the ship desperately trying to make it to"
        print "the escape pod before the whole ship explodes.  It seems like"
        print "hardly any Gothons are on the ship, so your run is clear of"
        print "interference.  You get to the chamber with the escape pods, and"
        print "now need to pick one to take.  Some of them could be damaged"
        print "but you don't have time to look.  There's 5 pods, which one"
        print "do you take?"
        state.pod_good = randint(1,5)
        state.pod_guess = int(raw_input("[pod #]> "))

    if state.pod_guess==None:
        state.set_next(on_enter)
    elif state.pod_good==state.pod_guess:
        state.set_next(on_good_guess)
    else:
        state.set_next(on_bad_guess)

def loc_finished(state):
    print "You won! Good job."
    state.set_next(None)


def main():
    # we create a new instance of state, 
    # and pass it the starting location
    # the game loop can take it from there
    game_loop(State(loc_central_corridor_enter))

# the only code call in your module scope should be an entry point
# called main by convention
main()



While I worked off of ex43 for this, it does have more resemblance to ex41. However, it is object oriented to the extent I feel it should be.

I offer one object called State. This object stores everything in our program that can change. While it primarily stores a function called location, which takes the current state as a parameter, it also stores other values specific to our program. We can expand state to add more as we work on our program. Since location is not a string, but an actual object in scope, this offers clearer debugging. If your IDE is particularly clever, it might even warn you of an invalid location.

The game_loop has no knowledge of how state works, beyond that it offers two methods, is_done and next. Because, ultimately, that's all a game loop needs to know about.

The rest of the program follows a convention I've adopted, with an entirely new location, a function in global scope, called loc_X and sub locations called on_X. An important element of this is that functions needn't store anything, they are strictly immutable. The state object, that's where all the changes happen. Also, there is no road map for this. As long as you have a starting point, you're off and running. As new locations are created, they can be added simply without changing any other element of the program.

This is my take on a sensible version of Zed Shaw's Exercise 43. In the next post, I will use this framework to make my own text type adventure. (Actually, I'm planning on ripping off an old Java one I wrote.) I will show how to create things like inventories, using only your state object and a game loop.

1 Comments On This Entry

Page 1 of 1

albert003 Icon

25 April 2017 - 07:22 PM
Man, that makes a lot more sense than what Zed Shaw had in his book. I never understood why he initially had the game using only functions and then changed it so that instead of functions he had classes. I looked around online and couldn't find one example of someone making a game on python that used classes for everything. Thank you for the tutorial, between you and DK320 you guys just gave me a huge idea how to fix my current program I am working on. Keep up the good work.
0
Page 1 of 1

October 2017

S M T W T F S
1234567
891011121314
15161718192021
22 23 2425262728
293031    

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)