我正在尝试制作一个小型射手,问题是:如何使英雄的旋转取决于鼠标的旋转,当我单击去时,他已经在人行道上了。
public GameMenu(JFrame frame) {
try {
bufferedImage = ImageIO.read(new File("qwe.png"));
} catch (IOException e) {
e.printStackTrace();
}
Image dimg = bufferedImage.getScaledInstance(BLOCK_WEIGHT, BLOCK_HIEGHT, Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(dimg);
frame.remove(MainMenuDT.card1);
frame.setSize(WEIGHT_MAP, HIEGHT_MAP);
frame.setLocationRelativeTo(null);
canvas.setBackground(Color.BLACK);
frame.add(canvas);
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (!gameOver) {
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) {
myPerson.move(e.getKeyCode());
}
}
}
});
frame.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
mousePosY = e.getY();
mousePosX = e.getX();
canvas.repaint();
}
});
}
}
这是机芯的执行
class MyPerson {
boolean livePerson(){
return true;
}
void move(int loc) {
if (loc == KeyEvent.VK_RIGHT){
heroPosX += 3;
}else if( loc == KeyEvent.VK_LEFT){
heroPosX -= 3;
}
canvas.repaint();
}
}
class Canvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fill3DRect(mousePosX, mousePosY, 10, 10, true);
g.drawImage(imageIcon.getImage(), heroPosX, heroPosY, null);
}
}
会有一个景象,但有必要使他与英雄保持一定距离。
g.fill3DRect(mousePosX, mousePosY, 10, 10, true);