if语句中的随机数没有随机
2020-7-26 15:51:5
收藏:0
阅读:149
评论:2
这是我正在用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
点赞
用户6879826
OP 代码设置 player.damage 和 enemy.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.hit 和 enemy.hit,这些都是函数,以及 player.health 和 enemy.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
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

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