使用Java实现飞机大战
时间: 2023-11-15 15:07:01 浏览: 209
Java实现飞机大战
飞机大战是一款经典的游戏,下面我介绍一下使用Java实现飞机大战的基本思路:
1. 创建游戏窗口
使用Java中的JFrame类创建一个游戏窗口,设置窗口大小、标题、背景色等属性。
2. 加载游戏资源
使用Java中的Image类加载游戏所需的图片资源,包括背景图、飞机图、子弹图、敌机图等。
3. 创建游戏角色
创建游戏角色,包括玩家飞机、敌机、子弹等,使用Java中的JPanel类将它们添加到游戏窗口中。
4. 添加游戏事件
为游戏角色添加事件,包括玩家飞机的移动、子弹的发射、敌机的生成等。
5. 实现游戏逻辑
实现游戏的逻辑,包括碰撞检测、得分计算、游戏结束判断等。
6. 实现游戏界面
使用Java中的Graphics类绘制游戏界面,包括背景图、游戏角色、得分、生命等信息。
7. 运行游戏
将游戏窗口设置为可见状态,启动游戏。
上述是Java实现飞机大战的基本思路,具体实现可以参考以下示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PlaneGame extends JFrame {
private static final int WIDTH = 500;
private static final int HEIGHT = 700;
private final Image background = new ImageIcon("resources/background.jpg").getImage();
private final Image airplane = new ImageIcon("resources/airplane.png").getImage();
private final Image bullet = new ImageIcon("resources/bullet.png").getImage();
private final Image enemyAirplane = new ImageIcon("resources/enemy_airplane.png").getImage();
private final List<Bullet> bullets = new ArrayList<>();
private final List<EnemyAirplane> enemyAirplanes = new ArrayList<>();
private int score = 0;
private int life = 3;
private final Random random = new Random();
private int playerX = WIDTH / 2 - airplane.getWidth(null) / 2;
private int playerY = HEIGHT - airplane.getHeight(null) - 50;
private boolean isMovingLeft = false;
private boolean isMovingRight = false;
private boolean isFiring = false;
public PlaneGame() {
setSize(WIDTH, HEIGHT);
setTitle("Plane Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
isMovingLeft = true;
break;
case KeyEvent.VK_RIGHT:
isMovingRight = true;
break;
case KeyEvent.VK_SPACE:
isFiring = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
isMovingLeft = false;
break;
case KeyEvent.VK_RIGHT:
isMovingRight = false;
break;
case KeyEvent.VK_SPACE:
isFiring = false;
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
});
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null);
g.drawImage(airplane, playerX, playerY, null);
for (Bullet bullet : bullets) {
g.drawImage(this.bullet, bullet.getX(), bullet.getY(), null);
}
for (EnemyAirplane enemyAirplane : enemyAirplanes) {
g.drawImage(this.enemyAirplane, enemyAirplane.getX(), enemyAirplane.getY(), null);
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Score: " + score, 20, 30);
g.drawString("Life: " + life, WIDTH - 100, 30);
}
public void start() {
while (life > 0) {
if (isMovingLeft) {
playerX -= 5;
}
if (isMovingRight) {
playerX += 5;
}
if (isFiring) {
bullets.add(new Bullet(playerX + airplane.getWidth(null) / 2, playerY));
}
for (Bullet bullet : bullets) {
bullet.move();
}
for (EnemyAirplane enemyAirplane : enemyAirplanes) {
enemyAirplane.move();
}
if (random.nextInt(100) < 10) {
enemyAirplanes.add(new EnemyAirplane(random.nextInt(WIDTH - enemyAirplane.getWidth(null)), -enemyAirplane.getHeight(null)));
}
for (Bullet bullet : bullets) {
for (EnemyAirplane enemyAirplane : enemyAirplanes) {
if (bullet.isCollidedWith(enemyAirplane)) {
score++;
bullets.remove(bullet);
enemyAirplanes.remove(enemyAirplane);
break;
}
}
}
for (EnemyAirplane enemyAirplane : enemyAirplanes) {
if (enemyAirplane.isCollidedWith(playerX, playerY, airplane.getWidth(null), airplane.getHeight(null))) {
life--;
enemyAirplanes.remove(enemyAirplane);
break;
}
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(this, "Game Over!\nScore: " + score);
System.exit(0);
}
private static class Bullet {
private static final int SPEED = 10;
private final int x;
private int y;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public void move() {
y -= SPEED;
if (y < -bullet.getHeight(null)) {
bullets.remove(this);
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isCollidedWith(EnemyAirplane enemyAirplane) {
int bulletX = getX() + bullet.getWidth(null) / 2;
int bulletY = getY() + bullet.getHeight(null) / 2;
int enemyAirplaneX = enemyAirplane.getX() + enemyAirplane.getWidth(null) / 2;
int enemyAirplaneY = enemyAirplane.getY() + enemyAirplane.getHeight(null) / 2;
int distance = (int) Math.sqrt(Math.pow(bulletX - enemyAirplaneX, 2) + Math.pow(bulletY - enemyAirplaneY, 2));
return distance < bullet.getWidth(null) / 2 + enemyAirplane.getWidth(null) / 2;
}
}
private static class EnemyAirplane {
private static final int SPEED = 5;
private final int x;
private int y;
public EnemyAirplane(int x, int y) {
this.x = x;
this.y = y;
}
public void move() {
y += SPEED;
if (y > HEIGHT) {
enemyAirplanes.remove(this);
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isCollidedWith(int x, int y, int width, int height) {
int enemyAirplaneX = getX() + enemyAirplane.getWidth(null) / 2;
int enemyAirplaneY = getY() + enemyAirplane.getHeight(null) / 2;
int distance = (int) Math.sqrt(Math.pow(enemyAirplaneX - x - width / 2, 2) + Math.pow(enemyAirplaneY - y - height / 2, 2));
return distance < enemyAirplane.getWidth(null) / 2 + width / 2;
}
}
public static void main(String[] args) {
new PlaneGame().start();
}
}
```
上述代码实现了基本的飞机大战游戏,包括玩家飞机的移动、子弹的发射、敌机的生成、碰撞检测、得分计算、游戏结束等功能。可以根据实际需求进行修改和扩展。
阅读全文