EX43, why do we need an engine?

Hi all,
I’m going through the ex 43 in LPTHW and was wondering why do we need an engine at all ?
Mainly because I’m not able to completely comprehend the engine code.
I tried to do it without the engine and it works…

from sys import exit
from random import randint
from textwrap import dedent

class Scene(object):

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

class Death():

    def enter(self):
        quips = [
            "Taunt1: You died. You kinda suck at this.",
            "Taunt2: Your mom would be proud... if she were smarter.",
            "Taunt3:  hdh"
            ]
        print(quips[randint(0, len(quips)-1)])
        exit(1)

class CentralCorridor(Scene):

    def enter(self):
        print(dedent("""
            The Gothons of Planet Percal #25 have invaded your ship and
            destroyed your entire crew. You are th last surviving member and your last
            mission is to get the neutron destruct bomb from the Weapons Armory,
            put it in the bridge, and blow the ship after getting into the
            escape pod.

            You're running down the central corridor to the Weapons
            Armory when a Gothon jumps out, red scaly skin, dark grimy
            teeth, and evil clown costume flowing around his hate filled body.
            He's blocking the door to the Armory and about to pull a weapon to
            blast you!!!.
        """))

        action = input("> ")

        if action == "shoot!":
            print(dedent("""
                    You're dead

            """))
            return Death.enter(self)

        elif action == "dodge":
            print(dedent("""
                you dodge, you die

            """))
            return Death.enter(self)

        elif action == 'tell a joke':
            print(dedent("""
                Joke: Good one!
                jump through the weapon aromory door.
            """))
            return LaserWeaponArmory.enter(self)

        else:
            print("Does not Compute")
            return CentralCorridor.enter(self)


class LaserWeaponArmory(Scene):
    def enter(self):
        print(dedent("""
        You do a dive and get in.
        There's a keypad lock on th box ad you need the code to
        get the bomb out. If you get the code wrong ten times then
        the lock closes forever and you can't get the bomb. The
        code is 3 digits
        """))

        code = f"{randint(1,9)}{randint(1,9)}{randint(1,9)}"
        guess = input("[keypad]>")
        guesses = 0

        while (guess != code and int(guess) != 999) and guesses < 10:
            print("BZZZZZEDDD!")
            guesses += 1
            guess = input("[keypad]>")
        if guess == code or int(guess) == 999:
            print(dedent("""
            You guessed right code you advance to bridge
            """))
            return TheBridge.enter(self)
        else:
            print(dedent("""
            the lock buzzes last time and you die
            """))
            return Death.enter(self)



class TheBridge(Scene):

    def enter(self):
        print(dedent("""
        On bridge there are 5 more gothons
        """))
        action = input("What do you do > ")

        if action == "throw the bomb":
            print(dedent("""
                You failed your mission
            """))
            return Death.enter(self)

        elif action == "slowly place the bomb":
            print(dedent("""
            You get into stealth mode and place the bomb safely!!
            """))

            return EscapePod.enter(self)

        else:
            print("Does not Compute")
            return TheBridge.enter(self)



class EscapePod(Scene):

    def enter(self):
        print(dedent("""
        There are 5 pods....
        Which one do you take??
        """))
        good_pod = randint(1,5)
        guess = input("[pod # ]> ")

        if guess != good_pod and int(guess) != 9:
            print(dedent("""
            wrong pod..... you die
            """))
            return Death.enter(self)
        else:
            print(dedent("""
            You chose the right . You won!!!
            """))
            return Finished.enter(self)

class Finished(Scene):

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


game = CentralCorridor()
game.enter()

Exactly, why do you even need it? Only reason is if you wanted to have a looser set of rooms, or maintain something between rooms.

Isn’t it also the case that we don’t need Scene() since none of the subclasses are dependent upon anything in that superclass in order to function? I left Engine() intact but removed Scene() and changed all the individual scenes so that they were Class(obect) instead of Class(Scene) and it worked fine.

Don’t stop now: As it is now, is there any reason for the scenes to be classes at all? Can you make it work only with functions?

Probably but then Zed loses an Exercise from his book because we’ve reverted from Object Oriented programming to Procedural programming… :joy:

Zed is totally alright with you rewriting this to use zero OOP.

For me it was kind of confusing too, in the beginning. When I started to understand how OOP in Python works, I started liking the structure and helicopter-view the objects like the engine offer. Like: switching from scene to scene could be seen as a game mechanic, and I like controlling those mechanics from the engine. So when I look for it later, I (hopefully) remember that this functionality ‘belongs’ to the engine. While expanding the game in later excercises I also used the engine to control the player inventory and to prepare a game history file that every scene prints a line into. The engine became a helpful thing that controls some other objects.

1 Like

I guess that’s the best way to put it @Tim. If you just want to make this game work, then the Engine is pointless. All of the OOP is pointless since you could write this game as a giant if-statement like the first game.

The purpose of the engine is for the future where you want the game to do more complex things, but we don’t really get into that in the book.

It’s also a great exercise to simplify away the Engine to see how that works.