gpt4 book ai didi

java - 增量数学问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:06 26 4
gpt4 key购买 nike

我一直对如何为我正在创建的小游戏编写数学公式感到困惑。该游戏是我个人学习更多 Java 的旅程,主要基于 The Monolith,http://monolith.greenpixel.ca/ .我看过游戏的源代码,但无法确定开发人员是如何计算的。

这是我目前所拥有的代码。

package DamageDealer;
import javax.swing.*;
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Take1 {

int dmg = 0;
int epoints = 1000;
int intUnit1 = 0;

public static void main(String[] args) {
new Take1();
}

public Take1() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace(); }
catch (IllegalAccessException ex) {
ex.printStackTrace(); }
catch (InstantiationException ex) {
ex.printStackTrace(); }
catch (ClassNotFoundException ex) {
ex.printStackTrace(); }

JFrame frame = new JFrame("Button Clicker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new addComponentsToPane());



frame.setSize(300, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
});
}

protected class addComponentsToPane extends JPanel {


/**
*
*/
private static final long serialVersionUID = 1L;

public addComponentsToPane() {
JPanel appPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);

JLabel EvoPoints = new JLabel();
EvoPoints.setText("Points: " + String.valueOf(epoints));
gbc.gridx = 1;
gbc.gridy = 0;
appPanel.add(EvoPoints, gbc);

JButton jbtUnit1 = new JButton("Unit 1");
gbc.gridx = 0;
gbc.gridy = 1;
appPanel.add(jbtUnit1, gbc);

JLabel lblUnit1 = new JLabel();
lblUnit1.setText(String.valueOf(intUnit1));
gbc.gridx = 1;
gbc.gridy = 1;
appPanel.add(lblUnit1, gbc);

JButton jbtDamage = new JButton("Damage");
gbc.gridx = 0;
gbc.gridy = 10;
appPanel.add(jbtDamage, gbc);

JLabel DamageOutput = new JLabel();
DamageOutput.setText(String.valueOf(dmg));
gbc.gridx = 1;
gbc.gridy = 10;
appPanel.add(DamageOutput, gbc);

add(appPanel, gbc);

jbtDamage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dmg++;
DamageOutput.setText(String.valueOf(dmg));
if ((dmg % 10) == 0) {
epoints++;
EvoPoints.setText("Points: " + String.valueOf(epoints));
}

}
});

jbtUnit1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (epoints >= 10) {
intUnit1++;
epoints = epoints - 10;
lblUnit1.setText(String.valueOf(intUnit1));
EvoPoints.setText("Points: " + String.valueOf(epoints));
}
}
});
}
}
}

现在,我只有两个按钮,jbtUnit1jbtDamage。以及底部各自的 Action 监听器。

我目前的数学运算仅在 dmg 值能被 10 整除时才有效。此时(当 dmg 可被 10 整除时),epoints 值增加。但是,我的 jbtUnit1 应该为每个单元增加我的 dmg 输出一个额外的点。因此,(dmg % 10) == 0 将不起作用,因为在达到精确的 %10 之前,您会多次超过值。

问题:我应该如何计算?或者更好的是:进行此类数学运算的更好(或首选)方法是什么?

感谢您的帮助!米奇

最佳答案

尝试替换这个:

dmg++;
DamageOutput.setText(String.valueOf(dmg));
if ((dmg % 10) == 0) {
epoints++;
EvoPoints.setText("Points: " + String.valueOf(epoints));
}

用这个:

dmg+= 1+intUnit1; //Deal more damage with more units
DamageOutput.setText(String.valueOf(dmg));
int newEPoints = dmg/10 - spentEPoints;
if(epoints != newEPoints) {
EvoPoints.setText("Points: " + String.valueOf(newEPoints));
}
epoints = newEPoints;

然后在您的主类中添加一个 spentEPoints 字段以跟踪花费的金额,而不是每次购买单位时从您的 epoints 计数器中减去,您将花费的金额添加到您的 spentEPoints 计数器。

但是请注意,像这样的游戏往往会非常迅速地增加数字,并且您的整数类型变量最终会溢出。看看 BigInteger类让您拥有几乎无限大的数字。

关于java - 增量数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com