我要一个飞机大战用java实现的程序
时间: 2023-10-06 09:05:20 浏览: 76
用Java实现飞机大战
好的,以下是一个简单的飞机大战Java程序的示例代码,您可以根据自己的需求进行修改和扩展。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PlaneGame extends JPanel implements KeyListener, Runnable {
private static final long serialVersionUID = 1L;
private BufferedImage background;
private BufferedImage planeImage;
private BufferedImage bulletImage;
private BufferedImage enemyImage;
private int planeX = 250;
private int planeY = 400;
private int bulletX = 0;
private int bulletY = 0;
private int enemyX = 50;
private int enemyY = 50;
private int enemySpeed = 3;
private boolean gameOver = false;
private List<Rectangle> bullets = new ArrayList<Rectangle>();
private List<Rectangle> enemies = new ArrayList<Rectangle>();
public PlaneGame() {
try {
background = ImageIO.read(new File("background.png"));
planeImage = ImageIO.read(new File("plane.png"));
bulletImage = ImageIO.read(new File("bullet.png"));
enemyImage = ImageIO.read(new File("enemy.png"));
} catch (IOException e) {
e.printStackTrace();
}
setFocusable(true);
addKeyListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(background, 0, 0, null);
g.drawImage(planeImage, planeX, planeY, null);
for (Rectangle bullet : bullets) {
g.drawImage(bulletImage, bullet.x, bullet.y, null);
}
for (Rectangle enemy : enemies) {
g.drawImage(enemyImage, enemy.x, enemy.y, null);
}
if (gameOver) {
g.setColor(Color.RED);
g.drawString("游戏结束!", 250, 250);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
planeX -= 5;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
planeX += 5;
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bullets.add(new Rectangle(planeX + 25, planeY - 10, 5, 10));
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void run() {
while (!gameOver) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).y -= 10;
if (bullets.get(i).y < 0) {
bullets.remove(i);
i--;
}
}
enemyY += enemySpeed;
if (enemyY > 480) {
enemyY = 50;
enemyX = (int) (Math.random() * 550);
}
Rectangle enemy = new Rectangle(enemyX, enemyY, 50, 50);
for (int i = 0; i < bullets.size(); i++) {
if (enemy.contains(bullets.get(i))) {
enemies.remove(enemy);
bullets.remove(i);
i--;
}
}
Rectangle plane = new Rectangle(planeX, planeY, 50, 50);
if (plane.intersects(enemy)) {
gameOver = true;
}
enemies.add(enemy);
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("飞机大战");
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PlaneGame game = new PlaneGame();
frame.add(game);
frame.setVisible(true);
Thread thread = new Thread(game);
thread.start();
}
}
```
这个程序实现了一个简单的飞机大战游戏,您可以使用左右方向键控制飞机的移动,按下空格键发射子弹,当子弹打中敌机时,敌机会消失,当敌机撞到飞机时,游戏结束。
阅读全文