2017-05-25 148 views
2

首先,我知道这是一个混乱,无组织的一堆Lua,但我仍在学习,所以请原谅我。功能和while语句创建无限打印循环

我的问题是: 我在iPad的TouchLua +上运行我的代码,它运行的很顺利,直到我回到tinput = io.read(),无论是输入'attack'还是'run away'I最终会在屏幕上印上“这不是一个有效的命令”,这是无限的。

我认为这是while语句中的get_input()与我已经将tin.read()定义为tinput而不是整个脚本的标准输入之间的冲突。

我可以接受这一点,除了我从中学习的脚本(目前的YouTube视频)没有这样做的事实,我已经比较了两者和功能,以及'如果能够'和他们写的完全一样。

我试图找到答案在谷歌上,视频和原始脚本存储在github上的意见,但我不明白github,但谷歌并没有帮助。

我做了什么错误,或者错过了,或者一般搞砸了......

另外,请原谅我的大量使用的意见,对不起。

print("Welcome to the game") 

    -- expilictly set the variable to an empty string 
    -- because it is used in a 'while' loop 
    input = "" 
    inv = {"sword", "coin", "armour"} 

    -- inventory function 
    function get_inv() 
     for i, v in pairs(inv) do 
      print(i .. " " .. v) 
     end 
    end 

    -- invalid command function 
    function inv_com() 
     print("You did not type a valid command...") 
    end 

    -- function to simplify 'while' statement throughout 
    -- entire programme 
    function get_input() 
     print("What do you want to do?") 
     i = io.read() -- get what the user types 
     return i 
    end 

    -- fuction adding item to inventory 
    function push_inv(item) 
     table.insert(inv, item) 
    end 

    -- function removing item from inventory 
    function pop_inv(item) 
     for i, v in pairs(inv) do 
      if v == item then 
       table.remove(inv, i) 
      end 
     end 
    end 

    -- '~=' means not equal to 
    while input ~= "leave cave" do 
     input = get_input() -- get what the user types 

     -- '==' means equal to 
     if input == "inspect" then 
      print("You are in a cave") 
     elseif input == "leave cave" then 
      print("You leave cave") 
     elseif input == "inv" then 
      get_inv() 
     else 
      inv_com() 
     end 
    end 

    input = "" 
    while input ~= "follow path" do 
     input = get_input() 

if input == "inspect" then 
    print("You are at the base of a hill. There is a path") 
elseif input == "follow path" then 
    print("You follow the path") 
    print("A troll appears wielding an axe") 
    print("What do you want to do") 
    tinput = io.read() 
    if tinput == "attack" then 
     print("You smack it, and it falls dead") 
    elseif tinput == "run away" then 
     print("You cowardly run away and it smack you, and steals your coin") 
     pop_inv("coin") 
    else 
     print("You stand there, it stabs you, you die") 
     os.exit() 
    end 
elseif input == "inv" then 
    get_inv() 
else 
    inv_com() 
end 
    end 


    input = "" 

    -- a boolean value can only have 1 of 2 values 
    -- 'true' or 'false', this works great for the have_key 
    -- value because there are only 2 possibilities: 
    -- you either have the key or you don't. 
    have_key = false 

    -- this stement mean: while this statement: 
    -- 'input == "open gate" 
    -- and have_key' is true 
    while not (have_key == true and input == "open gate") do 
input = get_input 


if input == "inspect" then 
    --- if we dont have the key, tell us 
    if have_key == false then 
     print("there is a path behind you, a gate infront of you and key is hidden in the grass") 
     -- if we have the key, dont tell us 
    elseif have_key == true then 
     print("There is a path behind you, and a gate infront of you") 
    else 
     print("you did not put a valid command") 
    end 

    -- we now have the key, set variable to true 
elseif input == "grab key" then 
    have_key = true 
    print("You grabbed the key") 
    table.insert(inv, "gate key") 
elseif input == "inv" then 
    get_inv() 
elseif input == "open gate" then 
    if have_key == true then 
     print("You've escaped") 
    elseif have_key == false then 
     print("The gate is locked") 
    end 
elseif input == "pick up magic" then 
    push_inv("MAGIC") 
elseif input == "inv" then 
    get_inv() 
else 
    inv_com() 
end 
    end 


    print("You won the game. Please leave!") 

回答

2

你的一个while循环中有代码

input = get_input 

这应该是

input = get_input() 

发生了什么事是你已经设置的变量输入等于功能 get_input ,而不是调用它的结果,所以输入永远不会是有效的字符串,它总是会调用inv_com。


编辑:我知道,这是问题的范围之外,但我有一对夫妇的提示对于特定问题之外你的程序。

  1. 在命名您的函数时请小心,以免造成歧义。您有用于库存管理的功能,如pop_inv和push_inv,但同时具有inv_com用于无效命令。
  2. 阅读Lua style guide。你的代码有一些非常疯狂的缩进,这让其他人很难理解正在发生的事情。
  3. 试着思考如何将所有这些代码转换为可能处理所有可能性的主循环,而不是一个接一个的循环和if语句。想想如果你想在以后增加更多的功能,你的程序结构将会变得多么复杂。

你在做什么让我想起了很多我如何开始编程,使文本冒险。如果你对文字冒险感兴趣,可以看看Zo​​rk,看看他们的游戏循环是如何工作的。另外做一些关于状态机的研究,因为它似乎对你现在正在构建的东西有用。快乐编码:)

+0

如果我的回答有帮助,可否请您将其标记为已接受?如果它仍然处于无限循环状态,我很乐意再看看并相应地编辑我的答案。 –