gpt4 book ai didi

java - mouseMoved 方法中的碰撞检测

转载 作者:行者123 更新时间:2023-12-01 14:08:39 27 4
gpt4 key购买 nike

我正在用java创建一个游戏。在其中,您控制一个跟随鼠标的方 block 。我想对正方形实现碰撞检测,以便它在 JFrame 内稍微停止,而不是在边缘处。使用箭头键执行此操作非常容易,但我无法使用 mouseMoved 方法弄清楚这一点。以下是 mouseMoved 方法所在的代码:

public void mouseMoved(MouseEvent e){

repaint();
if(e.getX() <= 0)
playerX = 0;
if(e.getX() >= 300)
playerX = 500;
if(e.getY() <= 0)
playerY = 0;
if(e.getY() >= 300)
playerY = 500;
else
playerX = e.getX()-25;
playerY = e.getY()-25;
repaint();

}

这是创建正方形的代码:

public void paintComponent(Graphics g) {

Rectangle player = new Rectangle(playerX, playerY, 50, 50);
g.setColor(Color.blue);
g.fillRect(player.x, player.y, player.width, player.height);
repaint();

}

我认为您不需要这个,但为了以防万一,这里是 GamePanel 类的所有代码,它充当 Main 类中 JFrame 的面板。如果您需要主类,请告诉我,但我怀疑您会:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

//Global variables

//Double buffering
private Image dbImage;
private Graphics dbg;


//JPanel variables
static final int GWIDTH = 500, GHEIGHT = 500;
static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);

//Game variable
private Thread game;
private volatile boolean running = false;
public boolean mouseClicked = false;

//Character variables
int playerX = 150, playerY = 150;

public class Mouse extends MouseAdapter{

public void mousePressed(MouseEvent e){

mouseClicked = true;

}

public void mouseReleased(MouseEvent e){

mouseClicked = false;

}

public void mouseMoved(MouseEvent e){

repaint();
if(e.getX() <= 0)
playerX = 0;
if(e.getX() >= 300)
playerX = 500;
if(e.getY() <= 0)
playerY = 0;
if(e.getY() >= 300)
playerY = 500;
else
playerX = e.getX()-25;
playerY = e.getY()-25;
repaint();

}
}



public GamePanel(){

addMouseMotionListener(new Mouse());
setPreferredSize(gameDim);
setBackground(Color.BLUE);
setFocusable(true);
requestFocus(true);

}

public void run(){

while(running){



}

}

public void addNotify(){

super.addNotify();
startGame();

}

private void startGame(){

if(game == null || !running){

game = new Thread(this);
game.start();
running = true;

}

}

public void stopGame(){

if(running){

running = false;

}

//Paint method


}

public void paint(Graphics g){

dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);

}

public void paintComponent(Graphics g) {

Rectangle player = new Rectangle(playerX, playerY, 50, 50);
g.setColor(Color.blue);
g.fillRect(player.x, player.y, player.width, player.height);
repaint();

}

private void log(String s){

System.out.println(s);

}

}

感谢您的帮助。如果您需要什么,请告诉我。

最佳答案

您的移动代码有点不对劲。仅当 Y 不超出范围时才设置 x 位置,并且始终替换 Y 值。我可以建议将来使用完整的 block 吗?它们有助于避免此类问题。

playerX = e.getX()-25;
playerY = e.getY()-25;
if(e.getX() <= 0){
playerX = 0;
}
else if(e.getX() >= 300){
playerX = 500;
}

if(e.getY() <= 0){
playerY = 0;
}
else if(e.getY() >= 300){
playerY = 500;
}

这首先设置位置,然后在玩家出界时纠正它。

关于java - mouseMoved 方法中的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707895/

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