java实现飞机大战代码
时间: 2023-11-12 07:55:54 浏览: 295
下面是一个简单的 Java 实现飞机大战的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlaneWar extends JFrame {
private MyPanel mp;
public PlaneWar() {
mp = new MyPanel();
this.add(mp);
this.setTitle("PlaneWar");
this.setSize(400, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new PlaneWar();
}
}
class MyPanel extends JPanel {
private ImageIcon bg;
private ImageIcon plane;
private int planeX = 175;
private int planeY = 500;
public MyPanel() {
bg = new ImageIcon("bg.jpg");
plane = new ImageIcon("plane.png");
this.addMouseListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
planeX = e.getX() - plane.getIconWidth() / 2;
planeY = e.getY() - plane.getIconHeight() / 2;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg.getImage(), 0, 0, null);
g.drawImage(plane.getImage(), planeX, planeY, null);
}
}
```
这个简单的实现只有一个玩家飞机,玩家可以通过鼠标控制飞机的移动。如果想要更加完整的实现,还需要添加敌方飞机、子弹、碰撞检测等功能。
阅读全文