当在if内部时,随机值不是随机的

  • 本文关键字:随机 if 内部 当在 lua
  • 更新时间 :
  • 英文 :


这是我在lua中玩的一个小游戏,伤害是一个math.random。但当我把让玩家攻击的函数放在一个如果它不再是随机的。

它的作用如下:攻击敌人,造成22点伤害!当前敌人生命值为78敌人准备进攻!敌人对你造成19点伤害!你目前的健康状况是81袭击

攻击敌人,造成22点伤害!当前敌人生命值为78敌人准备进攻!敌人对你造成19点伤害!你目前的健康状况是81袭击

攻击敌人,造成22点伤害!当前敌人生命值为78敌人准备进攻!敌人对你造成19点伤害!你目前的健康状况是81袭击

正如你所看到的,它不是随机的,有什么修复方法吗?

math.randomseed(os.time())
player_attacked = 0
enemy_attack_time = 0
enemy_attack = 0
player = {}
player.health = 100
player_health = player.health
player.damage = math.random(0,25)
player_damage = player.damage

enemy = {}
enemy.health = 100
enemy_health = enemy.health
enemy.damage = math.random(5, 30)
enemy_damage = enemy.damage
function player.init_attack()
print('Attack? ')
wanna_attack = io.read()
if wanna_attack == 'y' then
print('Attacked enemy with '..player_damage..' damage! current enemy health is '..enemy_health - player_damage)
else
os.exit()
end
player_attacked = 1
end
function enemy.init_attack()
print('Enemy Attacked you with '..enemy_damage..' damage! your current health is '..player_health - enemy_damage)
if player_health <= 0 then
os.exit()
end
end
player.init_attack()
if player_attacked == 1 then
print('Enemy getting ready to attack!')
enemy.init_attack()
player_attacked = 0 
end
while player_health ~= 0 or enemy_health ~= 0 do
player.init_attack()
if player_attacked == 1 then
print('Enemy getting ready to attack!')
enemy.init_attack()
player_attacked = 0 
end
end

OP代码将player.damageenemy.damage值设置为随机值一次,但该随机值从未更改。

为了解决这个问题,代码必须找到每次命中时调用random的方法。一种解决方案是设计一个新的函数player.hit,它可以获得一个随机伤害值,并将其应用于敌人的健康,从函数调用中返回伤害值。这可能是什么样子:

-- Player
player = {}
player.health = 100
player.hit = function ()
local damage = math.random(0,25)
enemy.health = enemy.health - damage
return damage
end

最好使用一个表构造函数来完成此操作,如下所示:

-- Enemy
enemy = {
health = 100,
hit = function ()
local damage = math.random(5, 30)
player.health = player.health - damage
return damage
end
}

现在,剩下的代码需要修改为使用函数player.hitenemy.hit,以及现在通过调用.hit函数修改的player.healthenemy.health

function player.init_attack()
print('Attack? ')
wanna_attack = io.read()
if wanna_attack == 'y' then
print('Attacked enemy with '.. player.hit() ..' damage! current enemy health is '.. enemy.health)
else
os.exit()
end
player_attacked = 1
end
function enemy.init_attack()
print('Enemy Attacked you with '.. enemy.hit() ..' damage! your current health is '.. player.health)
if player.health <= 0 then
os.exit()
end
end
-- ...
while player.health ~= 0 or enemy.health ~= 0 do
player.init_attack()

这是一个测试运行:

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
Attack? 
y
Attacked enemy with 2 damage! current enemy health is 98
Enemy getting ready to attack!
Enemy Attacked you with 25 damage! your current health is 75
Attack? 
y
Attacked enemy with 18 damage! current enemy health is 80
Enemy getting ready to attack!
Enemy Attacked you with 24 damage! your current health is 51
Attack? 
y
Attacked enemy with 22 damage! current enemy health is 58
Enemy getting ready to attack!
Enemy Attacked you with 28 damage! your current health is 23
Attack? 
y
Attacked enemy with 20 damage! current enemy health is 38
Enemy getting ready to attack!
Enemy Attacked you with 11 damage! your current health is 12
Attack? 
y
Attacked enemy with 6 damage! current enemy health is 32
Enemy getting ready to attack!
Enemy Attacked you with 7 damage! your current health is 5
Attack? 
y
Attacked enemy with 13 damage! current enemy health is 19
Enemy getting ready to attack!
Enemy Attacked you with 17 damage! your current health is -12

因为在初始化期间只执行player.damage = math.random(0,25)enemy.damage = math.random(5, 30)一次,而不是针对每次攻击重复执行。

最新更新