gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:24:11 25 4
gpt4 key购买 nike

基本上,我正在制作一个“可怕的”迷宫游戏,并希望每次失败时游戏结束图片都会更改为不同的图片,而无需重新编译。目前,每次失败时“游戏结束”画面都会保持不变,直到重新编译为止。

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())。因此,每次评估失败的用户都会收到更改的图片。

关于java - 随机数生成器生成相同的数字,直到重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com