Python 游戏后不玩房间段的方向功能
我创建了代码,游戏开始运行得很好,但是在我设置了一个方向后,它不会进入房间。我已经检查了这两个函数是否独立工作,它们是否工作正常,我只是不确定它们如何连接在一起Python 游戏后不玩房间段的方向功能,python,python-3.x,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",
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())
导入时间
导入系统
随机输入
前进=[“F”,“F”,“前进”,“前进”,“前进”]
向后=[“B”,“B”,“向后”,“向后”,“向后”,“向后”]
左=[“L”、“L”、“左”、“左”、“左”]
右=[“R”,“R”,“右”,“右”,“右”]
方向L=右+左+后+前
健康=10
def延迟打印:
对于s中的c:
sys.stdout.write(“%s”%c)
#sys.stdout.flush()
睡眠时间(0.10)
定义名称():
name=input('嘿,你是谁?')
返回名称
定义方向():
方向=输入(“您希望前进、向左、向右还是向后?”)
当方向不在方向L中时:
方向=输入(“请输入向前、向左、向右或向后。”)
其他:
打印(“你去了”,方向)
def室():
全球卫生
a=随机。randint(1,8)#1=怪物2=出口3-4=幽灵5-8=空
如果a==1:
延迟打印('你遇到怪物,失去5点生命')
健康=健康-5
elif a==2:
延迟打印(“你逃出了地牢”)
如果a==3或a==4:
延迟打印(“您遇到了幽灵”)
x=random.randint(1,11)
y=random.randint(1,11)
打印((x),“+”,(y))
答案=int(输入(“答案是什么”))
realanswer=x+y
如果答案==真实答案:
打印(“这是正确的答案”)
健康=健康+2
其他:
打印(“错误答案,答案是”realswer“!”)
健康=健康-2
如果a==5或a==6或a==7或a==8:
打印('这个房间是空的')
健康=健康-1
返回打印(“您有”,健康,“HP左”)
如果健康状况分析
它没有进入房间,因为它给了你错误信息。你的压痕有几个地方是错误的,最后的致命原因是:
room(direction())
这表示“调用方向,并使用其返回值调用房间”。但是,方向不返回值,房间不接受值
进步
使用增量编程。您的困惑部分来自于在没有正确测试链接的情况下编写超过50行代码。写几行,测试它们,确保它们工作,直到你解决了问题才继续
在这种情况下,将你假定正在工作的个人例行程序放在一边一段时间。尝试使用“存根”例程在迷宫中前进,这些代码只做一件合理的事情。例如:
def direction():
return "L"
def room():
print ("You entered another room.")
从这里开始,学习如何从一个房间移动到另一个房间
我该怎么做
研究,就像Stack Overflow intro教程告诉您的那样。互联网上有无数的地牢爬行游戏。例如,您可以从“艰苦学习Python”中获取模板。您使用哪个调试器逐步完成代码?