-3

我正在为我的电脑课做一个石头剪刀布类型的游戏。只要生成的图像正在加载(并为您的选择、石头、纸或剪刀生成图像),得分就会增加 1。你能检查我的代码并告诉我它有什么问题吗?谢谢!

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class RockPaperScissors extends Applet implements ActionListener {

    private Button rockButton;
    private Button scissorsButton;
    private Button paperButton;
    private String buttonPressed = ""; // the label of the button pressed
    private int computerValue;
    private int myValue;
    Image rockimg;
    Image rock2img;
    Image paperimg;
    Image paper2img;
    Image scissorsimg;
    Image scissors2img;

    int score;
    int compscore;
    int drawscore;

    public void init() {
        rockimg = getImage(getCodeBase(), "rock.gif");
        rock2img = getImage(getCodeBase(), "rock2.gif");
        paperimg = getImage(getCodeBase(), "paper.gif");
        paper2img = getImage(getCodeBase(), "paper2.gif");
        scissorsimg = getImage(getCodeBase(), "scissors.gif");
        scissors2img = getImage(getCodeBase(), "scissors2.gif");
        paperimg = paperimg.getScaledInstance(350, -1, Image.SCALE_SMOOTH);
        paper2img = paper2img.getScaledInstance(350, -1, Image.SCALE_SMOOTH);
        rockimg = rockimg.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
        rock2img = rock2img.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
        scissorsimg = scissorsimg
                .getScaledInstance(200, -1, Image.SCALE_SMOOTH);
        scissors2img = scissors2img.getScaledInstance(200, -1,
                Image.SCALE_SMOOTH);
        setSize(800, 600);

        rockButton = new Button("Rock");
        scissorsButton = new Button("Scissors");
        paperButton = new Button("Paper");
        add(rockButton);
        add(scissorsButton);
        add(paperButton);
        rockButton.addActionListener(this);
        scissorsButton.addActionListener(this);
        paperButton.addActionListener(this);
        score = 0;
        compscore = 0;
        drawscore = 0;
        myValue = -1;
        computerValue = -1;
    }

    Image offScreenBuffer;

    @SuppressWarnings("deprecation")
    public void update(Graphics g) {
        Graphics gr;
        if (offScreenBuffer == null
                || (!(offScreenBuffer.getWidth(this) == this.size().width && offScreenBuffer
                        .getHeight(this) == this.size().height))) {
            offScreenBuffer = this.createImage(size().width, size().height);
        }
        gr = offScreenBuffer.getGraphics();
        paint(gr);
        g.drawImage(offScreenBuffer, 0, 0, this);
    }

    public void actionPerformed(ActionEvent event) {
        buttonPressed = ((Button) event.getSource()).getLabel();
        computerValue = randomNumber();
        translator(buttonPressed);
        repaint();
    }

    // paint is called each time a button is pressed
    public void paint(Graphics g) {

        g.fillRect(0, 0, getWidth(), getHeight());
        computerChoice(g);

        ScoreBoard(g);
        winner(g);
    }

    // Randomly generate one of the numbers 0, 1, 2.
    int randomNumber() {
        return (int) (Math.random() * 3);
    }

    // Prints on the screen one of the strings "Rock", "Scissors", or "Paper" if
    // one of the generated numbers is 0, 1, or 2
    void computerChoice(Graphics g) {

        if (myValue == -1) {
            g.drawString("", 200, 150);
        }
        if (myValue == 0) {
            g.setColor(Color.WHITE);
            g.drawString("Your choice: Rock", getWidth() / 2 - 50, 170);
            g.drawImage(rockimg, 0, 100, this);
        }
        if (myValue == 1) {
            g.setColor(Color.WHITE);
            g.drawString("Your choice: Scissors", getWidth() / 2 - 50, 170);
            g.drawImage(scissorsimg, 0, 100, this);
        }
        if (myValue == 2) {
            g.setColor(Color.WHITE);
            g.drawString("Your choice: Paper", getWidth() / 2 - 50, 170);
            g.drawImage(paperimg, 0, 100, this);
        }
        if (computerValue == 0) {
            g.setColor(Color.WHITE);
            g.drawString("Computer's choice: Rock", getWidth() / 2 - 50, 190);
            g.drawImage(rock2img, getWidth() - 240, 100, this);
        }
        if (computerValue == 1) {
            g.setColor(Color.WHITE);
            g.drawString("Computer's choice: Scissors", getWidth() / 2 - 50,
                    190);
            g.drawImage(scissors2img, getWidth() - 240, 100, this);
        }
        if (computerValue == 2) {
            g.setColor(Color.WHITE);
            g.drawString("Computer's choice: Paper", getWidth() / 2 - 50, 190);
            g.drawImage(paper2img, getWidth() - 340, 100, this);
        }
    }

    // Translates "Rock" to 0, "Scissors" to 1, and "Paper" to 2.
    void translator(String s) {
        if (s.equals("Rock")) {
            myValue = 0;
        } else if (s.equals("Scissors")) {
            myValue = 1;
        } else if (s.equals("Paper")) {
            myValue = 2;
        }
    }

    // Decides the winner.
    void winner(Graphics g) {
        // Before playing, nothing happens, so print nothing.
        if (computerValue == -1) {
            g.setColor(Color.WHITE);
            g.drawString("", 200, 100);
        }
        // If the machine and the player have the same thing, it is a draw.
        else if (computerValue == myValue) {
            g.setColor(Color.WHITE);
            g.drawString("Draw", getWidth() / 2 - 50, 210);
            drawscore+=1;
        }
        // Computer beats if
        // it has Rock and you have scissors, or it has scissors and you have
        // paper or
        // you have paper and computer has rock.
        else if (computerValue == 0 && myValue == 1 || computerValue == 2
                && myValue == 0 || computerValue == 1 && myValue == 2) {
            g.setColor(Color.WHITE);
            g.drawString("Computer wins", getWidth() / 2 - 50, 210);
            compscore+=1;

        }
        // You win in any other case
        else {
            g.setColor(Color.WHITE);
            g.drawString("You win", getWidth() / 2 - 50, 210);
            score+=1;
        }
    }

    void ScoreBoard(Graphics g){
        g.drawString("Computer Score:" + compscore, 300, 560);
        g.drawString("Your Score:" + score, 300, 530);
        g.drawString("Draws:" + drawscore, 300, 500);
    }
}
4

1 回答 1

1

每次小程序本身调用“更新”时,都会调用 Paint。

Paint 调用“获胜者”,获胜者说“在其他情况下,用户获胜”:

  // You win in any other case
    else {
        g.setColor(Color.WHITE);
        g.drawString("You win", getWidth() / 2 - 50, 210);
        score+=1;
    }

我会将您的分数计算代码从 paint 方法移到 actionPerformed 方法中。这将视图与游戏逻辑分开。

于 2013-06-19T08:43:52.583 回答