我刚刚开始编码。我现在正在尝试使用 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++;
}
}
}