【问题标题】:Simple Python Game (stuck on classes)简单的 Python 游戏(坚持上课)
【发布时间】:2015-04-13 19:10:09
【问题描述】:

我真的沉迷于我创建的这个简单的用户输入游戏。这是一个艰难地学习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()

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

【问题讨论】:

  • edit您的问题并发布您遇到的错误的全文
  • 你忘记在Engine.play()方法的这一行添加selfcurrent_island = island_map.opening_island()应该是self.current_island

标签: python function class oop methods


【解决方案1】:
def play(self):

    current_island = island_map.opening_island()

需要改成

def play(self):

    current_island = self.island_map.opening_island()

因为它是该实例的一部分

【讨论】:

  • 谢谢大家!我现在已经开始工作了。太令人沮丧了,就在我的眼皮底下,这是一个如此简单的修复!
猜你喜欢
  • 2014-06-18
  • 2012-05-12
  • 2020-09-25
  • 1970-01-01
  • 2017-12-17
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多