如何使用重置按钮 Java 打砖块游戏

How to use a reset button Java arkanoid game

所以这是我的游戏面板的一部分。我让我的开始按钮工作,并创建了一个重置​​按钮,但我不知道每次按下它时如何重置游戏。有谁知道按"Reset"后如何将游戏重置到原来的位置?现在它会显示 "You have died" 字样,但当我按 Reset 时它什么也没做。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class GamePanel extends JPanel
{
   private Brick[][] bary;
   private static final Color BACKGROUND = Color.black;
   private BufferedImage myImage;
   private Graphics myBuffer;
   private Ball ball = new Ball();
   public Bumper bumper;
   private Timer t;
   private int hits = 0;  
   private boolean isPlayingGame = false;

   public GamePanel()
   {
      myImage =  new BufferedImage(600, 800, BufferedImage.TYPE_INT_RGB);
      myBuffer = myImage.getGraphics();
      myBuffer.setColor(BACKGROUND);
      myBuffer.fillRect(0, 0, 600,800);

      JButton sbutton = new JButton("Start");
      sbutton.setFocusable(false);
      sbutton.addActionListener(new Listener());
      add(sbutton);
      JButton rbutton = new JButton("Reset");
      rbutton.setFocusable(false);
      rbutton.addActionListener(new Listener());
      add(rbutton);

      bary = new Brick[5][14];

      bumper = new Bumper(270, 775, 60, 10, Color.BLUE);
      ball = new Ball(300, 400, 10, Color.RED);

      t = new Timer(5, new Listener());

      setFocusable(true);
      //addKeyListener(new Key());
   }

   // tick method is called every 10ms by Arkanoid.java
   // only does stuff if game is actually being played
   public void tick()
   {
      if(isPlayingGame)
      {
         ball.move(600,800, 600, 800); 
         death(ball);
      }
   }


   // these get called by the BumperLitsener, which is added to the whole frame
   public void moveBumperLeft()
   {
      bumper.setX(bumper.getX()-20);
      //System.out.println("moving bumper left");
   }

   public void moveBumperRight()
   {
      bumper.setX(bumper.getX()+20);
      //System.out.println("moving bumper right");
   }
   /*

   public class Key extends KeyAdapter
   {
      public void keyPressed(KeyEvent e)
      {   
         if(e.getKeyCode() == KeyEvent.VK_A)
            bumper.setX(bumper.getX()+10 );
         if(e.getKeyCode() == KeyEvent.VK_S)
            bumper.setX(bumper.getX()-10 ); 
      }
   }
   */


   public void startGame()
   {
      isPlayingGame = true;
   }


   public void paintComponent(Graphics g)
   {
      // draw myBuffer, then draw that onto g
         myBuffer.setColor(BACKGROUND);     
         myBuffer.fillRect(0,0,600,800); 
         setLayout(new GridLayout(5, 14));
         myBuffer.setColor(Color.WHITE);

         int b=1;
         int d=40;
         for(int r = 0; r < bary.length; r++)
         {  
            for(int c = 0; c < bary[0].length; c++)
            {

               bary[r][c] = new Brick(b, d,40,20, Color.BLUE);
               b=b+43;
               bary[r][c].draw(myBuffer);

            }
            d=d+23;
            b=1;
         }
         ball.draw(myBuffer);
         bumper.draw(myBuffer); 
         repaint();
         if(ball.getColor()==Color.BLACK)
         {
         myBuffer.setFont(new Font("MS Comic Sans", Font.ITALIC, 45));
         myBuffer.drawString("GAME OVER!", 168, 300);
         ball.move(0, 0, 0, 0);
         }
      g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
   }

   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         GamePanel.this.getTopLevelAncestor().requestFocus();
         startGame(); // starts game when button is pressed
         System.out.println("Game is starting!");              
      }
   }  

   private void death(Ball ball)
   {
      double d = ball.getY() + ball.getRadius();

      if(d>800.0)
      {
         ball.setX(300);
         ball.setY(400);
         ball.setdx(ball.getdx()*-1);
         ball.setdy(ball.getdy()*-1);
         ball.setColor(Color.BLACK);
      }
   }

    /* private void collide(Ball ball, Bumper b)
   {
      double d = distance(ball.getX(), ball.getY(), b.getX(), b.getY());  
      if(d <= 37.5)
      {
        ball.move();
         hits++;
      }
   }*/
  // private double distance(double x1, double y1, double x2, double y2)
 //  {
   //   return(Math.sqrt(Math.pow(x2 - x1, 2.0) +  Math.pow(y2 - y1, 2.0)));     // enter the calculation here.
   //}


}

非常感谢!!!

现在,您有一个监听器 class 和两个已针对监听器注册的按钮(sbuttonrbutton)。如果您有不同的 class 实现 ActionListener 会更容易,但我们现在先跳过它。

您需要对 actionPerformed 方法进行修改,以便:

  1. 您可以区分操作的来源 - 即按下了哪个按钮 - 重置还是启动?提示:使用 ActionEvent
  2. 中的 getSource() 方法
  3. 如果按下的按钮是重置按钮,您需要将 GUI 重置为初始状态