java实现坦克大战游戏

 更新时间:2020年09月24日 08:17:57   作者:sinJack  
这篇文章主要为大家详细介绍了java实现坦克大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现坦克大战游戏的具体代码,供大家参考,具体内容如下

一、实现的功能

1、游戏玩法介绍
2、自定义游戏(选择游戏难度、关卡等)
3、自定义玩家姓名
4、数据的动态显示

二、程序基本结构

三、主要的界面

1)游戏首页

2)自定义游戏页面

3)游戏介绍

4)开始游戏,自定义玩家姓名

5)游戏主页面

四、主要代码

1)数据的初始化类

public class Data {
 public static boolean isRestart=false;
 public static boolean isGameOver=false;
 public static int start = 0;// 1为开始 0为还未开始
 public static int levle = 1;//关卡
 public static int pouse = 0;//0表示继续 1表示暂停
 public static String name = "";
 public static int time = 0;
 public static int b = 0;
 public static int a = 1;//玩家的方向 1为上 2为下 3为左 4为右
 public static int count; // 计算积分数
 public static int hit = 0; // 计算击毁坦克的数量
 public static Player player = new Player(200, 560, 3, 1);// 创建 玩家
 public static home home = new home(280, 560, 1);// 创建碉堡
 // 加载图片 相对路径 jpg png gif
 public static final Image IMG_ST = new ImageIcon("image/timg (2).jpg").getImage();
 public static final Image IMG_buleUp = new ImageIcon("image/buleUp.gif").getImage();
 public static final Image IMG_buledown = new ImageIcon("image/buledown.gif").getImage();
 public static final Image IMG_buleLeft = new ImageIcon("image/buleLeft.gif").getImage();
 public static final Image IMG_buleRight = new ImageIcon("image/buleRight.gif").getImage();
 public static final Image IMG_pinkDown = new ImageIcon("image/pinkDown.gif").getImage();
 public static final Image IMG_pinkLeft = new ImageIcon("image/pinkLeft.gif").getImage();
 public static final Image IMG_pinkRight = new ImageIcon("image/pinkRight.gif").getImage();
 public static final Image IMG_pinkUp = new ImageIcon("image/pinkUp.gif").getImage();
 public static final Image IMG_yellowDown = new ImageIcon("image/yellowDown.gif").getImage();
 public static final Image IMG_yellowLeft = new ImageIcon("image/yellowLeft.gif").getImage();
 public static final Image IMG_yellowRight = new ImageIcon("image/yellowRight.gif").getImage();
 public static final Image IMG_yellowUp = new ImageIcon("image/yellowUp.gif").getImage();
 public static final Image IMG_tie = new ImageIcon("image/tie4.gif").getImage();
 public static final Image IMG_warter = new ImageIcon("image/water.gif").getImage();
 public static final Image IMG_grass = new ImageIcon("image/grass.gif").getImage();
 public static final Image IMG_wall = new ImageIcon("image/walls.gif").getImage();
 public static final Image IMG_smallwall = new ImageIcon("image/smallwalls.gif").getImage();
 public static final Image IMG_PLAYER1 = new ImageIcon("image/player1.gif").getImage();
 public static final Image IMG_PLAYER2 = new ImageIcon("image/player2.gif").getImage();
 public static final Image IMG_PLAYER3 = new ImageIcon("image/player3.gif").getImage();
 public static final Image IMG_PLAYER4 = new ImageIcon("image/player4.gif").getImage();
 public static final Image IMG_home = new ImageIcon("image/symbol.gif").getImage();
 public static final Image IMG_bullet = new ImageIcon("image/bullet.gif").getImage();
 public static final Image IMG_over = new ImageIcon("image/gameOver.gif").getImage();
 public static final Image IMG_win = new ImageIcon("image/gameWin.jpeg").getImage();
 public static final Image IMG_hp = new ImageIcon("image/hp.png").getImage();
 public static final Image IMG_speed = new ImageIcon("image/124.png").getImage();
 public static final Image IMG_bomb = new ImageIcon("image/128.png").getImage();
 public static final Image IMG_gia1 = new ImageIcon("image/i1.jpg").getImage();
 public static final Image IMG_gia2 = new ImageIcon("image/i2.jpg").getImage();
 public static final Image IMG_gia3 = new ImageIcon("image/i3.jpg").getImage();
 // 子弹的集合
 public static ArrayList<Bullet> zdList = new ArrayList<Bullet>();
 public static ArrayList<EnBullet> enzdList = new ArrayList<EnBullet>();
 // 界面敌人的集合
 public static ArrayList<Enemy> enemyList = new ArrayList<Enemy>();
 //后台敌人的集合
 public static ArrayList<Enemy> backlist = new ArrayList<Enemy>();
 // 计算各个敌人的数量集合
 public static ArrayList<Enemy> yellowcount = new ArrayList<Enemy>();
 public static ArrayList<Enemy> bulecount = new ArrayList<Enemy>();
 public static ArrayList<Enemy> pinkcount = new ArrayList<Enemy>();
 //道具的集合
 public static ArrayList<stage> stagelList = new ArrayList<stage>();


