用java写一个飞机大战小游戏,只要代码
时间: 2023-09-19 22:06:42 浏览: 199
### 回答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);
}
}
阅读全文