如何使用键事件实际移动Java中的对象

时间:2017-11-04 14:09:22

标签: java swing

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;

public class Gameplay extends JPanel implements KeyListener, ActionListener {
    boolean play = true;
    private int score = 0, delay = 8, ballposX = 120, ballposY = 250, ballXdir = -10,
            ballYdir = -10, playerX = 310, padXdir = -20, padYdir = -20;
    private int padposX = 300, padposY = 50;
    Timer timer;

    public Gameplay() {
        setSize(new Dimension(700, 600));
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        timer = new Timer(delay, this);
        timer.start();
    }

    public void paint(Graphics g) {
        // Gameboard size
        g.setColor(Color.black);
        g.fillRect(1, 1, 700, 600);
        // Border of the game
        g.setColor(Color.yellow);
        g.fillRect(0, 0, 3, 592);
        g.fillRect(0, 0, 692, 3);
        g.fillRect(692, 0, 3, 592);
        // Paddle
        g.setColor(Color.green);
        g.fillRect(playerX, 500, 100, 8);
        // Second Paddle
        g.setColor(Color.red);
        g.fillRect(padposX, padposY, 100, 8);
        // Ball
        g.setColor(Color.yellow);
        g.fillOval(ballposX, ballposY, 20, 20);
        g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
        timer.start();
        if (play) {
            if (new Rectangle(ballposX, ballposY, 20, 20)
                    .intersects(new Rectangle(playerX, 500, 100, 8))) {
                ballYdir = -ballYdir;
            }
            if (new Rectangle(ballposX, ballposY, 20, 20)
                    .intersects(new Rectangle(padposX, padposY, 100, 8))) {
                ballYdir = -ballYdir;
            }
            ballposX += ballXdir;
            ballposY += ballYdir;
            if (ballposX < 0) {
                ballXdir = -ballXdir;
            }
            if (ballposY < 0) {
                ballYdir = -ballYdir;
            }
            if (ballposY > 560) {
                ballYdir = -ballYdir;
            }
            if (ballposX > 665) {
                ballXdir = -ballXdir;
            }
            repaint();
        }
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (playerX >= 600) {
                playerX = 600;
            } else {
                moveRight();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            if (playerX < 10) {
                playerX = 10;
            } else {
                moveLeft();
            }
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void moveRight() {
        play = true;
        playerX += 20;
    }

    public void moveLeft() {
        play = true;
        playerX -= 20;
    }

    public static void main(String[] args) {
        JFrame obj = new JFrame();
        Gameplay gamePlay = new Gameplay();
        obj.setBounds(10, 10, 700, 600);
        obj.setTitle("Air Hockey");
        obj.setResizable(false);
        obj.setVisible(true);
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.getContentPane().add(gamePlay);
    }
}

在这个节目中,我正在制作一个空气曲棍球比赛,其中一个玩家控制其中一个桨。但由于某种原因,我不能用我的代码移动球拍。它有什么问题吗?我在关键事件中使用了if语句,因此paddle无法离开游戏板。当你运行这个代码时,你会看到一个球弹跳和两个固定的桨。

0 个答案:

没有答案