 public static void initenemy(int num) {
 Random ran = new Random();
 for (int i = 0; i < num; i++) {
 int key = ran.nextInt(3);
 Enemy en = null;
 if (key == 0) {
 en = new YellowEnemy(0, 0, 1, 0, 0,100);
 yellowcount.add(en);
 } else if (key == 1) {
 en = new BuleEnemy(560, 0, 1, 0, 0,100);
 bulecount.add(en);
 } else {
 en = new PinkEnemy(250, 0, 1, 0, 0,100);
 pinkcount.add(en);
 }
 backlist.add(en);
 }
 }

}

2)开始游戏

public class StartAction implements ActionListener {
 private GameFrame f;
 private GameArea ga;

 public StartAction(GameFrame f) {
 super();
 this.f = f;
 }

 public void actionPerformed(ActionEvent e) {
 String startaction = e.getActionCommand();
 if (startaction.equals("start")) {

 String title = JOptionPane.showInputDialog(null, "请输入姓名", "欢迎来到坦克大战", JOptionPane.PLAIN_MESSAGE);
 Data.name = title;
 Data.start = 1;
 Data.initenemy(3);
 f.getGm().getStartgame().setEnabled(false);
 f.getGm().getRestart().setEnabled(true);
 } else if (startaction.equals("restart")) {
 Data.enemyList.clear();
 Data.backlist.clear();
 Data.yellowcount.clear();
 Data.bulecount.clear();
 Data.pinkcount.clear();
 Data.stagelList.clear();
 Data.player.setX(200);
 Data.player.setY(560);
 Data.initenemy(3);

 Data.b = 0;
 Player.setHP(3);
 Data.isGameOver = true;
 Data.isRestart =true;


 } else if (startaction.equals("exit")) {
 System.exit(0);
 } else if (startaction.equals("game")) {
 JOptionPane.showMessageDialog(null,"用 W S A D控制方向,J键盘发射,P为暂停,O为继续。\n道具: 炸弹----使敌人消失 星星-----加速 爱心----增加生命值!\n制作不易请多多包含!!!",
 "提示!", JOptionPane.INFORMATION_MESSAGE);

 } else if (startaction.equals("self")) {
 new GameSelf();
 }
 }

}

3)监听按键类

public class PlayerKeyListener extends KeyAdapter {
 private Player player;

 @Override
 public void keyPressed(KeyEvent e) {
 super.keyPressed(e);
 int key = e.getKeyCode();
 if (key == KeyEvent.VK_W) {
 if (Data.pouse == 0 && Data.player.getY() > 0) {
 Data.player.up();
 Data.a = 1;
 }
 }
 if (key == KeyEvent.VK_S) {
 if (Data.pouse == 0 && Data.player.getY() < 560) {
 Data.player.down();
 Data.a = 2;
 }
 }
 if (key == KeyEvent.VK_A) {
 if (Data.pouse == 0 && Data.player.getX() > 0) {
 Data.player.left();
 Data.a = 3;
 }
 }
 if (key == KeyEvent.VK_D) {
 if (Data.pouse == 0 && Data.player.getX() < 560) {
 Data.player.right();
 Data.a = 4;
 }
 }
 if (key == KeyEvent.VK_P) {
 Data.pouse = 1;
 }
 if (key == KeyEvent.VK_O) {
 Data.pouse = 0;
 }
 }

