-1
这是我第一次编写的代码,因为我刚刚开始,所以我不确定是否有任何内容正确缩进。但我想要一种能够重复文本的方式,但我不知道如何去做。如何在Lua中重复代码?
print("Hey there. I want to play a game.")
print("Tell me your name")
print("Type your name")
name = io.read()
print("Oh, what a nice name. I was gonna call my kid the same")
print("Ok. So as you can see, there are no ligths here. It's all dark. You have three choices. Go west, go east, go north.")
print("To go North, type 'n'. To go East, type 'e'. to go West, type 'w'.")
repeat
direction = io.read()
if direction == "w" then
print(" You see a light switch.")
print(" type 'switch'to turn on the switch.")
print(" type 'go_back' to turn around")
best = io.read() -- This is where i want it to be able to repeat
end
if best == "switch" then
print(" The light goes on.")
print(" As the light glows stronger, you see someone standing infront of you")
print("Before you know it, he slits your throat. Want to try again?")
print("Press enter to try again")
try_again = io.read()
best = io.read()
direction = io.read()
elseif best == "switch" then
print(" The light goes on.")
print(" As the light glows stronger, you see someone standing infront of you")
print("Before you know it, he slits your throat. Want to try again?")
print("Press enter to try again")
try_again = io.read()
end
end
until best ~= "go_back" -- to repeat the code up above
类型'如何重复代码lua'到谷歌 – zeromus
如果您缩进代码正确它会显示有一个额外的'end'之前' until'。关于重复代码,请将要重复的字符串放入函数中并根据需要调用它们。在Lua中搜索“多重字符串”,它可以很方便。 –