只要图像加载,我的分数就会增加1

时间:2013-06-10 16:53:31

标签: java applet

我正在为我的电脑课做一个石头剪刀式游戏。只要生成的图像正在加载(并为您的选择,岩石,纸张或剪刀生成图像),评分就会增加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);
    }
}

1 个答案:

答案 0 :(得分:1)

每次applet本身调用“update”时都会调用Paint。

绘制调用“赢家”,获胜者说“在每个其他情况下用户获胜”:

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

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