 @Override
 public void keyReleased(KeyEvent e) {
 super.keyReleased(e);
 int key = e.getKeyCode();
 if (key == KeyEvent.VK_J) {
 if (Data.pouse == 0 && Data.a == 1) {
 int x = Data.player.getX() + 16;
 int y = Data.player.getY() - 5;
 Bullet bullet = new Bullet(x, y, Data.a);
 Data.zdList.add(bullet);
 return;
 }
 if (Data.pouse == 0 && Data.a == 2) {
 int x = Data.player.getX() + 16;
 int y = Data.player.getY() + 32;
 Bullet bullet = new Bullet(x, y, Data.a);
 Data.zdList.add(bullet);
 return;
 }
 if (Data.pouse == 0 && Data.a == 3) {
 int x = Data.player.getX() - 10;
 int y = Data.player.getY() + 15;
 Bullet bullet = new Bullet(x, y, Data.a);
 Data.zdList.add(bullet);
 return;
 }
 if (Data.pouse == 0 && Data.a == 4) {
 int x = Data.player.getX() + 38;
 int y = Data.player.getY() + 15;
 Bullet bullet = new Bullet(x, y, Data.a);
 Data.zdList.add(bullet);
 return;
 }
 }
 }
}

源码下载:坦克游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决Mapper接口和mapper.xml的文件位置问题

    解决Mapper接口和mapper.xml的文件位置问题

    这篇文章主要介绍了解决Mapper接口和mapper.xml的文件位置问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • 关于java编译过程中的bug说明

    关于java编译过程中的bug说明

    本篇文章是对java编译过程中的bug进行了详细的说明介绍,需要的朋友参考下
    2013-05-05
  • IDEA 阿里JAVA规范插件的具体使用

    IDEA 阿里JAVA规范插件的具体使用

    这篇文章主要介绍了IDEA 阿里JAVA规范插件的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Mybatis中的mapper是如何和XMl关联起来的

    Mybatis中的mapper是如何和XMl关联起来的

    这篇文章主要介绍了Mybatis中的mapper是如何和XMl关联起来的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Java集合中的LinkedHashSet源码解读

    Java集合中的LinkedHashSet源码解读

    这篇文章主要介绍了Java集合中的LinkedHashSet源码解读,在LinkedHashMap中,双向链表的遍历顺序通过构造方法指定,如果没有指定,则使用默认顺序为插入顺序,即accessOrder=false,需要的朋友可以参考下
    2023-12-12
  • SpringBoot项目实现关闭数据库配置和springSecurity

    SpringBoot项目实现关闭数据库配置和springSecurity

    这篇文章主要介绍了SpringBoot项目实现关闭数据库配置和springSecurity的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • java虚拟机JVM类加载机制原理(面试必问)

    java虚拟机JVM类加载机制原理(面试必问)

    这篇文章主要介绍了面试当中必会问到的java虚拟机JVM类加载机制,非常的详细,有需要的朋友可以借鉴参考下,欢迎多多交流讨论
    2021-08-08
  • Mybatis查询Sql结果未映射到对应得实体类上的问题解决

    Mybatis查询Sql结果未映射到对应得实体类上的问题解决

    使用mybatis查询表数据得时候,发现对应得实体类字段好多都是null,本文主要介绍了Mybatis查询Sql结果未映射到对应得实体类上的问题解决,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • Java以命令模式设计模式

    Java以命令模式设计模式

    这篇文章主要详细的介绍Java以命令的模式设计模式,是用场景、优缺点等都作有详细介绍,需要的朋友请具体参考下面文章内容
    2021-09-09
  • C# 中Excel导入时判断是否被占用三种方法

    C# 中Excel导入时判断是否被占用三种方法

    这篇文章主要介绍了C# 中Excel导入时 判断是否被占用三种方法的相关资料,需要的朋友可以参考下
    2017-04-04

最新评论