Java Swing 打砖块游戏(1)

最近在B站关注了萧井陌大佬,发现投稿空间有JS的游戏制作录播
网址如下:
https://www.bilibili.com/video/av12138532

只完成了第一部分,部分代码模仿Java Core 1(第九版 第十章)。
照着模仿的效果图


Java Swing 打砖块游戏(1)_第1张图片

项目结构如下


Java Swing 打砖块游戏(1)_第2张图片

1.Main

public class Main {
    public static void main(String[] args) {
        // 事件分配线程写法来自Java Core 1(408页) 
        // 没有记错的话这个是Java 8里的lambda表达式
        EventQueue.invokeLater(() -> {
            //创建对象
            ListenerFrame frame = new ListenerFrame();
            //添加关闭按钮
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //添加Title
            frame.setTitle("打砖块");
            //添加键盘监听事件
            frame.addKeyListener(frame);
            //设置可见
            frame.setVisible(true);
        });
    }
}

2.ListenerFrame

继承JFrame
实现KeyListener(键盘事件)

class ListenerFrame extends JFrame implements KeyListener {
    /**
     * @param DEFAULT_WIDTH 组件宽
     * @param DEFAULT_HEIGHT 组件高度
     * */
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 400;

    //创建 挡板和球对象
    private Paddle paddle = new Paddle();
    private Ball ball = new Ball();

    // 创建画图对象,传入构造方法
    private MyComponent component = new MyComponent(ball, paddle);

    public ListenerFrame() {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        add(component);

        //定时器 定时重绘组件
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                component.repaint();
                ball.move();
                ball.collide(paddle.getX(),paddle.getY(),paddle.PADDLE_WIDTH,paddle.PADDLE_HEIGHT);
            }
        }, 0, 1000 / 45);

    }

    //键盘监听
    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                paddle.moveLeft();
                break;
            case KeyEvent.VK_RIGHT:
                paddle.moveRight();
                break;

            case KeyEvent.VK_A:
                paddle.moveLeft();
                break;
            case KeyEvent.VK_D:
                paddle.moveRight();
                break;
            case KeyEvent.VK_F:
                ball.fire();
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
}

3.MyComponent
继承JComponent

class MyComponent extends JComponent {
    //初始化
    private Ball ball = null;
    private Paddle paddle = null;

    // 获取对象
    public MyComponent(Ball ball, Paddle paddle) {
        this.ball = ball;
        this.paddle = paddle;
    }

    //调用画图
    public void paintComponent(Graphics g) {

        ball.draw(g);
        paddle.draw(g);
    }

    @Override
    public Dimension getPreferredSize() {
        return super.getPreferredSize();
    }

}

4.Paddle

public class Paddle {
    /**
     * @param PADDLE_WIDTH 挡板宽度
     * @param PADDLE_HEIGHT 挡板高度
     */
    public static final int PADDLE_WIDTH = 125;
    public static final int PADDLE_HEIGHT = 25;

    private int x = 125;
    private int y = 300;
    private int speed = 15;

    //绘图
    public void draw(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
    }

    public  void moveLeft(){
        if(x > 0)
            x -= speed;
    }

    public void moveRight(){
        if(x < 400-PADDLE_WIDTH)
            x += speed;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

5.Ball

public class Ball {
    /**
     * @param BALL_WIDTH 球宽度
     * @param BALL_HEIGHT 球高度
     */
    private static final int BALL_WIDTH = 25;
    private static final int BALL_HEIGHT = 25;

    private int x = 175;
    private int y = 100;
    private int speedX = 8;
    private int speedY = 8;
    private boolean fired = false;

    public void draw(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        //抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawOval(x, y, BALL_WIDTH, BALL_HEIGHT);
    }

    public void fire() {
        fired = true;
    }

    public void move() {
        if (fired) {
            if (x <= 0 || x >= 400)
                speedX *= -1;
            if (y <= 0 || y >= 400)
                speedY *= -1;
            x += speedX;
            y += speedY;
        }
    }

    public void collide(int x, int y, int width, int height) {
        if (this.x >= x && this.x <= x + width) {
            if (this.y +BALL_HEIGHT > y && this.y < y + height) {
                //与球碰撞
                speedY *= -1;
            }
        }
    }
}

代码思路来自萧大,我只是用Java实现的,我觉得一些地方还不够抽象,面向对象这种思路太难,琢磨不透。
当然欢迎看完,谢谢!
有错误的、有问题的、写的不好的,请指出!!!谢谢观看
BUG还是有的,球进了侧面就有问题。
打砖块游戏(2),还不知道什么时候继续出。

你可能感兴趣的:(Java Swing 打砖块游戏(1))