簡體   English   中英

使游戲以while循環運行

[英]Making a game run with a while loop

這個問題實際上是我昨天發布的問題的繼續。 我正在完成一個教程,並且花了很多時間讓我的“游戲”運行。 游戲是一組場景的類。 然后是一個旨在在每個場景中工作的引擎對象。 根據每個場景中玩家的輸入,下一個場景是變量。

注意,有些人已經說過,這可能不是如何使用類的最佳示例。 除了那個問題,我想將這個問題集中在特定的問題上。

我試圖直奔對象和類,但是在這種情況下,遇到麻煩時,我的麻煩是不了解如何創建確定應該運行哪個場景的變量。

next = self.scenes.get(next).enter()是由另一個SO用戶提供的。 作為一個初學者,如果我將其翻譯成英文,它將顯示為“從self.scenes dict獲取與當前“ next”變量有關的對象,然后運行它的enter方法。但是從代碼中,我不會看看接下來在整個游戲互動過程中會如何變化?同樣,它仍然會輸出錯誤。

  1. next的值在每個場景中如何變化? 我看不到機制。
  2. 如何使腳本運行? 它最初確實運行,但是當我用raw_input回答if問題時,第一個場景TheTrolls拋出了錯誤(如下)。

這是代碼終端輸出,下面是代碼:

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'

和腳本:

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

next的值是enter()方法的返回值。

出現錯誤時,請檢查next的值。 我懷疑這可能是"death" ,在一系列場景中不存在。 您還存在一個錯誤,其中回答trick根本不會返回任何內容,因此next將為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM