0

我刚刚开始编码。我现在正在尝试使用 a 制作 FruitNinja 类型的游戏JPanel,并且我已经完成了所有工作(除了使游戏继续的循环和其余的水果/炸弹动画)

到目前为止,我只制作了动画Apple.png(完成后将开始制作其他水果的动画)

我正在尝试制作一种checkHit方法来检查我的鼠标是否越过图像(切开水果),因此MouseMotionListener需要使用该方法。如果鼠标确实越过水果,我需要它消失/移出框架。

那么我如何从中检索坐标MouseMotionListener,检查它是否在我的水果的 40 像素内,如果是,移动框架的那个水果?如果可能的话,不仅要这样做,还要在完成时播放声音。

我有另一个用户来帮助我解决这个问题,但我仍然无法弄清楚。

import javax.swing.*;//imports JPanel class
import java.awt.*;//imports the Graphics class
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
//imports packages needed for the hovering


public class FruitNinja extends JPanel{


   private Image dojo;
   private Image apple;
   private Image orange;
   private Image pear;
   private Image strawberry;
   private Image banana;
   private Image bomb;

   //declares the import image variables 

  private javax.swing.Timer timer;

   private int dx, dy;

   private int vx =40;
   //velocity of the object moving in the x direction
   private int x=-10;

   private int vy=40;
   //velocity of the object moving in the y direction
   private int y=770;


   private int divide=(int)(2*Math.random())+1;//sets random number
   //determines when the fruit will bounce off

  public FruitNinja() { // a constructor to set up graphics windo
      super();
      setBackground(Color.WHITE);
      loadImage();
     dx = 25;
     dy = 25;

     timer = new javax.swing.Timer( 30, new TimerListener() );
         //interval of 10 milliseconds
     timer.start();
         //timer is a facility for threads to schedule tasks for future execution in a background thread.
   }

   private void loadImage() {
      ImageIcon ii = new ImageIcon("Dojo.jpg");
      dojo = ii.getImage();

      ImageIcon oo = new ImageIcon("Orange.ico");
      orange = oo.getImage();

      ImageIcon ss = new ImageIcon("Strawberry.png");
      strawberry = ss.getImage();

      ImageIcon bb = new ImageIcon("Banana.png");
      banana = bb.getImage();

      ImageIcon pp = new ImageIcon("Pear.png");
      pear = pp.getImage();

      ImageIcon aa = new ImageIcon("Apple.png");
      apple = aa.getImage();

      ImageIcon bo=new ImageIcon("Bomb.png");
      bomb=bo.getImage();


      //loads neccaseary images for this game


   }@Override 

   public void paintComponent(Graphics g){ // draw graphics in the panel

     super.paintComponent(g);// to make panel display correctly
     g.drawImage(dojo, 0,0, this);
  //draws out dojo
     g.drawImage(apple, x,y, this);
     g.drawImage(orange, -300,-300, this);
     g.drawImage(pear, -300,-300, this);
     g.drawImage(banana, -300,-300, this);
     g.drawImage(strawberry, -300,-300, this);
     //draws out the fruits somewhere
     g.drawImage(bomb,-300,-300,this);
     //draws out the bomb that will make the player lose

   }

  private class TimerListener implements ActionListener {

         public void actionPerformed(ActionEvent e) {

         //implements the ActionListenter


        x+=vx;   //adds velocity to the x, which makes it travel in x direction        

        if (x<0){

        vx=(-1*vx);
         //tells the velocity to change direction
        }

        y+=vy; //adds velocity to the y, which makes it travel in y direction  
        if (y>=getHeight()||y<=getHeight()/4){

        vy=(-1*vy);
         //tells the velocity to change direction
         }

        repaint(); // causes paintComponent to be called by the JVM

       }


}

   public static void main(String[] args) { 

      FruitNinja panel = new FruitNinja(); // window for drawing  
      JFrame f = new JFrame(); // the program itself
      f.setTitle("Fruit Ninja");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//when the X button is clicked, the program quits
      f.setSize(1280,800);//size of the frame
      Container pane = f.getContentPane();//pane refers to the interior of the JFrame

      FruitNinja p1 = new FruitNinja();

      pane.add(p1);//add the FacePanel object to the interior of the frame
      f.setVisible(true);

   }
}

如果我在这里所拥有的东西或/以及如何MouseMotionListner在其中使用它,请告诉我。我对此还是很陌生,所以我不知道如何检索坐标或设置“MouseMotionListener”。如果我的问题不清楚,请随时进入评论并让我知道是否是这种情况;我愿意更彻底地解释这个问题。

谢谢

编辑:

这是方法吗?

      private class HandleMouse extends MouseAdapter {
   //extends MouseAdapter rather than implement Mouse Adapter
   public void mousePressed( MouseEvent e ){
        Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);
        Rectangle appleHitBox=new Rectangle(ax-128,ay-128,256,256);

       Area area = new Area(hitBox);
        area.intersect(new Area(appleHitBox));
        if(!area.isEmpty()){
            ax=-500;
            ay=-500;
            avx=0;
            avy=0;   
        }
        else{
            numOfMisses++;

        }
}
}

4

1 回答 1

1

那么如何从 MouseMotionListener 中检索坐标,

MouseMotionListener将a传递MouseEvent给方法,其中包含事件的各种属性,这包括事件在组件坐标上下文中的坐标

检查它是否在我的水果的 40 像素范围内,是否在

所以,这是基本的碰撞检测,有很多可能的方法你可以解决这个问题,但是因为我很懒,我可能会考虑使用Rectangle来表示用户点击框

Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);

这会生成一个围绕中心点 20 像素的框MouseEvent

“但为什么?” 你问?因为Rectangle有许多有用的方法可用于碰撞检测,包括

  • intersection
  • intersects
  • union

仅举几例

因此,给定水果的x/y位置和大小,您可以简单地创建第二个Rectangle并确定第一个是否命中第二个

如果是的话,把水果从框架上移开?

好吧,这将是一个设置标志的过程,“主循环”(即你的TimerListener)告诉它水果已被击中,现在由“主循环”将其从“油漆”中删除列出并设置下一个对象

如果可能的话,不仅要这样做,还要在完成后播放声音

作为一般答案,是。一旦您能够检测到发生了碰撞,播放声音就相对简单了,当它检测到发生碰撞时,我再次尝试使用“主循环”来执行此操作

于 2017-05-29T09:04:37.007 回答