如何用Java写一个飞机大战的小游戏
时间: 2024-01-01 22:04:12 浏览: 78
飞机大战是一种基于2D图形和键盘事件的小游戏,下面是一个使用Java编写的简单飞机大战游戏的代码示例:
1. 创建游戏窗口
```java
import javax.swing.JFrame;
public class GameFrame extends JFrame {
public GameFrame() {
setTitle("飞机大战");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new GameFrame();
}
}
```
2. 创建游戏画布并绘制背景
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private Image bgImage;
public GamePanel() {
bgImage = Toolkit.getDefaultToolkit().getImage("bg.png");
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, getWidth(), getHeight(), null);
g.setColor(Color.WHITE);
g.drawString("Hello, world!", getWidth() / 2 - 50, getHeight() / 2);
}
}
```
3. 添加键盘监听器
```java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class GameFrame extends JFrame implements KeyListener {
private GamePanel gamePanel;
public GameFrame() {
setTitle("飞机大战");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
gamePanel = new GamePanel();
add(gamePanel);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
System.out.println("向左移动");
break;
case KeyEvent.VK_RIGHT:
System.out.println("向右移动");
break;
case KeyEvent.VK_SPACE:
System.out.println("发射子弹");
break;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
```
4. 添加飞机和子弹
```java
import java.awt.Color;
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.JPanel;
public class GamePanel extends JPanel implements KeyListener {
private Image bgImage;
private Plane plane;
private List<Bullet> bullets;
public GamePanel() {
bgImage = Toolkit.getDefaultToolkit().getImage("bg.png");
plane = new Plane(getWidth() / 2, getHeight() - 100);
bullets = new ArrayList<>();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, getWidth(), getHeight(), null);
plane.draw(g);
for (Bullet bullet : bullets) {
bullet.draw(g);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
plane.moveLeft();
break;
case KeyEvent.VK_RIGHT:
plane.moveRight(getWidth());
break;
case KeyEvent.VK_SPACE:
bullets.add(new Bullet(plane.getX(), plane.getY()));
break;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
private static class Plane {
private int x, y;
private final int speed = 5;
public Plane(int x, int y) {
this.x = x;
this.y = y;
}
public void moveLeft() {
x -= speed;
}
public void moveRight(int maxWidth) {
x += speed;
if (x > maxWidth) {
x = maxWidth;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.drawRect(x - 25, y - 25, 50, 50);
}
}
private static class Bullet {
private int x, y;
private final int speed = 10;
public Bullet(int x, int y) {
this.x = x;
this.y = y;
}
public void move() {
y -= speed;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(x - 5, y - 5, 10, 10);
}
}
}
```
这只是一个简单的示例代码,您可以根据需求对代码进行修改和扩展。
阅读全文