游戏没有在方向功能后播放房间部分

时间:2017-04-25 18:36:45

标签: python python-3.x

我创建了代码并且游戏开始工作正常,但是在我放入方向后它不会进入房间。我已经检查过两个函数是否都可以自行运行并且它们工作正常,我只是不确定它们是如何连接在一起的。

import time
import sys
import random

Forward = ["F", "f", "forward", "FORWARD", "Forward"]
Backwards = ["B", "b", "back", "BACK", "backwards", "BACKWARDS"]
Left = ["L", "l", "LEFT", "left", "Left"]
Right = ["R", "r", "right", "RIGHT", "Right"]
directionL = Right + Left + Backwards + Forward

health = 10

def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        #sys.stdout.flush()
        time.sleep(0.10)
def name():
    name = input('Hey, Who are you? ')
    return name

def direction():
    direction = input("What direction would you like to go: Forward, Left, Right or Backwards?")
    while direction not in directionL:
        direction = input ("Please enter Forward, Left, Right or Backwards.")
    else:
        print ("You went", direction)
def room():
    global health
    a=random.randint(1,8) #1=Monster 2=Exit 3-4=Ghost 5-8=Empty
    if a==1:
        delay_print('You encountered the monster you lose 5HP ')
        health=health-5
    elif a==2:
        delay_print('You escaped the dungeon ')
    elif a==3 or a==4:
        delay_print('You encountered the ghost ')
        x=random.randint(1,11)
        y=random.randint(1,11)
        print((x),'+',(y))
        answer=int(input("What is the answer"))
        realanswer = x+y
        if answer==realanswer:
        print("That's the correct answer")
        health = health+2
    else:
        print("Wrong answer, the answer was",realanswer,"!")
        health = health-2
elif a==5 or a==6 or a==7 or a==8:
    print ('This room is empty')
    health = health-1  
return print("You have", health,"HP left ")

if health <1:
    print ("Game Over")


print('You hear a voice echo in the background.')
delay_print('H...')
delay_print('Hey...')
delay_print('Welcome %s you are stuck in this labyrinth and you need to 
escape.' % name())
print ('\n')
room(direction())

1 个答案:

答案 0 :(得分:0)

<强>分析

由于它给你的错误信息,它没有进入房间。你的缩进在几个地方都是错误的,最终的死亡就在这里:

room(direction())

这表示“呼叫方向并使用其返回值来呼叫房间”。但是,方向不会返回值,会议室也不会接受。

<强>进步

使用增量编程。您的部分困惑来自编写超过50行代码而无需正确测试链接。写几行,测试它们,确保它们有效,并且在你解决问题之前不要继续。

在这种情况下,将你所谓的工作个人惯例暂时搁置一边。尝试使用“存根”例程在迷宫中取得进展,这些例程只做一件合理的事情。例如:

def direction():
    return "L"

def room():
     print ("You entered another room.")

从这里开始,研究如何从一个房间移动到另一个房间。

我该怎么做

研究,就像Stack Overflow介绍之旅告诉你的那样。互联网上有无数的地下城爬行游戏。例如,您可以从“以艰难的方式学习Python”中获取模板。