随机数生成器生成相同的数字,直到重新编译

时间:2016-05-04 17:17:42

标签: java random

基本上,我制作一个可怕的"迷宫游戏,并希望每次失败时,无需重新编译即可将图片上的游戏更改为不同的图片。目前,该游戏已超过'每次丢失都会保持不变,直到你重新编译为止。

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) 
{

}

}

1 个答案:

答案 0 :(得分:1)

那是因为你总是在最后的Mazegame()中将cuurentLevel设置为Intro。注释这一行并尝试。

 currentLevel = intro;

此外,我表面上看了你的代码..不确定我看到图片切换到底会发生什么。在评估失败之后,您将立即调用您的changePic(即您的mazegame())。因此,每当评估发生失败时,用户将获得更改的图片。