转为python文本冒险的计数器

时间:2014-11-21 01:04:52

标签: python

我已经编写了一个python文本冒险游戏,我想要添加的最后一件事是计算在比赛结束时显示的转弯数量的计数器。

每次玩家输入内容时只需计算一下,但我不知道如何编码,这有点令人尴尬,因为我确信这将是一个非常简单的解决方案

即时使用python 3.4.1

while True:
    playerInput = input("What do you want to do? ")
    playerInput = playerInput.lower()
    playerWords = playerInput.split(" ", 1)
    verb = playerWords[0]
    if len(playerWords) == 2:
        noun = playerWords[1]
    else:
        noun = ""


    if playerInput == "quit":
        break



    elif playerInput == "look":
        print(roomDescriptions[currentRoom])



    ##--Controls movement--##             
    elif playerInput in dirs:
        playerInput = playerInput[0]
        if "treasure" in invItems and playerInput == "s" and currentRoom == "strangeWall":##--Checks for treasure in inventory before allowing game to be won--##
            print("!!!!Congratulations you have escaped from the dark dungeon!!!!")
            break

        elif playerInput in roomDirections[currentRoom]:
            currentRoom = roomDirections[currentRoom][playerInput]
            print(roomEntrance [currentRoom])
        else:
            print("You can't go that way")



    elif playerInput == "lookdown":##--checks for room items on the ground--##
        printList ("You see;", roomItems[currentRoom])



    elif playerInput == "inventory" or playerInput == "inv":##--Displays inventory items--##
        printList ("You are carrying;", invItems)



    elif verb == "get":##--Controls picking up items and adding them to inventory/removes from room--##
        if noun in roomItems[currentRoom]:
            print("picked up", noun)
            invItems.append(noun)
            roomItems[currentRoom].remove(noun)
        else:
            print("There is nothing to pick up")



    elif verb == "drop":##--Controls dropping items and removing them from the inventory/adds to room items--##
        if noun in invItems:
            print("You drop the", noun)
            roomItems[currentRoom].append(noun)
            invItems.remove(noun)
        else:
            print("You are not carrying", noun)


    elif verb == "use":##--Controls using the lamp and snow boots--##
        if noun in invItems:##--Checks inventory for lamp or snowboots before allowing certain directional movement--##
            if noun == "lamp":
                print("You light the lamp")
                invItems.remove(noun)
                roomDirections["hallMid"]["e"] = "giantNature"

            elif noun == "snowboots":
                print("You put on the snowboots")
                invItems.remove(noun)
                roomDirections["hallMid"]["s"] = "snowRoom"
            else:
                print("You cannot use that")
        else:
            print("You do not have", noun)





    else:
        print ("I don't understand")

2 个答案:

答案 0 :(得分:1)

如果没有看到代码的示例,几乎不可能告诉您在代码中可以使用的任何具体内容。

但我可以给你一些通用的东西,你可以适应你的代码。

class CountedInput(object):
    def __init__(self):
        self.counter = 0
    def input(self, *args):
        self.counter += 1
        return input(*args)

counted_input = CountedInput()

现在,您在代码中调用input()的任何地方,而是致电counted_input.input()

当你想要显示转弯计数器时,那只是counted_input.counter

(如果您使用的是Python 2.x,请将input更改为raw_input。)


现在您已经在问题中添加了一个示例:

这个建议可以正常使用,但你可以让事情变得更简单。

您的整个游戏都是围绕命令循环构建的。每个循环只调用input一次。所以,你需要做的就是计算你循环多少次。你可以这样做:

counter = 0
while True:
    counter += 1
    playerInput = input("What do you want to do? ")
    # all the rest of your code

现在,您只需打印或以其他任何变量使用counter。例如:

    elif playerInput == "score":
        print("You have 0/0 points after", counter, "turns")

(我猜你实际上并不想在没有得分的情况下用score命令来哄你的球员,但这应该会向你显示理想状态。)


如果你想变聪明,有一种更简单的方法:只需将所有数字从1循环到无穷大。怎么样? count函数,有点像range,但没有stop值,因为它永远不会停止:

from itertools import count

for counter in count(1):
    # the rest of your code

答案 1 :(得分:0)

我知道很多人可能不喜欢这个想法,因为我已经看到了关于全局变量使用的矛盾观点,但是我会使用全局变量进行转数计数存储,并使用全局函数来跟踪它。

例如:

global turn_counter
turn_counter = 0

然后,当采取行动时,你可以这样做:

turn_counter += 1

我相信你需要在你的函数中包含全局。

示例:

def game_input_handler():
    """ handles all user input """
    global turn_counter

    // game prompt code here
    // if..elif...else for option handling