简体   繁体   中英

Making a game run with a while loop

This question is really a continuation of a question I posted yesterday. I'm working through a tutorial and am having tricky time getting my "game" to run. The game is a set of scenes that are classes. Then there is an engine object that is meant to work through each scene. Depending on the players input in each scene, the next scene played is a variable.

Note that some folk already said that this may not be the best example of how to use classes. That question aside, I'd like to focus this question on something specific.

I'm trying to get my head around objects and classes as it is, but in this instance, when it comes down to it, the trouble I have is not understanding how to create a variable that determines what scene should run.

The line next = self.scenes.get(next).enter() was kindly given to me by another SO user. As a beginner, if I was to translate it into english it would read "from the self.scenes dict get the object associated with what the "next" variable currently is and then run it's enter method. But from the code, I do not see how next changes throughout game interaction? Also, as it is, there is an error output.

  1. How does the value of next vary from scene to scene? I don't see the mechanism.
  2. How do I get the script to run? It does run initially, but the first scene, TheTrolls, throws back an error (below) when I answer the if question with raw_input.

Here is the code terminal output and the code is below that:

Traceback (most recent call last):
  File "ex45.py", line 90, in <module>
    GameEngine().run()
  File "ex45.py", line 20, in run
    next = self.scenes.get(next).enter()
AttributeError: 'NoneType' object has no attribute 'enter'

And the script:

from sys import exit

class Scene(object):

    def enter(self):
        pass

class GameEngine(object):

    def __init__(self):
        self.scenes = {'TheTrolls':TheTrolls(), 'MistyMountainsAndGollum':MistyMountainsAndGollum(), "Smaug":Smaug()}

    def run(self):

        next = 'TheTrolls'
        while True:
            if next == 'finished':
                print "Well done! You won!"
                exit(1)
            next = self.scenes.get(next).enter()

# the scenes
class TheTrolls(Scene):

    def enter(self):
        print "You encounter a group of 3 trolls who sneak upon the company while asleep."
        print "The trolls are discussing how to eat the dwarves."
        print "You remember that trolls cannot survive in sunlight."
        print "You must find a way to distract them till sunrise."
        print "You also notice in their posession a shiny object"
        print "Do you 1\)Fight them, 2\) Tell them jokes or 3\) Trick them"

        trollAction = raw_input("What do you do? ")

        if "fight" in trollAction:
            print "The trolls grab you and fight over who gets to eat you."
            print "They pull you apart limb by limb"
            return 'death'

        elif "joke" in trollAction:
            print "You think of a joke to keep the trolls amused."
            print "You run out at the trolls and yell 'A man didn't like his haircut, but it started to grow on him.'"
            print" The trolls don't laugh at your joke and grab you and then eat you."
            return 'death'

        elif "trick" in trollAction:
            print "By throwing your voice and talking like a troll you confuse the trolls."
            print "You manage to make them fight amonst themselves"
            print "The sun rises fro behind a rock and turns the trolls to stone."
            print "Do you get out of there as fast as you can or take a look around the trolls posessions?"

        else:
            print "Does not compute"
            return 'MistyMountainsAndGollum'

class MistyMountainsAndGollum(Scene):

    def enter(self):
        print "Since the trolls the company has been to Rivendel and have continued their journey."
        print "You are now in the Misty Mountains."
        print "Goblins come and chase you and you get seperated from everyone else."
        print "While trying to find your way back you come across a lake deep in the mountain."
        print "In the dark, you sit in despair and notice a metal object on the ground which you grab." 
        print "You grab the object and realise that it's a ring."
        print "You hear someone talking. You have encountered Gollum."
        print "Gollum talks to you, threatens to eat you but then agrees to a game of riddles because he is, in fact, just really bored and lonely."
        print "[I'll add some riddles later but for now just complete the game - your programer]"
        print "You play a game of riddles and Gollum is winning, till, you put your hand in your pocket and say"
        print "aloud \"What have I got in my pocket?\"."
        print "Golum freaks out but you put the ring on which makes you invisible."
        print "In trying to chase you Gollum actually leads you out of the cave as you follow him."

        return 'Smaug'

class Smaug(Scene):

    def enter(self):
        print "Something about chatting with Smaug and then getting th Arkenstone."     
        print "You got the Arkenstone from Smaug (Admittidley prematurely by the book version)."
        print "Finished"
        return 'finished'

class death(Scene):

    def enter(self):
        print "Well you did something wrong."
        print "Game Over"
        exit(1)

GameEngine().run()

The value of next is the return value of the enter() method.

Check the value of next when you get the error. I suspect that it may be "death" , which is not present in the array of scenes. You also have a bug where answering trick won't return anything at all, so next will be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM