2016-05-04 9 views
1

基本的に、私は "怖い"迷路ゲームを作っており、再コンパイルせずに失敗するたびに、画像上のゲームを別の画像に変更します。現在、「ゲームオーバー」画像は、再コンパイルするまで失うたびに同じままです。乱数ジェネレータは、再コンパイルするまで同じ番号を生成します

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

2回目の新しいmazegame()をどこに呼び出すことができませんか?コンストラクタでは、これが背景画像が選択されている場所ですか? –

+0

@ minh-kieuが最初にそれを得たので、私は答えません。あなたは、 'gameOver = ImageIO.read(getClass()。getResource( "gr/pic" + number + ".jpg"))という行を呼び出す必要があります。失うたびに。 –

+0

@ Ray-Falkだから私はそれをどこでやるのだろうか? – Griffins13

答えて

1

これは、Mazegame()の最後に常にcuurentLevelをIntroとして設定しているためです。この行をコメントして試してみてください。

currentLevel = intro; 

また、私はあなたのコードを表面的に読んでいます。画像の切り替えがどこで起こるかわかりません。フェイルが評価された直後にchangePic(つまり、mazegame())を呼び出します。毎回評価が失敗すると、ユーザーは変更された画像を取得します。

+0

これを行うだけで、ゲームグレーが塗られます。そのコード行は、ゲームがどのレベルで始まるかを設定します。 – Griffins13

+0

このコードにはいくつかのロジックが組み込まれているので、明らかに何かが間違っています。 ICは非常に賢明ですが、あなたは次のことだけを行う新しいプログラムに小さな単一メソッドを書くことができます。 1.イメージセットurレベルフラグ1をロードします。2. urレベルフラグを2に設定し、そこでイメージを変更します。こうすることで、ペイント/塗り替えの作業は他にはないことを確認できます。そして、あなたはこの論理で何が起こっているのかを理解することができます。他の賢明なことは、何が起こっているのか分かりません。また、ロジックをステップバイステップで書くことができればデバッグすると、間違った行が表示されます。 – SnackDragon

関連する問題