为Java游戏产生攻击 [英] Generating attack for java game

查看:285
本文介绍了为Java游戏产生攻击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java创建一个高级"文本冒险游戏,以深入了解有关类用法和其他方面的知识. 我正在尝试弄清楚攻击的逻辑,这就是我到目前为止所掌握的:

I am creating an 'advanced' text adventure game on java to get into learning more about class useage and whatnot. I am trying to work out the logic of attacking and this is what I have so far:

我想做的是为不同的事物设置另一个随机的较小的乘数,例如intel是角色要拥有的属性,什么是添加乘数的最佳方法?

What I want to do is have another random smaller multiplier for different things, like intel is a stat the character will have, what would be the best way to add a multiplyer?

public int magicAttack()
    {
        int randomMultiplyer = randomGenerator.nextInt(10);
        double extraMultiplyer = randomGenerator.nextDouble();
        System.out.println((character.basedmg/*10*/ + character.strength) * randomMultiplyer);
        return (character.basedmg/*10*/ + character.magic) * randomMultiplyer;
    }

以下是Im正在使用的课程 Character.java AttackManager.java

Here are the classes Im using Character.java and AttackManager.java

因此,使用我在此处粘贴的上述代码,我不想对它的工作原理过分地了解,只是因为它工作得相当好.我想添加另一个乘法器(正如您从double'extraMultiplyer中看到的那样,我已经开始了,我希望它可能只是添加0.6,因此将其乘以1.6,但我不确定如何随机获得它.

So with the above code that I pasted here, I dont want to be overly technical with how it works, simply that it works reasonably well. I want to add another multiplyer (which I have started as you can see from the double 'extraMultiplyer' and I wanted it to maybe just add 0.6, so multiplying it by 1.6 but I am not sure how to get that with random.

推荐答案

如果使用的是Java utils.Random,nextDouble()将返回0.0到1.0之间的值.

If you are using Java utils.Random, nextDouble() returns a value between 0.0 and 1.0.

乘以您期望的最大值和最小值(范围)之差并加上最小值,您将得到一个介于最小值和最大值之间的值.

Multiplying by the difference of your desire max and min value (range) and adding your min value, you will get you a value between min and max.

Random r = new Random();
double multiplier = (max - min) * r.nextDouble() + min;

如果您想要0到0.6,

If you want 0 to 0.6,

double multiplier = 0.6 * r.nextDouble();

这篇关于为Java游戏产生攻击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