用java写一个飞机大战游戏
时间: 2023-12-19 09:07:51 浏览: 67
好的,关于飞机大战游戏的开发,我可以为您提供一些建议和思路。首先,您可以使用 Java 语言,配合使用游戏引擎来开发这个游戏。您可以先搭建好游戏引擎的框架,然后再进行游戏场景的绘制和游戏角色的设计。在游戏角色的设计中,您可以为玩家设计不同的飞机,每个飞机都有特有的属性和技能。同时,您也需要为敌人设计不同的角色,包括飞机和BOSS,他们具有不同的攻击和防御方式,需要玩家使用不同的策略进行击败。最后,为了提高游戏的趣味性,您可以增加各种特效和音效,使游戏更加动感和有趣。希望这些建议可以为您带来一些灵感,祝您开发成功!
相关问题
用java写一个飞机大战的游戏
这里是一个基于Java Swing的简单飞机大战游戏的示例代码,仅供参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class PlaneGame extends JFrame {
// 窗口大小
private static final int FRAME_WIDTH = 480;
private static final int FRAME_HEIGHT = 700;
// 飞机移动速度
private static final int PLANE_SPEED = 8;
// 子弹速度
private static final int BULLET_SPEED = 20;
// 敌机生成速度
private static final int ENEMY_PLANE_INTERVAL = 100;
// 敌机移动速度
private static final int ENEMY_PLANE_SPEED = 5;
// 分数
private int score = 0;
// 飞机图片
private Image planeImage;
// 子弹图片
private Image bulletImage;
// 敌机图片
private Image enemyPlaneImage;
// 爆炸图片
private Image boomImage;
// 飞机坐标
private int planeX;
private int planeY;
// 飞机状态
private boolean planeAlive = true;
// 子弹列表
private ArrayList<Bullet> bulletList = new ArrayList<>();
// 敌机列表
private ArrayList<EnemyPlane> enemyPlaneList = new ArrayList<>();
public PlaneGame() {
// 设置标题
setTitle("Plane Game");
// 设置窗口大小
setSize(FRAME_WIDTH, FRAME_HEIGHT);
// 设置窗口不可调整大小
setResizable(false);
// 居中显示窗口
setLocationRelativeTo(null);
// 关闭窗口时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加键盘监听器
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 获取键盘按下的键
int keyCode = e.getKeyCode();
// 根据按键移动飞机
if (keyCode == KeyEvent.VK_LEFT) {
planeX -= PLANE_SPEED;
} else if (keyCode == KeyEvent.VK_RIGHT) {
planeX += PLANE_SPEED;
} else if (keyCode == KeyEvent.VK_UP) {
planeY -= PLANE_SPEED;
} else if (keyCode == KeyEvent.VK_DOWN) {
planeY += PLANE_SPEED;
} else if (keyCode == KeyEvent.VK_SPACE) {
// 发射子弹
Bullet bullet = new Bullet(planeX + planeImage.getWidth(null) / 2 - bulletImage.getWidth(null) / 2, planeY - bulletImage.getHeight(null));
bulletList.add(bullet);
}
}
});
// 加载图片
planeImage = new ImageIcon("plane.png").getImage();
bulletImage = new ImageIcon("bullet.png").getImage();
enemyPlaneImage = new ImageIcon("enemy_plane.png").getImage();
boomImage = new ImageIcon("boom.png").getImage();
// 初始化飞机坐标
planeX = FRAME_WIDTH / 2 - planeImage.getWidth(null) / 2;
planeY = FRAME_HEIGHT - planeImage.getHeight(null) - 50;
// 开始游戏循环
startGameLoop();
// 显示窗口
setVisible(true);
}
// 开始游戏循环
private void startGameLoop() {
// 创建游戏循环线程
Thread gameLoopThread = new Thread(() -> {
while (true) {
try {
// 生成敌机
if (Math.random() * ENEMY_PLANE_INTERVAL < 1) {
EnemyPlane enemyPlane = new EnemyPlane((int) (Math.random() * (FRAME_WIDTH - enemyPlaneImage.getWidth(null))), -enemyPlaneImage.getHeight(null));
enemyPlaneList.add(enemyPlane);
}
// 移动子弹
for (int i = bulletList.size() - 1; i >= 0; i--) {
Bullet bullet = bulletList.get(i);
bullet.y -= BULLET_SPEED;
// 判断子弹是否击中敌机
for (int j = enemyPlaneList.size() - 1; j >= 0; j--) {
EnemyPlane enemyPlane = enemyPlaneList.get(j);
if (bullet.x >= enemyPlane.x && bullet.x <= enemyPlane.x + enemyPlaneImage.getWidth(null) && bullet.y >= enemyPlane.y && bullet.y <= enemyPlane.y + enemyPlaneImage.getHeight(null)) {
// 击中敌机,移除子弹和敌机,并增加分数
bulletList.remove(i);
enemyPlaneList.remove(j);
score += 10;
break;
}
}
// 移除越界的子弹
if (bullet.y < -bulletImage.getHeight(null)) {
bulletList.remove(i);
}
}
// 移动敌机
for (int i = enemyPlaneList.size() - 1; i >= 0; i--) {
EnemyPlane enemyPlane = enemyPlaneList.get(i);
enemyPlane.y += ENEMY_PLANE_SPEED;
// 判断敌机是否撞击到飞机
if (enemyPlane.x + enemyPlaneImage.getWidth(null) >= planeX && enemyPlane.x <= planeX + planeImage.getWidth(null) && enemyPlane.y + enemyPlaneImage.getHeight(null) >= planeY && enemyPlane.y <= planeY + planeImage.getHeight(null)) {
// 撞击到飞机,游戏结束
planeAlive = false;
break;
}
// 移除越界的敌机
if (enemyPlane.y > FRAME_HEIGHT) {
enemyPlaneList.remove(i);
}
}
// 重绘窗口
repaint();
// 等待一段时间
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// 启动游戏循环线程
gameLoopThread.start();
}
@Override
public void paint(Graphics g) {
// 创建双缓冲图像
Image doubleBufferImage = createImage(getWidth(), getHeight());
Graphics doubleBufferGraphics = doubleBufferImage.getGraphics();
// 绘制背景
doubleBufferGraphics.setColor(Color.WHITE);
doubleBufferGraphics.fillRect(0, 0, getWidth(), getHeight());
// 绘制分数
doubleBufferGraphics.setColor(Color.BLACK);
doubleBufferGraphics.setFont(new Font("微软雅黑", Font.BOLD, 20));
doubleBufferGraphics.drawString("分数:" + score, 20, 40);
// 绘制飞机
if (planeAlive) {
doubleBufferGraphics.drawImage(planeImage, planeX, planeY, null);
} else {
doubleBufferGraphics.drawImage(boomImage, planeX, planeY, null);
doubleBufferGraphics.setColor(Color.RED);
doubleBufferGraphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
doubleBufferGraphics.drawString("游戏结束", FRAME_WIDTH / 2 - 100, FRAME_HEIGHT / 2);
}
// 绘制子弹
for (Bullet bullet : bulletList) {
doubleBufferGraphics.drawImage(bulletImage, bullet.x, bullet.y, null);
}
// 绘制敌机
for (EnemyPlane enemyPlane : enemyPlaneList) {
doubleBufferGraphics.drawImage(enemyPlaneImage, enemyPlane.x, enemyPlane.y, null);
}
// 绘制双缓冲图像到窗口
g.drawImage(doubleBufferImage, 0, 0, null);
}
// 子弹类
private static class Bullet {
int x;
int y;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
}
// 敌机类
private static class EnemyPlane {
int x;
int y;
public EnemyPlane(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
new PlaneGame();
}
}
```
注意:这个示例代码只是一个简单的飞机大战游戏,还有很多需要完善和优化的地方,比如增加多种敌机、道具、关卡等。
用java写一个飞机大战
好的,下面是一个简单的 Java 飞机大战的示例代码,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class PlaneWar extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel mainPanel;
private JButton startButton, pauseButton, continueButton, exitButton;
private JLabel scoreLabel, timeLabel;
private Timer timer;
private int score = 0, time = 0;
private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
private MyPlane myPlane = new MyPlane();
private boolean isPaused = false;
public PlaneWar() {
super("Plane War");
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setLayout(null);
this.add(mainPanel);
startButton = new JButton("Start");
startButton.setBounds(10, 10, 80, 30);
startButton.addActionListener(this);
mainPanel.add(startButton);
pauseButton = new JButton("Pause");
pauseButton.setBounds(100, 10, 80, 30);
pauseButton.addActionListener(this);
mainPanel.add(pauseButton);
continueButton = new JButton("Continue");
continueButton.setBounds(190, 10, 80, 30);
continueButton.addActionListener(this);
mainPanel.add(continueButton);
exitButton = new JButton("Exit");
exitButton.setBounds(280, 10, 80, 30);
exitButton.addActionListener(this);
mainPanel.add(exitButton);
scoreLabel = new JLabel("Score: 0");
scoreLabel.setBounds(400, 10, 80, 30);
mainPanel.add(scoreLabel);
timeLabel = new JLabel("Time: 0");
timeLabel.setBounds(490, 10, 80, 30);
mainPanel.add(timeLabel);
timer = new Timer(50, this);
timer.start();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
isPaused = false;
myPlane.reset();
enemies.clear();
score = 0;
time = 0;
scoreLabel.setText("Score: " + score);
timeLabel.setText("Time: " + time);
} else if (e.getSource() == pauseButton) {
isPaused = true;
} else if (e.getSource() == continueButton) {
isPaused = false;
} else if (e.getSource() == exitButton) {
System.exit(0);
} else if (e.getSource() == timer) {
if (!isPaused) {
if (Math.random() < 0.05) {
enemies.add(new Enemy());
}
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
enemy.move();
if (enemy.isCollided(myPlane)) {
JOptionPane.showMessageDialog(this, "Game Over!");
isPaused = true;
}
if (enemy.getY() > 600) {
enemies.remove(i);
i--;
}
}
myPlane.move();
time++;
timeLabel.setText("Time: " + time);
scoreLabel.setText("Score: " + score);
}
}
mainPanel.repaint();
}
class MyPlane {
private int x = 250, y = 500, width = 50, height = 50;
public void move() {
if (isKeyDown(KeyEvent.VK_LEFT)) {
x -= 5;
}
if (isKeyDown(KeyEvent.VK_RIGHT)) {
x += 5;
}
if (isKeyDown(KeyEvent.VK_UP)) {
y -= 5;
}
if (isKeyDown(KeyEvent.VK_DOWN)) {
y += 5;
}
if (x < 0) {
x = 0;
}
if (x > 550) {
x = 550;
}
if (y < 0) {
y = 0;
}
if (y > 550) {
y = 550;
}
}
public void reset() {
x = 250;
y = 500;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
private boolean isKeyDown(int keyCode) {
return Toolkit.getDefaultToolkit().getLockingKeyState(keyCode);
}
}
class Enemy {
private int x, y, width = 50, height = 50, speed = 5;
public Enemy() {
x = (int) (Math.random() * 500);
y = -50;
}
public void move() {
y += speed;
}
public boolean isCollided(MyPlane myPlane) {
return x + width >= myPlane.getX() && x <= myPlane.getX() + myPlane.getWidth()
&& y + height >= myPlane.getY() && y <= myPlane.getY() + myPlane.getHeight();
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(new ImageIcon("plane.jpg").getImage(), myPlane.getX(), myPlane.getY(), myPlane.getWidth(),
myPlane.getHeight(), null);
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
g.drawImage(new ImageIcon("enemy.jpg").getImage(), enemy.getX(), enemy.getY(), enemy.getWidth(),
enemy.getHeight(), null);
}
}
public static void main(String[] args) {
new PlaneWar();
}
}
```
这个示例代码中使用了 Swing 组件和 Java 的定时器 Timer,其中 MyPlane 类表示玩家的飞机,Enemy 类表示敌机。在窗口上绘制飞机和敌机使用了 Graphics 类的 drawImage() 方法。
当玩家和敌机碰撞时,会弹出提示框 Game Over。在游戏界面中,玩家可以使用方向键来控制飞机的移动,按下空格键可以开火攻击敌机,但是这个示例代码中并没有实现开火攻击敌机的功能,你可以自己尝试添加。
阅读全文