增量数学麻烦

时间:2016-04-20 04:35:19

标签: java math

我一直在为如何为我正在制作的小游戏编写数学而感到困惑。这个游戏是我自己个人学习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。他们各自的行动听众都在底层。

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

问题:我该怎么做数学?或者更好的是:做这种数学的更好(或首选)方法是什么?

感谢您的帮助! MIKEY

1 个答案:

答案 0 :(得分:1)

尝试替换它:

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类,它可以让几乎无限大的数字。