【JAVA】飞机大战

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

class MyThread extends Thread
{
    GameFrame gameFrame;

    public MyThread()
    {
        gameFrame = new GameFrame();
    }

    public void run()
    {
        System.out.println("run");
        Boolean runFlag = true;
        while (runFlag)
        {
            runFlag = gameFrame.Run();
            //System.out.println("runFlag: " + runFlag);
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        MyThread gameFrame = new MyThread();
        gameFrame.start();
    }
}

public class GameFrame extends JFrame
{
    private Boolean runFlag;

    private final Graphics graphics;

    private final Image planeImage;

    private final Image bulletImage;
    private final Image backgroundImage1;
    private final Image backgroundImage2;

    //plane x
    private int planeX;

    //plane y
    private int planeY;

    private int bulletX;
    private int bulletY;

    private boolean bulletFlag;
    private int backgroundY;

    //bullet move step
    private final int moveStep;

    private final int width;
    private final int height;

    public GameFrame()
    {
        setTitle("Fighting");
        setLayout(null);
        moveStep = 4;
        width = 629;
        height = 990;
        setSize(width, height);
        setLocationRelativeTo(null);
        setVisible(true);
        KeyCapture();
        backgroundY = 0;
        bulletFlag = false;
        planeY = height - 200;
        planeX = width / 2;
        runFlag = true;
        graphics = getContentPane().getGraphics();
        planeImage = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\bullet05.png");
        bulletImage = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\bullet_02.png");
        backgroundImage1 = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\mapback.png");
        backgroundImage2 = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\mapback.png");
    }

    public void KeyCapture()
    {
        this.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                int keyCode = e.getKeyCode();
                System.out.println("keyCode: " + keyCode);
                switch (keyCode)
                {
                    case KeyEvent.VK_UP:
                        planeY -= moveStep; break;

                    case KeyEvent.VK_DOWN:
                        planeY += moveStep; break;

                    case KeyEvent.VK_LEFT:
                        planeX -= moveStep; break;

                    case KeyEvent.VK_RIGHT:
                        planeX += moveStep; break;

                    case KeyEvent.VK_SPACE:
                        bulletX = planeX;
                        bulletY = planeY;
                        bulletFlag = true;
                        break;

                    case KeyEvent.VK_Q:
                        runFlag = false;
                        break;

                    default:
                        System.out.println("KeyCode error!");break;
                }
            }
        });
    }

    public Boolean Run()
    {
        //System.out.println("x: " + planeX + " y:" + planeY);

        // background
        graphics.drawImage(backgroundImage2, 0, backgroundY, width, height, this);
        graphics.drawImage(backgroundImage1, 0, backgroundY - height, width, height, this);

        // plane
        graphics.drawImage(planeImage, planeX, planeY, null);

        //bullet
        if (bulletFlag)
        {
            graphics.drawImage(bulletImage, bulletX, bulletY, null);
            bulletY -= 2 * moveStep;
        }

        backgroundY += 2;

        return runFlag;
    }
}

你可能感兴趣的:(java,python,前端)