赞
踩
为您提供一个详细并且简单的坦克大战游戏实现的Java代码:
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
-
- public class TankGame extends JFrame {
- public static void main(String[] args) {
- new TankGame().launchFrame();
- }
-
- private void launchFrame() {
- this.setTitle("坦克大战");
- this.setSize(700, 600);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
-
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
-
- this.add(new GamePanel());
-
- this.setVisible(true);
- }
- }
-
- class GamePanel extends JPanel implements Runnable {
- private static final long serialVersionUID = 1L;
-
- private Tank tank;
- private Bullet bullet;
- private EnemyTank enemyTank;
- private boolean bGameOver = false;
- private boolean bPause = false;
-
- private Image offScreenImage = null;
-
- public GamePanel() {
- this.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- int code = e.getKeyCode();
-
- if (code == KeyEvent.VK_Q) {
- System.exit(0);
- }
-
- if (!bGameOver && !bPause) {
- switch (code) {
- case KeyEvent.VK_UP:
- tank.move(Dir.UP);
- break;
- case KeyEvent.VK_DOWN:
- tank.move(Dir.DOWN);
- break;
- case KeyEvent.VK_LEFT:
- tank.move(Dir.LEFT);
- break;
- case KeyEvent.VK_RIGHT:
- tank.move(Dir.RIGHT);
- break;
- case KeyEvent.VK_SPACE:
- bullet = tank.fire();
- break;
- }
- }
- }
-
- public void keyReleased(KeyEvent e) {
- int code = e.getKeyCode();
-
- if (!bGameOver && !bPause) {
- switch (code) {
- case KeyEvent.VK_UP:
- case KeyEvent.VK_DOWN:
- case KeyEvent.VK_LEFT:
- case KeyEvent.VK_RIGHT:
- tank.stop();
- break;
- }
- }
- }
- });
-
- this.tank = new Tank(150, 20, Dir.STOP, this);
- this.enemyTank = new EnemyTank(550, 500, Dir.UP, this);
- Thread t = new Thread(this);
- t.start();
- }
-
- public void paint(Graphics g) {
- g.setColor(Color.GREEN);
- g.fillRect(0, 0, 700, 600);
- if (bGameOver) {
- g.setColor(Color.RED);
- g.setFont(new Font("宋体", Font.BOLD, 50));
- g.drawString("游戏结束!", 200, 300);
- return;
- }
-
- if (bPause) {
- g.setColor(Color.RED);
- g.setFont(new Font("宋体", Font.BOLD, 50));
- g.drawString("游戏暂停", 200, 300);
- return;
- }
-
- if (tank.isLive()) {
- tank.draw(g);
- }
-
- if (enemyTank.isLive()) {
- enemyTank.draw(g);
- }
-
- if (bullet != null) {
- bullet.draw(g);
- // 碰撞检测
- if (enemyTank.isLive() && bullet.hit(enemyTank)) {
- bullet.setLive(false);
- enemyTank.setLive(false);
- bGameOver = true;
- }
- }
- }
-
- public void update(Graphics g) {
- if (offScreenImage == null) {
- offScreenImage = this.createImage(700, 600);
- }
-
- Graphics gOffScreen = offScreenImage.getGraphics();
- Color c = gOffScreen.getColor();
- gOffScreen.setColor(Color.GREEN);
- gOffScreen.fillRect(0, 0, 700, 600);
- gOffScreen.setColor(c);
- paint(gOffScreen);
- g.drawImage(offScreenImage, 0, 0, null);
- }
-
- public void run() {
- while (!bGameOver) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- if (!bPause && enemyTank.isLive()) {
- enemyTank.run();
- }
-
- if (bullet != null && bullet.isLive()) {
- bullet.run();
- }
- this.repaint();
- }
- }
-
- // 暂停游戏
- public void pauseGame() {
- bPause = true;
- }
-
- // 继续游戏
- public void continueGame() {
- bPause = false;
- }
-
- // 重新开始游戏
- public void restartGame() {
- this.tank = new Tank(150, 20, Dir.STOP, this);
- this.enemyTank = new EnemyTank(550, 500, Dir.UP, this);
- this.bullet = null;
- this.bGameOver = false;
- }
- }
-
- enum Dir {
- UP, DOWN, LEFT, RIGHT, STOP
- }
-
- class Tank {
- private int x, y;
- private Dir dir;
- private static final int SPEED = 10;
- private boolean live = true;
-
- private GamePanel gamePanel;
-
- public Tank(int x, int y, Dir dir, GamePanel gamePanel) {
- this.x = x;
- this.y = y;
- this.dir = dir;
- this.gamePanel = gamePanel;
- }
-
- public boolean isLive() {
- return live;
- }
-
- public void setLive(boolean live) {
- this.live = live;
- }
-
- public void draw(Graphics g) {
- if (!live) return;
-
- switch (dir) {
- case UP:
- g.drawImage(Tools.getImage("res/tankU.png"), x, y, null);
- break;
- case DOWN:
- g.drawImage(Tools.getImage("res/tankD.png"), x, y, null);
- break;
- case LEFT:
- g.drawImage(Tools.getImage("res/tankL.png"), x, y, null);
- break;
- case RIGHT:
- g.drawImage(Tools.getImage("res/tankR.png"), x, y, null);
- break;
- }
- }
-
- public void move(Dir dir) {
- this.dir = dir;
- switch (dir) {
- case UP:
- y -= SPEED;
- break;
- case DOWN:
- y += SPEED;
- break;
- case LEFT:
- x -= SPEED;
- break;
- case RIGHT:
- x += SPEED;
- break;
- }
-
- // 边界检测
- if (x < 0) x = 0;
- if (y < 40) y = 40; // 避免将游戏菜单挡住,留一定区域给游戏菜单
- if (x > 630) x = 630;
- if (y > 530) y = 530;
- }
-
- public Bullet fire() {
- int bulletX = x + Tools.getImage("res/tankU.png").getWidth(null) / 2 - Tools.getImage("res/bulletU.gif").getWidth(null) / 2;
- int bulletY = y + Tools.getImage("res/tankU.png").getHeight(null) / 2 - Tools.getImage("res/bulletU.gif").getHeight(null) / 2;
- return new Bullet(bulletX, bulletY, dir, gamePanel);
- }
-
- public void stop() {
- this.dir = Dir.STOP;
- }
-
- // 坦克的碰撞检测
- public Rectangle getRectangle() {
- return new Rectangle(x, y, Tools.getImage("res/tankU.png").getWidth(null), Tools.getImage("res/tankU.png").getHeight(null));
- }
- }
-
- class EnemyTank extends Tank {
- private static final long serialVersionUID = 1L;
- private int step = 0;
-
- public EnemyTank(int x, int y, Dir dir, GamePanel gamePanel) {
- super(x, y, dir, gamePanel);
- }
-
- @Override
- public void run() {
- // 敌军坦克自动开火
- if (Math.random() > 0.9) {
- this.fire();
- }
-
- // 坦克随机转向和移动,每个方向停留10次才能真正执行移动操作
- if (step == 10) {
- step = 0; // 该步骤结束
- int r = (int) (Math.random() * 4); // 随机生成0到3的整数
- switch (r) {
- case 0:
- this.setDir(Dir.UP);
- break;
- case 1:
- this.setDir(Dir.RIGHT);
- break;
- case 2:
- this.setDir(Dir.DOWN);
- break;
- case 3:
- this.setDir(Dir.LEFT);
- break;
- }
- }
-
- step++;
- move(getDir());
- }
- }
-
- class Bullet {
- private int x, y;
- private Dir dir;
- private static final int SPEED = 20;
- private boolean live = true;
-
- private GamePanel gamePanel;
-
- public Bullet(int x, int y, Dir dir, GamePanel gamePanel) {
- this.x = x;
- this.y = y;
- this.dir = dir;
- this.gamePanel = gamePanel;
- }
-
- public boolean isLive() {
- return live;
- }
-
- public void setLive(boolean live) {
- this.live = live;
- }
-
- public void draw(Graphics g) {
- if (!live) return;
- switch (dir) {
- case UP:
- g.drawImage(Tools.getImage("res/bulletU.gif"), x, y, null);
- break;
- case DOWN:
- g.drawImage(Tools.getImage("res/bulletD.gif"), x, y, null);
- break;
- case LEFT:
- g.drawImage(Tools.getImage("res/bulletL.gif"), x, y, null);
- break;
- case RIGHT:
- g.drawImage(Tools.getImage("res/bulletR.gif"), x, y, null);
- break;
- }
- }
-
- public void run() {
- switch (dir) {
- case UP:
- y -= SPEED;
- break;
- case DOWN:
- y += SPEED;
- break;
- case LEFT:
- x -= SPEED;
- break;
- case RIGHT:
- x += SPEED;
- break;
- }
-
- // 边界检测
- if (x < 0 || y < 0 || x > 700 || y > 550) {
- live = false;
- return;
- }
-
- // 子弹与坦克的碰撞检测
- if (gamePanel.tank.isLive() && hit(gamePanel.tank)) {
- live = false;
- gamePanel.tank.setLive(false);
- gamePanel.bGameOver = true;
- }
- }
-
- // 子弹的碰撞检测
- public boolean hit(Tank tank) {
- if (this.isLive() && tank.isLive() && tank.getRectangle().intersects(new Rectangle(x, y, Tools.getImage("res/bulletU.gif").getWidth(null), Tools.getImage("res/bulletU.gif").getHeight(null)))) {
- return true;
- }
- return false;
- }
- }
-
- class Tools {
- public static Image getImage(String path) {
- return new ImageIcon(path).getImage();
- }
- }

以上是一个简单的坦克大战游戏Java实现代码,其中涉及到一些基本的Java语法和图形化界面的实现,您可以自行修改或者扩展它,实现您想要的更多功能。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。