我正在尝试用蝙蝠(桨)和球做一个简单的Java游戏。到目前为止,我已将2个物体涂在面板上,但是我不能让它们移动。我已经添加了球棒的关键事件和球的move()方法。以下是我的所有课程。
游戏课程:
public class Game extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}});
}
MyDrawingPanel myDrawingPanel = new MyDrawingPanel(this);
MyUIPanel myUIPanel = new MyUIPanel(this);
public Game()
{
setSize(1160,660); // you may change frame and panel sizes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myDrawingPanel);
cp.add(myUIPanel);
setVisible(true);
}
}
MyDrawingPanel类:
class MyDrawingPanel extends JPanel {
Game game;
Ball ball = new Ball(this);
Bat bat = new Bat(this);
public MyDrawingPanel(Game game)
{
this.game=game;
setPreferredSize(new Dimension(800,600));
setBackground(Color.RED);
requestFocus();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}
球类:
public class Ball {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private MyDrawingPanel myDrawingPanel;
public Ball(MyDrawingPanel myDrawingPanel) {
this.myDrawingPanel = myDrawingPanel;
}
public void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > myDrawingPanel.getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > myDrawingPanel.getHeight() - 30)
ya = -1;
x = x + xa;
y = y + ya;
}
public void paint(Graphics2D g) {
g.fillOval(x, y, 30, 30);
}
}
Bat class:
public class Bat{
int x = 0;
int xa = 0;
private MyDrawingPanel myDrawingPanel;
public Bat(MyDrawingPanel myDrawingPanel)
{
this.myDrawingPanel = myDrawingPanel;
}
public void move(){
if(x + xa > 0 && x + xa <myDrawingPanel.getWidth()-60 )
x = x + xa;
}
public void paint(Graphics2D g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(x, 500, 100, 20, 10, 10);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}
public void keyReleased(KeyEvent e)
{
xa = 0;
}
}
答案 0 :(得分:0)
keyPressed
和keyReleased
等方法在没有KeyListener
实施的情况下不会执行任何操作。所以目前,你的方法毫无用处。您应该做什么让DrawingPanel
类实现KeyListener
,就像这样
public class DrawingPanel extends JPanel implements KeyListener {
...
}
方法keyPressed
和keyReleased
应该在该类中。您还需要setter
方法,以便在Bat
类中更新变量,例如x
和xa
,这些是决定变量的变量Bat
。
所以keyPressed
应该在DrawingPanel
类中,它可能看起来像这样
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
bat.setXa(bat.getXa() - 1);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
bat.setXa(1);
repaint();
}
}
请注意我如何调用repaint()
。这就是你移动东西后需要做的事情。
这应该让你开始正确的方向
虽然我会建议使用键绑定,而不是使用KeyListener
。您可能会使用KeyListener
面临焦点问题。我实现了DrawingPanel
的键绑定,它运行正常。它会给你一些合意的想法。
class MyDrawingPanel extends JPanel {
Game game;
Ball ball = new Ball(this);
Bat bat = new Bat(this);
public MyDrawingPanel(Game game) {
this.game = game;
setPreferredSize(new Dimension(800, 600));
setBackground(Color.RED);
requestFocus();
Action rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x += 10;
repaint();
}
};
Action leftAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x -= 10;
repaint();
}
};
InputMap inputMap = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
actionMap.put("rightAction", rightAction);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
actionMap.put("leftAction", leftAction);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}