简单 Python 游戏(停留在 类)

Simple Python Game (stuck on classes)

我真的沉迷于我创建的这个简单的用户输入游戏。这是通过艰难的方式学习 python 的练习。一个多星期以来,我一直在尝试自己解决这个问题,最后屈服于寻求帮助。我认为我的代码中的问题点是 Engine() class。无论如何,这是代码:

from sys import exit
from random import randint

class Island(object):
    def enter(self):
        pass


class Engine(object):
    def __init__(self, island_map):
        self.island_map = island_map

    def play(self):
        current_island = island_map.opening_island()
        last_island = self.island_map.next_island('Tropical')

        while current_island != last_island:
            next_island_name = current_island.enter()
            current_island = self.island_map.next_island(next_island_name)

        current_island.enter()


class Loser(Island):
    snippets = ["Welcome to loser Island",
                "Can't win them all", 
                "There's always next time"]

    def enter(self): 
        print "Game over"
        print Loser.snippets[randint(0,len(self.snippets)-1)]
        exit(1)


class Jungle(Island):
    def enter(self):
        print "Welcome to the Jungle!"
        print "You must fight the monkey to pass!"
        print "Choose your weapon..."

        weapons = ['stick', 'fly swatter', 'banana']

        print weapons

        fighter_weapon = raw_input("Choose from a a weapon above...")

        if fighter_weapon == 'stick':
            print "Stick can't beat a monkey!"
            print "Monkey wins"
            return 'Loser'
        elif fighter_weapon == 'fly swatter':
            print "The monkey steals your fly swatter"
            print "Monkey wins"
            return 'Loser'
        elif fighter_weapon == 'banana':
            print "The monkey is hypnotized by the banana"
            print "You continue to the next island..."
            return 'Frozen'
        else:
            print "What? Doesn't make sense"
            return 'Jungle'


class Frozen(Island):
    #add green, blue circle, and black diamond
    def enter(self):
        print "This is snow land"
        print "Here's your snowboard, choose your path"
        print "([[[[[[[[[)"

        runs = ["green", "blue", "black"]

        print runs

        run_choice = raw_input(" >")

        if run_choice == 'green':
            print "Easy way out?"
            print "No good"
            print "Wrong"
            return 'Loser'

        elif run_choice == 'blue':
            print "okay, at least not the easiest way"
            print "You have some guts, I'll let you choose one more time"
            return 'Frozen'

        elif run_choice == 'black':
            print "I like your style"
            print "Going for the hard choice shows courage"
            print "Continue to the next island"
            return 'Tropical'

        else:
            print "Say whaaat?!"
            return 'Frozen'


class Tropical(Island):
    def enter(self):
        print "You made it to the final Island!"
        print "Look here's the treasure chest!"
        print " All that's left to do is guess the code on the chest"
        print "Be smart, you only get five guesses..."

        t_key = "1234"
        chest_guess = raw_input("What do you think it is?")
        guesses = 0

        while chest_guess != t_key and guesses < 4:
            print "Guess again"
            guesses += 1
            chest_guess = raw_input(" ?")

        if chest_guess == t_key:
            print "You guessed right!"
            print "You won"

        else:
            print "Sorry"
            return 'Loser'      


class Map(object):
    islands = {
            'Loser': Loser(),
            'Jungle': Jungle(),
            'Frozen': Frozen(),
            'Tropical': Tropical(),
            }

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

    def next_island(self, island):
        val = Map.islands.get(island)
        return val

    def opening_island(self):
        return self.next_island(self.start_island)


mj_map = Map('Jungle')
mj_game = Engine(mj_map)
mj_game.play()

现在我遇到了一个错误,指出 "island_map" 未在我的引擎 class 中定义。我不明白,因为对我来说它看起来像是被定义了。如有任何意见,我们将不胜感激。

def play(self):

    current_island = island_map.opening_island()

需要改为

def play(self):

    current_island = self.island_map.opening_island()

因为它是那个实例的一部分