这是一个我似乎无法解决的问题。我是一个非常新的程序员,我喜欢编码,但是,我需要帮助这个非常基本的战斗系统,我相信你不会介意给我。它不好看或干净,所以任何关于如何缩短我的代码的提示也将非常感激。
local function battle() -- All of this is 100% unfinished, by the way
n = math.random(10) + 1 -- Everybody's HP, enemy HP randomly generated number from 10 to 100
enemyhp = 10*n
herohp = 100
io.write("Your HP: ")
io.write(herohp)
io.write(" ")
io.flush()
io.write("Enemy HP: ")
io.write(enemyhp)
io.write(" ")
io.flush()
if enemyhp <= 0 then
print("You won!")
end
local function attack() -- Attacking the enemy or running away
print("|Attack|Flee|")
input = io.read()
if input = "attack" then -- This is where my error is
attackdamage = math.random(51)
if attackdamage = 51 then
print("Critical Hit!")
enemyhp - 100
else
enemyhp - attackdamage
print("Enemy took ")
io.write(attackdamage)
io.write(" damage!")
elseif input = "flee" then
print("You ran away!")
end
end
end
谢谢。
答案 0 :(得分:0)
您错过了if
阻止的结束。它应该是
if enemyhp <= 0 then
print("You won!")
end
第二个问题是你指出的那一行:你需要使用两个等号(==
)而不是一个来比较:
if input == "attack" then
然后,当您为某个变量赋值时,您需要使用一个等号。否则它只是表达而没有意义。
enemyhp = enemyhp - 100
然后重复相同的错误(最后你还有一个end
)。完整代码,您可以编译/运行:http://ideone.com/dgPoZo
local function battle() -- All of this is 100% unfinished, by the way
n = math.random(10) + 1 -- Everybody's HP, enemy HP randomly generated number from 10 to 100
enemyhp = 10*n
herohp = 100
io.write("Your HP: ")
io.write(herohp)
io.write(" ")
io.flush()
io.write("Enemy HP: ")
io.write(enemyhp)
io.write(" ")
io.flush()
if enemyhp <= 0 then
print("You won!")
end
end
local function attack() -- Attacking the enemy or running away
print("|Attack|Flee|")
input = io.read()
if input == "attack" then -- This is where my error is
attackdamage = math.random(51)
if attackdamage == 51 then
print("Critical Hit!")
enemyhp = enemyhp - 100
else
enemyhp = enemyhp - attackdamage
print("Enemy took ")
io.write(attackdamage)
io.write(" damage!")
end
elseif input == "flee" then
print("You ran away!")
end
end