0

我正在开发一个使用 java GUI 的 Rock Paper Scissors 游戏,我的一切工作都很好,但我想为用户和计算机添加一个分数计数器,它们都位于屏幕的角落,每次都添加 1任何一方赢得一场比赛。我没有做到最好的三分之二或类似的东西,这只是一场无休止的比赛,我希望它随着你的前进而加起来。这是我的代码,.jpg 和 .png 文件只是我用于背景和其他内容的图像。我已经尝试创建一种方法来保持分数(方法分数和其他与 yourScore 和 compScore 相关的东西),但我无法显示它并且欢迎任何新想法,尽管我几乎所有东西都使用了 JLabels。

public class main extends JFrame implements ActionListener {
    JButton rock;
    JButton scissors;
    JButton paper;
    JLabel left;
    ImageIcon rock1;
    ImageIcon scissors1;
    ImageIcon scissors2;
    ImageIcon paper2;
    ImageIcon paper1;
    ImageIcon rock2;
    ImageIcon back;
    ImageIcon scissorsb;
    JLabel right;
    String[] RPS = { "Rock", "Paper", "Scissors" };
    Random rand = new Random();
    int num = rand.nextInt(RPS.length);
    int yourChoice = -1;
    JTextField texter;
    JLabel winner;
    Container contentPane = this.getContentPane();
    SpringLayout layout = new SpringLayout();
    String Tie = "Tie";
    String compwins = "Computer wins!";
    String youwin = "You win!";
    int yourNum = 9;
    JLabel yourScore;
    int compNum = 0;
    JLabel compScore;
    Font winnerFont = new Font("Font", Font.PLAIN, 35);

    public main() {
        super("Rock Paper Scissors");
        back = new ImageIcon("image1233450665506.png");
        paper1 = new ImageIcon("paper1.png");
        paper2 = new ImageIcon("paper2.png");
        scissors1 = new ImageIcon("scissors1.png");
        scissors2 = new ImageIcon("scissors2.png");
        rock1 = new ImageIcon("rock1.png");
        rock2 = new ImageIcon("rock2.png");

        JLabel background = new JLabel(back);
        left = new JLabel();
        right = new JLabel();
        winner = new JLabel();
        yourScore = new JLabel();
        compScore = new JLabel();
        right.setVisible(true);
        this.setVisible(true);
        this.setSize(1280, 985);
        winner.setFont(winnerFont);
        winner.setHorizontalAlignment(SwingConstants.CENTER);
        yourScore.setFont(winnerFont);
        compScore.setFont(winnerFont);

        contentPane.setLayout(layout);

        contentPane.add(winner);
        contentPane.add(left);
        contentPane.add(right);
        contentPane.add(yourScore);
        contentPane.add(compScore);
        layout.putConstraint(SpringLayout.NORTH, left, 300, SpringLayout.NORTH,
                contentPane);
        layout.putConstraint(SpringLayout.NORTH, right, 300,
                SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, right, 0, SpringLayout.EAST,
                contentPane);

        rock = new JButton("Rock");
        paper = new JButton("Paper");
        scissors = new JButton("Scissors");

        layout.putConstraint(SpringLayout.WEST, rock, 500, SpringLayout.WEST,
                contentPane);
        this.add(rock, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, rock, -30, SpringLayout.SOUTH,
                contentPane);
        layout.putConstraint(SpringLayout.WEST, rock, 40, SpringLayout.NORTH,
                contentPane);
        this.add(paper, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, paper, -30,
                SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, paper, 200, SpringLayout.NORTH,
                contentPane);
        this.add(scissors, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, scissors, -30,
                SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, scissors, 360,
                SpringLayout.NORTH, contentPane);
        rock.setBackground(Color.GRAY);
        rock.setPreferredSize(new Dimension(140, 50));
        paper.setBackground(Color.WHITE);
        paper.setPreferredSize(new Dimension(140, 50));
        scissors.setBackground(Color.LIGHT_GRAY);
        scissors.setPreferredSize(new Dimension(140, 50));
        rock.repaint();
        scissors.repaint();
        paper.repaint();
        contentPane.add(background);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        rock.doClick();
        paper.doClick();
        scissors.doClick();
        rock.addActionListener(this);
        paper.addActionListener(this);
        scissors.addActionListener(this);

    }

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

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

        if (arg0.getSource() == rock) {
            left.setIcon(rock1);
            yourChoice = 0;
        } else if (arg0.getSource() == paper) {
            left.setIcon(paper1);
            yourChoice = 1;
        } else if (arg0.getSource() == scissors) {
            left.setIcon(scissors1);
            yourChoice = 2;
        }

        computerMove();

        // result.repaint();
        Score();
    }

    public void Score() {
        layout.putConstraint(SpringLayout.NORTH, winner, 50,
                SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, winner, 560, SpringLayout.WEST,
                contentPane);
        layout.putConstraint(SpringLayout.SOUTH, compScore, 100,
                SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, compScore, 600,
                SpringLayout.EAST, contentPane);
        yourScore = new JLabel(Integer.toString(yourNum));
        compScore = new JLabel(Integer.toString(compNum));

        if (num == 0 && yourChoice == 0) {
            winner.setText(Tie);
            winner.repaint();
        } else if (num == 1 && yourChoice == 0) {
            yourNum++;
            yourScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 2 && yourChoice == 0) {
            compNum++;
            compScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 0 && yourChoice == 1) {
            yourNum++;
            yourScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 1 && yourChoice == 1) {
            winner.setText(Tie);
            winner.repaint();
        } else if (num == 2 && yourChoice == 1) {
            compNum++;
            compScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 0 && yourChoice == 2) {
            compNum++;
            compScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 1 && yourChoice == 2) {
            yourNum++;
            yourScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 2 && yourChoice == 2) {
            winner.setText(Tie);
            winner.repaint();
        }
        winner.repaint();
        yourScore.repaint();
        compScore.repaint();
        yourScore.setForeground(Color.RED);
        compScore.setForeground(Color.RED);
        winner.setForeground(Color.RED);

    }

    public void computerMove() {
        num = rand.nextInt(RPS.length);
        if (num == 0) {
            right.setIcon(rock2);
        } else if (num == 1) {
            right.setIcon(paper2);
        } else if (num == 2) {
            right.setIcon(scissors2);
        }

    }

}
4

1 回答 1

0

好吧,如果您使用绝对定位,这将非常容易,它允许您通过坐标设置 JLabels、JButton 等,而不必定位它们并使代码超级混乱。

然后对于分数部分,我将创建一个新的 JLabel,一个用于计算机,一个用于播放器,显示在左/右上方并显示分数。

希望能帮助到你!

于 2012-10-02T00:04:15.717 回答