基本上,我正在制作一个“可怕的”迷宫游戏,希望每次失败时无需重新编译,画面上方的游戏都可以更改为另一幅画面。目前,每次输家之前,“游戏结束”画面都保持不变,直到重新编译为止。

public class MazeGame extends JComponent implements MouseMotionListener, MouseListener
{
private Random generator = new Random();

BufferedImage intro;
BufferedImage level1;
BufferedImage level2;
BufferedImage level3;
BufferedImage pic1;
BufferedImage pic2;
BufferedImage pic3;
BufferedImage pic4;
BufferedImage pic5;
BufferedImage gameOver;
BufferedImage currentLevel;
AudioClip spooky = JApplet.newAudioClip(getClass().getResource("gr/spooky.aiff"));

public MazeGame() throws IOException
{
    intro = ImageIO.read(getClass().getResource("gr/start.png"));
    level1 = ImageIO.read(getClass().getResource("gr/level1.png"));
    level2 = ImageIO.read(getClass().getResource("gr/level2.png"));
    level3 = ImageIO.read(getClass().getResource("gr/level3.png"));
    pic1 = ImageIO.read(getClass().getResource("gr/pic1.jpg"));
    pic2 = ImageIO.read(getClass().getResource("gr/pic2.jpg"));
    pic3 = ImageIO.read(getClass().getResource("gr/pic3.jpg"));
    pic4 = ImageIO.read(getClass().getResource("gr/pic4.jpg"));
    pic5 = ImageIO.read(getClass().getResource("gr/pic5.jpg"));

    int number = 1 + generator.nextInt( 5 );
    gameOver = ImageIO.read(getClass().getResource( "gr/pic" + number + ".jpg" ));

    currentLevel = intro;
}

public static void main(String args[]) throws IOException
{
    JFrame window = new JFrame("Totally Not Scary Maze Game");
    MazeGame game = new MazeGame();
    window.add(game);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    window.setVisible(true);
    game.addMouseMotionListener(game);
    game.addMouseListener(game);
}

public Dimension getPreferredSize() {
    return new Dimension(800, 800);
}

protected void paintComponent(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(0, 0, 800, 800);

    g.drawImage(currentLevel, 0, 0, null);
}

public void mouseMoved(MouseEvent e) {

    int x = e.getX();
    int y = e.getY();
    int color = currentLevel.getRGB(x, y);

    System.out.println(color);

    int level1WallColor = -13939918;
    int level2WallColor = -10340732;
    int level3WallColor = -4640206;
    int goalColor = -14532251;

    if(color == goalColor)
    {
        if(currentLevel == intro)
        {
            currentLevel = level1;
        }

        else if(currentLevel == level1)
        {
            currentLevel = level2;
        }

        else if(currentLevel == level2)
        {
            currentLevel = level3;
        }

        else if(currentLevel == level3)
        {
            showGameOver();
        }
    }

    if(color == level1WallColor)
    {
        showGameOver();
    }

    if(color == level2WallColor)
    {
        currentLevel = intro;
    }

    if(color == level3WallColor)
    {
        showGameOver();
    }

    repaint();
}

private void showGameOver()
{
    currentLevel = gameOver;
    spooky.play();
}

public void mouseClicked(MouseEvent e)
{
    if(currentLevel == gameOver)
    {
        currentLevel = intro;
    }

    repaint();
}

public void mousePressed(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public void mouseDragged(MouseEvent e)
{

}

}

最佳答案

这是因为您总是在末尾Mazegame()处将cuurentLevel设置为Intro。评论此行,然后尝试。

 currentLevel = intro;


另外,我还浅浅地阅读了您的代码。.不确定我看到图片的确切位置会发生在哪里。您将在评估失败之后立即调用changePic(即mazegame())。因此,每次评估失败时,失败的用户都会获得更改的图片。

09-10 07:15