gpt4 book ai didi

java - 将 MouseMotionListener 添加到 paddle(Java Pong 游戏)

转载 作者:行者123 更新时间:2023-12-01 11:36:04 25 4
gpt4 key购买 nike

我搜索了整个网络试图找到解决方案,但最终我只是变得更加困惑和沮丧。我正在尝试让我的桨移动(paddle_y)。 Paddle 不需要 x 坐标,只需在垂直线上上下移动即可。我不知道 mouseListener 需要什么语法才能让我的桨移动。如果可能,请提供示例或解决方案。非常感谢,非常感谢任何帮助。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;


public class PongGame extends JFrame implements Runnable, MouseMotionListener {


int ball_x, ball_y, ball_dx, ball_dy;
int ball_r;

int x_left, x_right, y_top, y_bottom;

int paddle_y = 30;


/**
* Constructor
*/
public PongGame(){
init();
}

/**
* UI
*/
protected void init(){
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


int curX = -1, curY = -1;

ball_x = this.getWidth()/2;
ball_y = this.getHeight()/2;

ball_dx = ball_dy = 2;

ball_r = 20;

this.setVisible(true);
getFocus(this);

x_left = this.getInsets().left;
x_right = this.getWidth() - this.getInsets().right - ball_r;
y_top = this.getHeight() - this.getInsets().top + ball_r/3;
y_bottom = this.getInsets().bottom + ball_r;

addMouseMotionListener(this);

}

public void mouseMoved(MouseEvent e) {

//help
}


public void mouseDragged(MouseEvent e) {
//help
}


/**
* helper method which we use to get the focus
*/
public void getFocus(final JFrame frame)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
frame.requestFocus();
}
});
}

/**
* implementation of the Runnable interface to be able to move the ball, etc.
*/
public void run(){

while(true){

ball_x += ball_dx;
if(ball_x <= x_left || ball_x >= x_right){
ball_dx *=-1;
ball_x += (2*ball_dx);
}

ball_y += ball_dy;
if(ball_y <= y_bottom || ball_y >= y_top){
ball_dy *=-1;
ball_y += (2*ball_dy);
}

repaint();

try{
Thread.sleep(50);
}catch(InterruptedException ex){
System.out.println(ex);
}
}
}

/**
* all rendering occurs here
*/
public void paint(Graphics g){

//Color paddleOne;
//paddleOne = new Color(0);

g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());

g.setColor(Color.black);
g.fillOval(ball_x,ball_y, ball_r, ball_r);

g.setColor(Color.black);
g.fillRect(0,paddle_y,20,70);
}

/**
* entry point into the program
*/
public static void main(String[] args){
// create the class
PongGame application = new PongGame();
new Thread(application).start();

}

}

桨:

int paddle_y = 30;



g.setColor(Color.black);
g.fillRect(0,paddle_y,20,70);




public void mouseMoved(MouseEvent e) {

//help
}


public void mouseDragged(MouseEvent e) {
//help
}

最佳答案

Paddle 类需要 JFrame 类的实例才能添加鼠标监听器。如果将 JFrame 对象传递到构造函数中,则可以使用以下代码:

public class Paddle {
private JFrame frame;
private int paddle_y;
public Paddle(JFrame frame) {
this.frame = frame;
this.paddle_y = 30; //Or whatever number you want
frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
Paddle.this.paddle_y = e.getY(); //When the mouse is moved, it will call on this function to change the Paddle.paddle_y variable.
}

public void mouseDragged(MouseEvent e){} //This reacts when the mouse is clicked, moved, then released.
}
}

这应该替换 Paddle 类,并且当鼠标移动时它应该更改 paddle_y 变量。

编辑:如果您不想创建新类,请将其放入 JFrame 类中:

int paddle_y;
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
paddle_y = e.getY(); //When the mouse is moved, it will call on this function to change the paddle_y variable (Within the JFrame class).
}

然后就可以在渲染时调用 paddle_y 变量了。

关于java - 将 MouseMotionListener 添加到 paddle(Java Pong 游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com