请用java写一个飞机大战小游戏
时间: 2023-10-06 09:07:05 浏览: 18
好的,我会尽力用Java来写一个飞机大战小游戏。
首先,我们需要创建一个飞机类,来表示飞机的状态和行为。我们可以让飞机具有属性如位置、速度、生命值等,并且可以通过方法如移动、射击、受伤来控制飞机的行为。
接着,我们可以创建一个游戏类,来管理游戏的整体逻辑。游戏类应该包含一个飞机的实例,并且可以通过方法如开始游戏、暂停游戏、结束游戏来控制游戏的流程。此外,游戏类还应该包含方法来处理玩家的输入、更新游戏画面、检测碰撞等。
最后,我们需要创建一个主函数,来启动游戏。主函数中应该实例化一个游戏类,并调用它的开始游戏方法。
以下是一个简单的飞机大战小游戏的代码示例:
```
// 飞机类
class Plane {
// 属性:位置、速度、生命值
private int x;
private int y;
private int speed;
private int health;
// 构造方法:初始化飞机的状态
public Plane(int x, int y, int speed, int health) {
this.x = x;
this.y = y;
this.speed = speed;
this.health = health;
}
// 方法:移动飞机
public void move(int dx, int dy
相关问题
用java写一个飞机大战小游戏,只要代码
### 回答1:
下面是一个简单的 Java 飞机大战小游戏的代码示例:
```
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PlaneGame extends JPanel {
// 定义一个飞机对象
private Plane plane;
// 初始化窗口
public void init() {
plane = new Plane("plane.png", 250, 250);
// 创建一个窗口
JFrame frame = new JFrame("飞机大战");
frame.add(this);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// 绘制窗口内容的方法
public void paint(Graphics g) {
super.paint(g);
g.drawImage(plane.getImage(), plane.getX(), plane.getY(), null);
}
public static void main(String[] args) {
new PlaneGame().init();
}
}
// 飞机类
class Plane {
// 定义飞机图片
private Image image;
// 定义飞机的坐标
private int x, y;
public Plane(String imagePath, int x, int y) {
// 初始化图片
this.image = new ImageIcon(imagePath).getImage();
this.x = x;
this.y = y;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
```
上面的代码实现了一个简单的 Java 飞机大战小游戏,其中定义了一个 PlaneGame 类,该类继承了 JPanel 类,并在其中定义了一个飞机对象。init() 方法用于初始化窗口,paint() 方
### 回答2:
以下是一个简单的飞机大战小游戏的Java代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AircraftGame extends JFrame {
private final int FRAME_WIDTH = 400;
private final int FRAME_HEIGHT = 600;
private final int PLANE_WIDTH = 50;
private final int PLANE_HEIGHT = 50;
private final int ENEMY_WIDTH = 30;
private final int ENEMY_HEIGHT = 30;
private int planeX;
private int planeY;
private int enemyX;
private int enemyY;
public AircraftGame() {
setTitle("飞机大战");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
planeX = FRAME_WIDTH / 2 - PLANE_WIDTH / 2;
planeY = FRAME_HEIGHT - PLANE_HEIGHT;
enemyX = (int) (Math.random() * (FRAME_WIDTH - ENEMY_WIDTH));
enemyY = 0;
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
planeX -= 10;
if (planeX < 0) {
planeX = 0;
}
} else if (key == KeyEvent.VK_RIGHT) {
planeX += 10;
if (planeX > FRAME_WIDTH - PLANE_WIDTH) {
planeX = FRAME_WIDTH - PLANE_WIDTH;
}
}
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillRect(planeX, planeY, PLANE_WIDTH, PLANE_HEIGHT);
g.setColor(Color.RED);
g.fillRect(enemyX, enemyY, ENEMY_WIDTH, ENEMY_HEIGHT);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AircraftGame();
}
});
}
}
```
这是一个基于Java的飞机大战小游戏代码。游戏窗口大小为400x600像素,玩家控制的飞机为50x50像素的蓝色矩形,敌机为30x30像素的红色矩形。玩家可以通过键盘的左右键控制飞机的移动,飞机只能在窗口内移动,不能超出边界。游戏中敌机会从顶部随机位置下降,玩家需要通过控制飞机避开敌机。游戏仅提供简单的基本功能,可以根据需要进行扩展和改进。
### 回答3:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AirplaneGame extends JPanel {
private Image background, playerImage, enemyImage;
private int playerX, playerY, playerSpeed;
private List<Integer> enemyX, enemyY, enemySpeed;
public AirplaneGame() {
background = new ImageIcon("background.jpg").getImage();
playerImage = new ImageIcon("player.png").getImage();
enemyImage = new ImageIcon("enemy.png").getImage();
playerX = 400;
playerY = 550;
playerSpeed = 5;
enemyX = new ArrayList<>();
enemyY = new ArrayList<>();
enemySpeed = new ArrayList<>();
for (int i = 0; i < 5; i++) {
enemyX.add((int) (Math.random() * 750));
enemyY.add((int) (Math.random() * 300));
enemySpeed.add((int) (Math.random() * 5) + 1);
}
setPreferredSize(new Dimension(800, 600));
setBackground(Color.black);
setFocusable(true);
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
if (playerX > 0) playerX -= playerSpeed;
}
if (keyCode == KeyEvent.VK_RIGHT) {
if (playerX < 750) playerX += playerSpeed;
}
if (keyCode == KeyEvent.VK_UP) {
if (playerY > 0) playerY -= playerSpeed;
}
if (keyCode == KeyEvent.VK_DOWN) {
if (playerY < 550) playerY += playerSpeed;
}
}
@Override
public void keyReleased(KeyEvent e) {}
});
Thread gameThread = new Thread(() -> {
while (true) {
for (int i = 0; i < enemyY.size(); i++) {
enemyY.set(i, enemyY.get(i) + enemySpeed.get(i));
if (enemyY.get(i) > 600) {
enemyX.set(i, (int) (Math.random() * 750));
enemyY.set(i, -100);
enemySpeed.set(i, (int) (Math.random() * 5) + 1);
}
if (Math.abs(playerX - enemyX.get(i)) < 50 && Math.abs(playerY - enemyY.get(i)) < 50) {
System.exit(0);
}
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
gameThread.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(playerImage, playerX, playerY, this);
for (int i = 0; i < enemyX.size(); i++) {
g.drawImage(enemyImage, enemyX.get(i), enemyY.get(i), this);
}
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("SCORE: " + enemyY.size(), 20, 580);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Airplane Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new AirplaneGame());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
用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();
}
}
```
注意:这个示例代码只是一个简单的飞机大战游戏,还有很多需要完善和优化的地方,比如增加多种敌机、道具、关卡等。
阅读全文