用Java语言写一个坦克大战小游戏
时间: 2023-06-18 15:07:21 浏览: 133
java 写的坦克大战游戏
好的,这里提供一个简单的坦克大战小游戏的代码实现。这个游戏是一个单人游戏,玩家操纵一个坦克进行战斗,目标是消灭所有敌方坦克。
代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class TankGame extends JPanel implements ActionListener {
private final int B_WIDTH = 600;
private final int B_HEIGHT = 600;
private Timer timer;
private Tank tank;
private ArrayList<EnemyTank> enemyTanks;
private boolean inGame;
public TankGame() {
initGame();
}
private void initGame() {
setBackground(Color.WHITE);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
setFocusable(true);
tank = new Tank(B_WIDTH / 2, B_HEIGHT - 50);
enemyTanks = new ArrayList<>();
enemyTanks.add(new EnemyTank(50, 50));
enemyTanks.add(new EnemyTank(B_WIDTH - 100, 50));
enemyTanks.add(new EnemyTank(B_WIDTH / 2, 50));
inGame = true;
timer = new Timer(10, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
drawObjects(g);
} else {
drawGameOver(g);
}
Toolkit.getDefaultToolkit().sync();
}
private void drawObjects(Graphics g) {
tank.draw(g);
for (EnemyTank enemyTank : enemyTanks) {
enemyTank.draw(g);
}
}
private void drawGameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 32);
FontMetrics fm = getFontMetrics(small);
g.setColor(Color.BLACK);
g.setFont(small);
g.drawString(msg, (B_WIDTH - fm.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
updateTank();
updateEnemies();
checkCollisions();
repaint();
}
private void updateTank() {
tank.move();
}
private void updateEnemies() {
for (EnemyTank enemyTank : enemyTanks) {
enemyTank.move();
}
}
private void checkCollisions() {
Rectangle tankRect = tank.getBounds();
for (EnemyTank enemyTank : enemyTanks) {
Rectangle enemyRect = enemyTank.getBounds();
if (tankRect.intersects(enemyRect)) {
inGame = false;
}
}
}
private class Tank {
private int x, y;
private int dx, dy;
private final int SPEED = 2;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x, y, 30, 30);
}
public void move() {
x += dx;
y += dy;
if (x < 0) {
x = 0;
}
if (x > B_WIDTH - 30) {
x = B_WIDTH - 30;
}
if (y < 0) {
y = 0;
}
if (y > B_HEIGHT - 30) {
y = B_HEIGHT - 30;
}
}
public void setDx(int dx) {
this.dx = dx * SPEED;
}
public void setDy(int dy) {
this.dy = dy * SPEED;
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 30);
}
}
private class EnemyTank {
private int x, y;
private int dx, dy;
private final int SPEED = 1;
public EnemyTank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x, y, 30, 30);
}
public void move() {
int direction = (int) (Math.random() * 4);
switch (direction) {
case 0:
dx = SPEED;
dy = 0;
break;
case 1:
dx = -SPEED;
dy = 0;
break;
case 2:
dx = 0;
dy = SPEED;
break;
case 3:
dx = 0;
dy = -SPEED;
break;
}
x += dx;
y += dy;
if (x < 0) {
x = 0;
}
if (x > B_WIDTH - 30) {
x = B_WIDTH - 30;
}
if (y < 0) {
y = 0;
}
if (y > B_HEIGHT - 30) {
y = B_HEIGHT - 30;
}
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 30);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tank Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new TankGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个代码实现中,Tank类表示玩家的坦克,EnemyTank类表示敌方坦克。玩家可以使用键盘控制坦克的移动,敌方坦克会随机移动。玩家和敌方坦克碰撞时游戏结束。在游戏中,通过调用Timer对象的start()方法启动定时器,在固定的时间间隔内执行actionPerformed()方法中的代码更新游戏状态和重绘游戏界面。最后,使用Swing框架中的JFrame和JPanel实现游戏窗口和游戏画布。
阅读全文