如何使用重置按钮Java arkanoid游戏

时间:2017-06-09 06:07:28

标签: java swing jbutton reset

所以这是我游戏的游戏面板的一部分。我启动按钮开始工作,并创建了一个重置​​按钮,但我不知道如何在每次按下时重置游戏。按下"重置"?有没有人知道如何将游戏重置到原来的位置?现在它会显示单词"你已经死了#34;但是当我按下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.
   //}


}

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

现在,您有一个侦听器类和两个已针对侦听器(sbuttonrbutton)注册的按钮。如果你有不同的类实现了ActionListener,那就容易了,但是现在让我们超越它。

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

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