public class BattleGame extends JFrame { private GamePanel gamePanel = new GamePanel();
public BattleGame() { this.setTitle("飞机坦克大战"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800, 600); this.add(gamePanel); this.setVisible(true); }
public static void main(String[] args) { new BattleGame(); } }
class GamePanel extends JPanel implements KeyListener, ActionListener { private PlayerTank player; private ArrayList<EnemyPlane> enemies = new ArrayList<>(); private Timer timer; private boolean[] keys = new boolean[256];
public GamePanel() { this.setFocusable(true); this.addKeyListener(this); player = new PlayerTank(400, 500); timer = new Timer(16, this); timer.start(); spawnEnemy(); }