前段时间写的少帅下飞机,分享给大家哈哈

package ThreadPool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Shaoshuai extends JPanel implements ActionListener {

//    少帅,补药啊
//    少帅,补药啊

    private int planeX = 0;
    private final int planeY = 50;
    private int bombX1 = -1;
    private int bombY1 = -1;
    private int bombX2 = -1;
    private int bombY2 = -1;
    private final int soldier1X = 500;
    private final int soldier2X = 700;
    private final int soldierY = 600;
    private final Timer planeTimer;
    private final Timer bombTimer;
    private boolean exploded = false;
    private boolean showMessage = false;
    public Shaoshuai() {
        planeTimer = new Timer(10, this);
        bombTimer = new Timer(10, new BombDropListener());
        planeTimer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setFont(new Font("Monospaced", Font.BOLD, 30));
        g.drawString("=|==>", planeX, planeY);
        if (!exploded) {
            if (bombY1 >= 0) {
                g.drawString("少", bombX1, bombY1);
            }
            if (bombY2 >= 0) {
                g.drawString("帅", bombX2, bombY2);
            }
        }else if (showMessage) {
            g.setFont(new Font("Monospaced", Font.BOLD, 90));
            g.drawString("少帅的辉煌一生", getWidth() / 3, getHeight() / 2);
        }
        g.setFont(new Font("Monospaced", Font.BOLD, 40));
        g.drawString("卫兵", soldier1X, soldierY);
        g.drawString("卫兵", soldier2X, soldierY);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        planeX += 2;

        if (planeX > getWidth()) {
            planeX = -60;
        }

        // 当飞机到达中间位置时投放炸弹
        if (planeX == getWidth() / 2 && bombY1 == -1 && bombY2 == -1) {
            bombX1 = planeX + 40;
            bombX2 = planeX + 70;
            bombY1 = planeY;
            bombY2 = planeY;
            bombTimer.start();
        }

        repaint();
    }


    private class BombDropListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            bombY1 += 2;
            bombY2 += 2;

            if (bombY1 >= soldierY - 30) {
                bombY1 = -1;
                bombX1 = -1;
            }

            if (bombY2 >= soldierY - 30) {
                bombY2 = -1;
                bombX2 = -1;
                exploded = true;
                bombTimer.stop();


                Timer messageTimer = new Timer(200, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        showMessage = true;
                        repaint();
                    }
                });
                messageTimer.setRepeats(false);
                messageTimer.start();
            }
            repaint();
        }
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("少帅");
        Shaoshuai animation = new Shaoshuai();
        frame.add(animation);
        frame.setSize(1200, 1200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值