用java写一个打飞机小游戏
时间: 2023-12-07 19:30:03 浏览: 98
好的,以下是一个简单的打飞机小游戏的 Java 代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class PlaneGame extends JFrame implements ActionListener {
private Timer timer;
private MyPlane myPlane;
private ArrayList<EnemyPlane> enemyPlanes;
private ArrayList<Bullet> bullets;
private JLabel scoreLabel;
private int score;
public PlaneGame() {
setTitle("打飞机小游戏");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gamePanel = new JPanel();
gamePanel.setPreferredSize(new Dimension(400, 500));
gamePanel.setBackground(Color.WHITE);
myPlane = new MyPlane(180, 450);
enemyPlanes = new ArrayList<>();
bullets = new ArrayList<>();
scoreLabel = new JLabel("得分:0");
mainPanel.add(scoreLabel, BorderLayout.NORTH);
mainPanel.add(gamePanel, BorderLayout.CENTER);
add(mainPanel);
timer = new Timer(30, this);
timer.start();
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
myPlane.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
myPlane.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
myPlane.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
myPlane.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bullets.add(new Bullet(myPlane.getX() + 25, myPlane.getY()));
}
}
});
}
public void actionPerformed(ActionEvent e) {
scoreLabel.setText("得分:" + score);
myPlane.update();
for (int i = enemyPlanes.size() - 1; i >= 0; i--) {
EnemyPlane enemyPlane = enemyPlanes.get(i);
enemyPlane.update();
if (enemyPlane.getY() > 500) {
enemyPlanes.remove(i);
} else if (myPlane.isCollide(enemyPlane)) {
gameOver();
}
}
for (int i = bullets.size() - 1; i >= 0; i--) {
Bullet bullet = bullets.get(i);
bullet.update();
if (bullet.getY() < 0) {
bullets.remove(i);
} else {
for (int j = enemyPlanes.size() - 1; j >= 0; j--) {
EnemyPlane enemyPlane = enemyPlanes.get(j);
if (bullet.isCollide(enemyPlane)) {
enemyPlanes.remove(j);
bullets.remove(i);
score += 10;
break;
}
}
}
}
if (Math.random() < 0.03) {
int x = (int) (Math.random() * 350);
enemyPlanes.add(new EnemyPlane(x, 0));
}
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
myPlane.draw(g);
for (Bullet bullet : bullets) {
bullet.draw(g);
}
for (EnemyPlane enemyPlane : enemyPlanes) {
enemyPlane.draw(g);
}
}
private void gameOver() {
timer.stop();
JOptionPane.showMessageDialog(this, "得分:" + score, "游戏结束", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
public static void main(String[] args) {
PlaneGame game = new PlaneGame();
game.setVisible(true);
}
}
class MyPlane {
private int x, y;
public MyPlane(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void moveLeft() {
x -= 10;
if (x < 0) {
x = 0;
}
}
public void moveRight() {
x += 10;
if (x > 350) {
x = 350;
}
}
public void moveUp() {
y -= 10;
if (y < 0) {
y = 0;
}
}
public void moveDown() {
y += 10;
if (y > 450) {
y = 450;
}
}
public boolean isCollide(EnemyPlane enemyPlane) {
int dx = x - enemyPlane.getX();
int dy = y - enemyPlane.getY();
int distance = (int) Math.sqrt(dx * dx + dy * dy);
return distance < 30;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
public void update() {
}
}
class EnemyPlane {
private int x, y;
public EnemyPlane(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x, y, 30, 30);
}
public void update() {
y += 5;
}
}
class Bullet {
private int x, y;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isCollide(EnemyPlane enemyPlane) {
int dx = x - enemyPlane.getX();
int dy = y - enemyPlane.getY();
int distance = (int) Math.sqrt(dx * dx + dy * dy);
return distance < 15;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 10, 10);
}
public void update() {
y -= 10;
}
}
```
您可以将此代码复制到 Java IDE 中进行运行和测试。
阅读全文