Basically, I'm making a "scary" maze game and want the game over picture to change to a different picture every time you fail without having to recompile. Currently, the 'game over' picture stays the same every time you lose until you recompile.
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)
{
}
}
Aucun commentaire:
Enregistrer un commentaire