如何在lua中添加技能点限制器?

发布于 2025-01-10 10:43:13 字数 2286 浏览 2 评论 0原文

好吧,所以我正在用 lua 构建一个基于文本的冒险...如果有人认为这是一个坏主意,我是坏主意的大师,但问题是关于升级功能...所以这里是

  function lvl()
    if xp >= xpr then
      level = level + 1
      xpr = xpr * 1.5
      xp = 0
      io.write("Congrats! Somehow you didn't die and lose everything!", "\n")
      io.write("Your level is now:" .. level .. "\n")
      sp = sp + 6
      io.write("Do you want to allocate your " .. sp .. " skill points?", "\n")
      sac = io.read()
      if sac == "yes" then
          io.write("How much do you want to put into strength?", "\n")
          strsp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            str = str + strsp
            io.write("Strength: ", str, "\n")
          end
          io.write("How much do you want to put into dexterity?", "\n")
          dexsp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            dex = dex + dexsp
            io.write("Dexterity: ", dex, "\n")
          end
          io.write("How much do you want to put into constitution?", "\n")
          consp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            con = con + consp
            io.write("Constitution: ", str, "\n")
          end
          io.write("How much do you want to put into intelligence?", "\n")
          intsp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            int = int + intsp
          end
          io.write("How much do you want to put into wisdom?", "\n")
          wissp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            wis = wis + wissp
          end
          io.write("How much do you want to put into charisma", "\n")
          chasp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            cha = cha + chasp
          end
          
    elseif xp < xpr then
      io.write("You didn't level up!", "\n")
    end
  end

和问题出在技能分配制度上。我不知道如何限制您花费的技能点数量...提前谢谢您!

附: 我对lua很陌生。

Ok so I'm building a Text-based adventure in lua... If anyone thinks that is a bad idea well I am the master of bad ideas but the question is about the level up function... So here it is

  function lvl()
    if xp >= xpr then
      level = level + 1
      xpr = xpr * 1.5
      xp = 0
      io.write("Congrats! Somehow you didn't die and lose everything!", "\n")
      io.write("Your level is now:" .. level .. "\n")
      sp = sp + 6
      io.write("Do you want to allocate your " .. sp .. " skill points?", "\n")
      sac = io.read()
      if sac == "yes" then
          io.write("How much do you want to put into strength?", "\n")
          strsp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            str = str + strsp
            io.write("Strength: ", str, "\n")
          end
          io.write("How much do you want to put into dexterity?", "\n")
          dexsp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            dex = dex + dexsp
            io.write("Dexterity: ", dex, "\n")
          end
          io.write("How much do you want to put into constitution?", "\n")
          consp = io.read()
          if sp == 0 then
            io.write("You don't have any skill points!", "\n")
          else
            con = con + consp
            io.write("Constitution: ", str, "\n")
          end
          io.write("How much do you want to put into intelligence?", "\n")
          intsp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            int = int + intsp
          end
          io.write("How much do you want to put into wisdom?", "\n")
          wissp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            wis = wis + wissp
          end
          io.write("How much do you want to put into charisma", "\n")
          chasp = io.read()
          if sp <= 0 then
            io.write("You don't have any skill points!", "\n")
          else
            cha = cha + chasp
          end
          
    elseif xp < xpr then
      io.write("You didn't level up!", "\n")
    end
  end

and the problem lies within the skill allocation system. I cannot figure out how to limit the amount of skill points you spend... Thank you in advance!

PS:
I'm very new to lua.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-01-17 10:43:13

将字符串从 io.read() 转换为数字...

io.write("How much do you want to put into strength?", "\n")
          strsp = tonumber(io.read())
          if sp < strsp then -- check input is not more than sp
            io.write("You don't have enough skill points!", "\n")
          else
            sp = sp - strsp -- substract from sp
            str = str + strsp -- add to str
            io.write("Strength: ", str, "\n")
          end

PS: 向用户提供有关 sp 数量和当前 str 的信息。< br>
例如使用 format() 方法...

io.write(("%s %d %s%d%s: "):format("How much from", sp, "do you want to put into strength? (", str, ")"))

Convert the string from io.read() to a number...

io.write("How much do you want to put into strength?", "\n")
          strsp = tonumber(io.read())
          if sp < strsp then -- check input is not more than sp
            io.write("You don't have enough skill points!", "\n")
          else
            sp = sp - strsp -- substract from sp
            str = str + strsp -- add to str
            io.write("Strength: ", str, "\n")
          end

PS: Give user info about amount of sp and current str.
For example with format() method...

io.write(("%s %d %s%d%s: "):format("How much from", sp, "do you want to put into strength? (", str, ")"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文