简单的Python游戏(坚持上课)

时间:2015-04-13 19:10:10

标签: python function class oop methods

我真的很依赖我创建的这个简单的用户输入游戏。这是从艰难的方式学习python的练习。我已经尝试了一个多星期来自己解决这个问题并最终屈服于寻求帮助。我认为我的代码中的问题点是Engine()类。无论如何这里是代码:

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()

现在我遇到了一个错误,指出我的Engine类中没有定义“island_map”。我不理解,因为对我来说它看起来像是定义的。任何意见都将不胜感激。

1 个答案:

答案 0 :(得分:3)

def play(self):

    current_island = island_map.opening_island()

需要更改为

def play(self):

    current_island = self.island_map.opening_island()

因为它是该实例的一部分