java语言实现飞机大战
时间: 2023-07-07 15:26:17 浏览: 97
好的,下面是一个简单的Java实现飞机大战的示例代码,希望能够帮到您:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class PlaneWar extends JFrame implements ActionListener, KeyListener {
private Timer timer;
private int score = 0;
private ImageIcon bgImageIcon, playerImageIcon, enemyImageIcon, bulletImageIcon;
private JLabel bgLabel, playerLabel, scoreLabel;
private ArrayList<JLabel> enemyLabels, bulletLabels;
public PlaneWar() {
initUI();
initGame();
}
private void initUI() {
bgImageIcon = new ImageIcon(getClass().getResource("bg.jpg"));
bgLabel = new JLabel(bgImageIcon);
bgLabel.setBounds(0, 0, 400, 600);
add(bgLabel);
playerImageIcon = new ImageIcon(getClass().getResource("player.png"));
playerLabel = new JLabel(playerImageIcon);
playerLabel.setBounds(180, 500, 40, 40);
bgLabel.add(playerLabel);
scoreLabel = new JLabel("Score: " + score);
scoreLabel.setBounds(10, 10, 100, 20);
scoreLabel.setForeground(Color.WHITE);
bgLabel.add(scoreLabel);
enemyImageIcon = new ImageIcon(getClass().getResource("enemy.png"));
enemyLabels = new ArrayList<>();
bulletImageIcon = new ImageIcon(getClass().getResource("bullet.png"));
bulletLabels = new ArrayList<>();
setTitle("PlaneWar");
setSize(400, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
addKeyListener(this);
setVisible(true);
}
private void initGame() {
timer = new Timer(10, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
moveEnemy();
moveBullet();
checkCollisions();
}
private void moveEnemy() {
Random random = new Random();
if (random.nextInt(100) < 3) {
JLabel enemyLabel = new JLabel(enemyImageIcon);
enemyLabel.setBounds(random.nextInt(360), -50, 50, 50);
bgLabel.add(enemyLabel);
enemyLabels.add(enemyLabel);
}
for (int i = 0; i < enemyLabels.size(); i++) {
JLabel enemyLabel = enemyLabels.get(i);
enemyLabel.setLocation(enemyLabel.getX(), enemyLabel.getY() + 1);
if (enemyLabel.getY() > 600) {
bgLabel.remove(enemyLabel);
enemyLabels.remove(enemyLabel);
}
}
}
private void moveBullet() {
for (int i = 0; i < bulletLabels.size(); i++) {
JLabel bulletLabel = bulletLabels.get(i);
bulletLabel.setLocation(bulletLabel.getX(), bulletLabel.getY() - 3);
if (bulletLabel.getY() < 0) {
bgLabel.remove(bulletLabel);
bulletLabels.remove(bulletLabel);
}
}
}
private void checkCollisions() {
Rectangle playerRect = playerLabel.getBounds();
for (int i = 0; i < enemyLabels.size(); i++) {
JLabel enemyLabel = enemyLabels.get(i);
Rectangle enemyRect = enemyLabel.getBounds();
if (playerRect.intersects(enemyRect)) {
gameOver();
}
for (int j = 0; j < bulletLabels.size(); j++) {
JLabel bulletLabel = bulletLabels.get(j);
Rectangle bulletRect = bulletLabel.getBounds();
if (bulletRect.intersects(enemyRect)) {
score += 10;
scoreLabel.setText("Score: " + score);
bgLabel.remove(enemyLabel);
bgLabel.remove(bulletLabel);
enemyLabels.remove(enemyLabel);
bulletLabels.remove(bulletLabel);
}
}
}
}
private void gameOver() {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over!\nYour score is " + score);
System.exit(0);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP && playerLabel.getY() > 0) {
playerLabel.setLocation(playerLabel.getX(), playerLabel.getY() - 5);
} else if (keyCode == KeyEvent.VK_DOWN && playerLabel.getY() < 560) {
playerLabel.setLocation(playerLabel.getX(), playerLabel.getY() + 5);
} else if (keyCode == KeyEvent.VK_LEFT && playerLabel.getX() > 0) {
playerLabel.setLocation(playerLabel.getX() - 5, playerLabel.getY());
} else if (keyCode == KeyEvent.VK_RIGHT && playerLabel.getX() < 360) {
playerLabel.setLocation(playerLabel.getX() + 5, playerLabel.getY());
} else if (keyCode == KeyEvent.VK_SPACE) {
JLabel bulletLabel = new JLabel(bulletImageIcon);
bulletLabel.setBounds(playerLabel.getX() + 17, playerLabel.getY() - 20, 6, 20);
bgLabel.add(bulletLabel);
bulletLabels.add(bulletLabel);
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
new PlaneWar();
}
}
```
这是一个简单的飞机大战示例程序,实现了玩家飞机的移动、发射子弹、敌机的随机生成和移动、碰撞检测等功能。如果您需要更详细的解释,可以参考代码注释或者在评论区提出问题。
阅读全文