飞机大战java代码

编写飞机大战游戏的Java代码是一个相对庞大的任务,因此我将为你提供一个简单版本的示例代码,以便你有一个基本的起点。

java
import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.List; import java.util.Random; class Bullet { int x, y; public Bullet(int x, int y) { this.x = x; this.y = y; } } class Enemy { int x, y; public Enemy(int x, int y) { this.x = x; this.y = y; } } class Plane { int x, y; public Plane(int x, int y) { this.x = x; this.y = y; } } public class PlaneGame extends JFrame { private List<Bullet> bullets; private List<Enemy> enemies; private Plane plane; public PlaneGame() { bullets = new ArrayList<>(); enemies = new ArrayList<>(); plane = new Plane(250, 250); addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { bullets.add(new Bullet(plane.x, plane.y)); } } @Override public void keyReleased(KeyEvent e) {} }); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void move() { for (Bullet bullet : bullets) { bullet.y -= 5; } for (Enemy enemy : enemies) { enemy.y += 2; } } public void generateEnemy() { Random random = new Random(); int x = random.nextInt(500); int y = 0; enemies.add(new Enemy(x, y)); } public void checkCollision() { List<Bullet> bulletsToRemove = new ArrayList<>(); List<Enemy> enemiesToRemove = new ArrayList<>(); for (Bullet bullet : bullets) { for (Enemy enemy : enemies) { if (bullet.x >= enemy.x && bullet.x <= enemy.x + 30 && bullet.y >= enemy.y && bullet.y <= enemy.y + 30) { bulletsToRemove.add(bullet); enemiesToRemove.add(enemy); } } } bullets.removeAll(bulletsToRemove); enemies.removeAll(enemiesToRemove); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.fillRect(plane.x, plane.y, 30, 30); for (Bullet bullet : bullets) { g.setColor(Color.RED); g.fillRect(bullet.x, bullet.y, 5, 10); } for (Enemy enemy : enemies) { g.setColor(Color.GREEN); g.fillRect(enemy.x, enemy.y, 30, 30); } checkCollision(); } public static void main(String[] args) { PlaneGame game = new PlaneGame(); game.setSize(500, 500); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setVisible(true); while (true) { game.move(); game.generateEnemy(); game.repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }

这个代码仅仅是一个简单的飞机大战游戏示例,缺乏很多功能和优化。你可能需要根据你的需求来添加更多的功能,比如得分系统、更复杂的敌人行为、更多的关卡等。此外,这个代码使用了简单的while (true)循环来模拟游戏循环,实际上在实际项目中应该使用更合适的游戏引擎。

java
import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; class Bullet { int x, y; public Bullet(int x, int y) { this.x = x; this.y = y; } } class Enemy { int x, y; public Enemy(int x, int y) { this.x = x; this.y = y; } } class Plane { int x, y; public Plane(int x, int y) { this.x = x; this.y = y; } } public class PlaneGame extends JFrame { private List<Bullet> bullets; private List<Enemy> enemies; private Plane plane; private int score; public PlaneGame() { bullets = new ArrayList<>(); enemies = new ArrayList<>(); plane = new Plane(250, 250); score = 0; addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { bullets.add(new Bullet(plane.x + 12, plane.y)); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { plane.x -= 5; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { plane.x += 5; } } @Override public void keyReleased(KeyEvent e) {} }); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void move() { Iterator<Bullet> bulletIterator = bullets.iterator(); while (bulletIterator.hasNext()) { Bullet bullet = bulletIterator.next(); bullet.y -= 5; if (bullet.y < 0) { bulletIterator.remove(); } } Iterator<Enemy> enemyIterator = enemies.iterator(); while (enemyIterator.hasNext()) { Enemy enemy = enemyIterator.next(); enemy.y += 2; if (enemy.y > 500) { enemyIterator.remove(); score++; } } } public void generateEnemy() { Random random = new Random(); int x = random.nextInt(500); int y = 0; enemies.add(new Enemy(x, y)); } public void checkCollision() { Iterator<Bullet> bulletIterator = bullets.iterator(); while (bulletIterator.hasNext()) { Bullet bullet = bulletIterator.next(); Iterator<Enemy> enemyIterator = enemies.iterator(); while (enemyIterator.hasNext()) { Enemy enemy = enemyIterator.next(); if (bullet.x >= enemy.x && bullet.x <= enemy.x + 30 && bullet.y >= enemy.y && bullet.y <= enemy.y + 30) { bulletIterator.remove(); enemyIterator.remove(); score += 10; } } } } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.fillRect(plane.x, plane.y, 30, 30); for (Bullet bullet : bullets) { g.setColor(Color.RED); g.fillRect(bullet.x, bullet.y, 5, 10); } for (Enemy enemy : enemies) { g.setColor(Color.GREEN); g.fillRect(enemy.x, enemy.y, 30, 30); } g.setColor(Color.BLACK); g.drawString("Score: " + score, 10, 20); checkCollision(); } public static void main(String[] args) { PlaneGame game = new PlaneGame(); game.setSize(500, 500); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setVisible(true); while (true) { game.move(); game.generateEnemy(); game.repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }

这个版本的代码增加了飞机的左右移动功能,以及分数计算。这依然是一个简单的示例,可能需要进一步改进和扩展以满足你的实际需求。

标签