用java写一个太空大战游戏
时间: 2023-12-03 17:04:07 浏览: 155
太空大战Java游戏
好的,我可以为你提供一个简单的太空大战游戏的Java代码,但需要注意的是,这只是一个简单的实现版本,如果你想要制作更加完整的游戏,需要进行更多的开发和优化。
首先,我们需要创建一个空的Java项目,然后创建以下类:
1. Game类:这是整个游戏的主类,包含游戏的主循环和其他必要的方法。
2. Player类:这个类表示玩家的飞船,包含位置、速度和其他属性。
3. Enemy类:这个类表示敌人的飞船,也包含位置、速度和其他属性。
4. Bullet类:这个类表示子弹,包含位置、速度和其他属性。
接下来是代码实现:
Game类:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
public class Game extends JPanel implements KeyListener, Runnable {
private Player player;
private ArrayList<Enemy> enemies;
private ArrayList<Bullet> bullets;
private Random random;
private int score;
private boolean gameOver;
public Game() {
setPreferredSize(new Dimension(800, 600));
addKeyListener(this);
setFocusable(true);
player = new Player(400, 500);
enemies = new ArrayList<>();
bullets = new ArrayList<>();
random = new Random();
score = 0;
gameOver = false;
new Thread(this).start();
}
public void paint(Graphics g) {
super.paint(g);
if (!gameOver) {
// 绘制玩家飞船
g.setColor(Color.BLUE);
g.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());
// 绘制敌人飞船
g.setColor(Color.RED);
for (Enemy enemy : enemies) {
g.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
}
// 绘制子弹
g.setColor(Color.BLACK);
for (Bullet bullet : bullets) {
g.fillRect(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
}
// 绘制分数
g.drawString("Score: " + score, 10, 20);
} else {
// 如果游戏结束,显示游戏结束的信息
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 48));
g.drawString("Game Over!", 250, 300);
g.setFont(new Font("Arial", Font.PLAIN, 24));
g.drawString("Score: " + score, 350, 350);
}
}
public void update() {
if (!gameOver) {
// 更新玩家飞船的位置
player.update();
// 更新敌人飞船的位置
for (Enemy enemy : enemies) {
enemy.update();
}
// 更新子弹的位置
for (Bullet bullet : bullets) {
bullet.update();
}
// 检测子弹是否击中敌人飞船
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
for (int j = 0; j < enemies.size(); j++) {
Enemy enemy = enemies.get(j);
if (bullet.intersects(enemy)) {
bullets.remove(i);
i--;
enemies.remove(j);
j--;
score += 10;
}
}
}
// 检测敌人飞船是否撞到玩家飞船
for (Enemy enemy : enemies) {
if (enemy.intersects(player)) {
gameOver = true;
break;
}
}
// 添加新的敌人飞船
if (random.nextInt(100) < 5) {
enemies.add(new Enemy(random.nextInt(800), 0));
}
// 删除移出边界的子弹和敌人飞船
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if (bullet.getY() < 0 || bullet.getY() > 600) {
bullets.remove(i);
i--;
}
}
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
if (enemy.getY() > 600) {
enemies.remove(i);
i--;
}
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
player.setVelocityX(-5);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
player.setVelocityX(5);
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bullets.add(new Bullet(player.getX() + player.getWidth() / 2 - 2, player.getY() - 10));
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) {
player.setVelocityX(0);
}
}
public void keyTyped(KeyEvent e) {}
public void run() {
while (true) {
update();
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Space War");
Game game = new Game();
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
Player类:
```java
public class Player {
private int x;
private int y;
private int width;
private int height;
private int velocityX;
public Player(int x, int y) {
this.x = x;
this.y = y;
width = 40;
height = 40;
velocityX = 0;
}
public void update() {
x += velocityX;
if (x < 0) {
x = 0;
}
if (x + width > 800) {
x = 800 - width;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setVelocityX(int velocityX) {
this.velocityX = velocityX;
}
}
```
Enemy类:
```java
public class Enemy {
private int x;
private int y;
private int width;
private int height;
private int velocityX;
private int velocityY;
public Enemy(int x, int y) {
this.x = x;
this.y = y;
width = 40;
height = 40;
velocityX = 0;
velocityY = 2;
}
public void update() {
x += velocityX;
y += velocityY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean intersects(Player player) {
return x < player.getX() + player.getWidth() &&
x + width > player.getX() &&
y < player.getY() + player.getHeight() &&
y + height > player.getY();
}
}
```
Bullet类:
```java
public class Bullet {
private int x;
private int y;
private int width;
private int height;
private int velocityX;
private int velocityY;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
width = 4;
height = 10;
velocityX = 0;
velocityY = -10;
}
public void update() {
x += velocityX;
y += velocityY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean intersects(Enemy enemy) {
return x < enemy.getX() + enemy.getWidth() &&
x + width > enemy.getX() &&
y < enemy.getY() + enemy.getHeight() &&
y + height > enemy.getY();
}
}
```
这就是一个简单的太空大战游戏的Java代码,希望能对你有所帮助!
阅读全文