if语句中的随机数没有随机

这是我正在用Lua编写的一个小游戏,攻击伤害采用了math.random。但是当我把让玩家进攻的函数放在if语句中时,伤害不再是随机的。

这是它的具体表现: 用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('攻击?')
    wanna_attack = io.read()
    if wanna_attack == 'y' then
        print('用'..player_damage..'点伤害攻击了敌人!目前的敌人生命值为'..(enemy_health - player_damage))
    else
        os.exit()
   end
   player_attacked = 1
end

function enemy.init_attack()
    print('敌人用'..enemy_damage..'点伤害攻击了你!你现在的生命值为'..(player_health - enemy_damage))
    if player_health <= 0 then
        os.exit()
    end
end

player.init_attack()

if player_attacked == 1 then
    print('敌人准备好进攻!')
    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.init_attack()
        player_attacked = 0
    end
end
点赞
用户7509065
用户7509065

因为在初始化的时候只进行了 player.damage = math.random(0,25)enemy.damage = math.random(5, 30) 这两行代码一次,而没有在每次攻击时都进行操作。

2020-07-26 16:05:30
用户6879826
用户6879826

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,这些都是函数,以及 player.healthenemy.health,这些现在都被调用 .hit 函数修改:

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
2020-07-26 16:24:41