Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 787|回复: 0

[Java游戏学习]支持双黑两人激战的坦克大战源码

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-16 15:07:21 | 显示全部楼层 |阅读模式
    写这个游戏已经有一段时间了,一直在实现各种新功能,从最开始的地图上只有坦克,发子弹还是一个大问题到现在可以两个人一起玩,还是花了不少心思的。

       现在坦克的速度更快,电脑坦克也不会撞墙.虽然游戏性没有经典坦克大战那么强,但是还是可以用来休闲娱乐一下,这个用了很多最近学到的新知识,模仿俄罗斯方块,还有一些小技巧,比如可以同时按触发多个按键事件,对子弹的处理等.

        左边的坦克用W D S A控制移动,H发射子弹,每次最多出现5颗子弹,右边的坦克用上下左右箭头控制移动,L键发射子弹,互不干扰.Q键可以直接退出游戏。

        游戏结束后按Y键可以继续游戏.为了结构清晰游戏分为Mywar Shoot Tanks 3个类.  
      
       
       

         
       

         
       
      

    /********************MyWar类***********************/
    1. import java.awt.Color;
    2. import java.awt.Font;
    3. import java.awt.Graphics;
    4. import java.awt.event.KeyAdapter;
    5. import java.awt.event.KeyEvent;
    6. import java.util.Arrays;
    7. import java.util.Random;
    8. import java.util.Timer;
    9. import java.util.TimerTask;
    10. import javax.swing.JFrame;
    11. import javax.swing.JPanel;
    12. public class MyWar{
    13.     public static void main(String[] args) {
    14.         JFrame frame = new JFrame("坦克大战");//新建一个窗口
    15.         War war = new War();//创建一个War类的对象
    16.         frame.add(war);//把war添加到窗口中
    17.         frame.setSize(750,530);//窗口宽高
    18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时结束进程
    19.         frame.setLocationRelativeTo(null);//使窗口居中
    20.         frame.setVisible(true);//这句是干啥的?
    21.         war.action();//启动(war)战斗,亲,可以游戏了!
    22.     }
    23. }
    24. class War extends JPanel{
    25.     private boolean sUp,sDown,sRight,sLeft,sH;//定义右边坦克按键的开关
    26.     private boolean sW,sD,sS,sA,sL;//定义左边坦克按键的开关
    27.     public static final int WIDTH=750;//定义地图宽度
    28.     public static final int HEIGHT=530;//定义地图高度
    29.     private int score;//设置分数
    30.     private boolean gameOver;//gameover=false表示游戏没有结束
    31.     private Timer timer;//刚学的东西
    32.     private int shootNum,shootNum1;//可以射击的子弹数,防止作弊
    33.     MyTank[] myTank=new MyTank[2];//定义一个我方坦克对象数组
    34.     EnemyTanks[] enemyTank=new EnemyTanks[5];//初始化敌人坦克的数量
    35.     EnemyTanks newEnemyTank;//用来产生一辆敌人的坦克,补充死去的T_T
    36.     Random r = new Random();
    37.     /**用于产生一辆新的敌人坦克,返回参数是EnemyTanks类型*/
    38.     public EnemyTanks nextTank(){
    39.         int x = r.nextInt(2)*WIDTH;//随机x的值,坦克出生在角落
    40.         int y=r.nextInt(2)*HEIGHT;//随机y的值,坦克出生在角落
    41.         int step=r.nextInt(4)+1;//速度
    42.         int direct=r.nextInt(4)+1;//方向
    43.         int who = r.nextInt(3)+1;//谁?
    44.         newEnemyTank=new EnemyTanks(x,y,step,direct,who,true);
    45.         return newEnemyTank;//返回一个新坦克
    46.     }
    47.     public void action(){
    48.         startGame();//开始游戏函数
    49.         KeyAdapter l = new KeyAdapter(){//键盘监听
    50.             public void keyPressed(KeyEvent e){
    51.                 int key = e.getKeyCode();
    52.                 if(key==KeyEvent.VK_Q){
    53.                     System.exit(0);//Q关闭进程结束游戏
    54.                 }
    55.                 if(gameOver){
    56.                     if(key==KeyEvent.VK_Y){
    57.                         startGame();//Y键开始游戏
    58.                     }
    59.                 return;
    60.                 }
    61.                 switch(key){
    62.                 case KeyEvent.VK_W:sW=true;break;
    63.                 case KeyEvent.VK_A:sA=true;break;
    64.                 case KeyEvent.VK_D:sD=true;break;
    65.                 case KeyEvent.VK_S:sS=true;break;
    66.                 case KeyEvent.VK_L:sL=true;break;
    67.                 case KeyEvent.VK_RIGHT:sRight=true;break;
    68.                 case KeyEvent.VK_LEFT:sLeft=true;break;
    69.                 case KeyEvent.VK_DOWN:sDown=true;break;
    70.                 case KeyEvent.VK_UP:sUp=true;break;
    71.                 case KeyEvent.VK_H:sH=true;break;
    72.                 }
    73.                 repaint();
    74.             }
    75.             public void keyReleased(KeyEvent e) {
    76.                 int key = e.getKeyCode();
    77.                 switch(key){
    78.                 case KeyEvent.VK_W:sW=false;break;
    79.                 case KeyEvent.VK_A:sA=false;break;
    80.                 case KeyEvent.VK_D:sD=false;break;
    81.                 case KeyEvent.VK_S:sS=false;break;
    82.                 case KeyEvent.VK_H:sH=false;break;
    83.                 case KeyEvent.VK_RIGHT:sRight=false;break;
    84.                 case KeyEvent.VK_LEFT:sLeft=false;break;
    85.                 case KeyEvent.VK_DOWN:sDown=false;break;
    86.                 case KeyEvent.VK_UP:sUp=false;break;
    87.                 case KeyEvent.VK_L:sL=false;break;
    88.                 }
    89.             }
    90.         };
    91.         this.requestFocus();
    92.         this.addKeyListener(l);
    93.                                                                  
    94.     }
    95.     public void level(){//每5分增加一辆敌人的坦克
    96.         int length = score/5+5;
    97.         if(length>enemyTank.length){
    98.             enemyTank=Arrays.copyOf(enemyTank,enemyTank.length+1);//坦克数组扩容
    99.             enemyTank[enemyTank.length-1]=nextTank();//初始化数组最后的一辆坦克
    100.             enemyTank[enemyTank.length-1].start();//启动这个坦克的线程
    101.         }
    102.     }
    103.     public void startGame(){
    104.         cleanShoot();//清空子弹
    105.         shootNum=5;//可以发射的子弹数为5
    106.         shootNum1=5;
    107.         score=0;
    108.         myTank[0]= new MyTank(550,370,4,1,-1,true);//初始化我的坦克
    109.         myTank[1]= new MyTank(100,100,4,1,0,true);
    110.         for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克
    111.             if(gameOver){//游戏结束,关闭每个坦克的线程
    112.                 enemyTank[i].s=false;
    113.             }
    114.         }
    115.         enemyTank=new EnemyTanks[5];
    116.         for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克
    117.             enemyTank[i]=nextTank();
    118.         }
    119.         gameOver=false;//游戏没有结束,表示开始
    120.         myTank[0].start();//启动我的坦克线程
    121.         myTank[1].start();
    122.         for (int i = 0; i < enemyTank.length; i++) {//启动敌人坦克线程
    123.             enemyTank[i].start();
    124.         }
    125.         timer = new Timer();//匿名内部类Timer
    126.         timer.schedule(new TimerTask(){
    127.             public void run(){//重写run()函数
    128.                 repaint();
    129.                 shootAndRun();
    130.             }
    131.         }, 0,50);//50毫秒执行一次
    132.     }
    133.     /**检查游戏是否结束*/
    134.     public void checkGameOver(){//模仿俄罗斯方块里面写的
    135.         if(myTank[0].isLive()||myTank[1].isLive()){
    136.             return;
    137.         }
    138.         gameOver = true;
    139.         timer.cancel();
    140.         repaint();
    141.     }
    142.     public void shootAndRun(){
    143.         if(sW){//如果开关sW=true就执行{}里面的代码
    144.             myTank[1].moveUp();
    145.         }else if(sD){
    146.             myTank[1].moveRight();
    147.         }else if(sS){
    148.             myTank[1].moveDown();
    149.         }else if(sA){
    150.             myTank[1].moveLeft();
    151.         }if(sH&&myTank[1].isLive()){//如果sL=true并且坦克是活的,就执行
    152.             if(shootNum1>=0){//如果允许的子弹数小于于0了,不执行
    153.                 myTank[1].shoot();
    154.                 shootNum1--;//控制子弹数量,射击一次,子弹减少,消失加1
    155.             }
    156.         }
    157.         if(sUp){
    158.             myTank[0].moveUp();
    159.         }else if(sRight){
    160.             myTank[0].moveRight();
    161.         }else if(sLeft){
    162.             myTank[0].moveLeft();
    163.         }else if(sDown){
    164.             myTank[0].moveDown();
    165.         }if(sL&&myTank[0].isLive()){
    166.             if(shootNum>=0){
    167.                 myTank[0].shoot();
    168.                 shootNum--;//控制子弹数量,射击一次,子弹减少,消失加1
    169.             }
    170.         }
    171.     }
    172.     public void paint(Graphics g){
    173.         g.setColor(Color.white);
    174.         g.fillRect(0, 0, this.getWidth(), this.getHeight());//画背景
    175.         paintWall(g);//画墙
    176.         //画我的坦克
    177.         paintTank(myTank[1].getX(),myTank[1].getY(),g,myTank[1].getDirect(),
    178.                   myTank[1].getWho(),myTank[1].isLive());
    179.         paintTank(myTank[0].getX(),myTank[0].getY(),g,myTank[0].getDirect(),myTank[0].getWho(),
    180.                   myTank[0].isLive());
    181.         for (int i = 0; i < enemyTank.length; i++) {//画敌人的坦克
    182.             paintTank(enemyTank[i].getX(),enemyTank[i].getY(),g,enemyTank[i].getDirect(),
    183.                         enemyTank[i].getWho(),enemyTank[i].isLive());
    184.         }
    185.         paintShoot(g);//画我射击的子弹
    186.         paintEemyShoot(g);//画敌人发出的子弹
    187.         paintScore(g);//画分数,及字符
    188.         //paintMap(g);
    189.     }
    190.     /* public void paintMap(Graphics g){//这是个方法可以生成漂亮的东西
    191.         for (int j = 0; j < 3; j++) {
    192.             Random color=new Random(); //通过Random生成随机颜色
    193.             int r = color.nextInt(256);
    194.             int g1 = color.nextInt(256);
    195.             int b = color.nextInt(256);
    196.             g.setColor(new Color(r, g1, b));
    197.             g.fillOval(color.nextInt(750), color.nextInt(530), 5, 5);
    198.         }
    199.                                                                  
    200.     }*/
    201.                                                             
    202.     public void paintScore(Graphics g){//画字符相关的
    203.         g.setColor(Color.lightGray);
    204.         Font f = getFont();
    205.         Font font = new Font(f.getName(),Font.BOLD,0x1e);
    206.         int x = 130;
    207.         int y = 275;
    208.         String str = "SCORE:"+this.score;
    209.         g.setFont(font);
    210.         g.drawString(str, x, y);
    211.         str="TANK:"+enemyTank.length ;
    212.         x+=170;
    213.         g.drawString(str, x, y);
    214.         x+=140;
    215.         str = "[Q]Quit!";
    216.         if(gameOver){
    217.             str = "[Y]Start!";
    218.         }
    219.         g.drawString(str, x, y);
    220.     }
    221.     public void paintWall(Graphics g){//画中间的柱子
    222.         g.setColor(Color.LIGHT_GRAY);
    223.         g.fill3DRect(WIDTH/2-45,150 , 40, HEIGHT-300, false);
    224.         g.fill3DRect(130,HEIGHT/2-20 , WIDTH-300, 40, false);
    225.     }
    226.     /**画自己坦克子弹,同时判断子弹有没有击中敌人*/
    227.     public void paintShoot(Graphics g){
    228.         ShootDispeal();
    229.         for (int i = 0; i < Shoot.myShoot.length; i+=4) {
    230.             if(Shoot.myShoot[i]==0&&Shoot.myShoot[i+1]==0){
    231.                 continue;
    232.             }
    233.             g.setColor(Color.RED);
    234.             g.fillOval(Shoot.myShoot[i], Shoot.myShoot[i+1], 10, 10);
    235.             int x = Shoot.myShoot[i];
    236.             int y = Shoot.myShoot[i+1];
    237.             for (int j = 0; j < enemyTank.length; j++) {
    238.                 int ex = enemyTank[j].getX();
    239.                 int ey = enemyTank[j].getY();
    240.                 if(x>ex&&x< ex+40&&y>ey&&y< ey+40){
    241.                     score+=1;
    242.                     level();
    243.                     enemyTank[j].s=false;//坦克死亡,线程关闭
    244.                     enemyTank[j]=nextTank();
    245.                     enemyTank[j].start();
    246.                     Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;//子弹消失
    247.                     Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
    248.                     shootNum++;
    249.                     shootNum1++;
    250.                 }
    251.             }
    252.             Shoot.myShoot[i]+=Shoot.myShoot[i+2];
    253.             Shoot.myShoot[i+1]+=Shoot.myShoot[i+3];
    254.         }
    255.     }
    256.     /**画敌人发出的子弹,同时判断是否击中了我的坦克*/
    257.     public void paintEemyShoot(Graphics g){
    258.         ShootDispeal();
    259.         for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {
    260.             if(Shoot.enemyShoot[i]==0&&Shoot.enemyShoot[i+1]==0){
    261.                 continue;
    262.             }
    263.             g.setColor(Color.blue);
    264.             g.fillOval(Shoot.enemyShoot[i], Shoot.enemyShoot[i+1], 10, 10);
    265.             int x = Shoot.enemyShoot[i];
    266.             int y = Shoot.enemyShoot[i+1];
    267.             int mx = myTank[0].getX();
    268.             int my = myTank[0].getY();
    269.             int mx1 = myTank[1].getX();
    270.             int my1 = myTank[1].getY();
    271.             if(x>mx&&x< mx+40&&y>my&&y< my+40){
    272.                 myTank[0].setLive(false);
    273.                 checkGameOver();
    274.             }
    275.             if(x>mx1&&x< mx1+40&&y>my1&&y< my1+40){
    276.                 myTank[1].setLive(false);
    277.                 checkGameOver();
    278.             }
    279.             Shoot.enemyShoot[i]+=Shoot.enemyShoot[i+2];//根据步伐,改变子弹的坐标
    280.             Shoot.enemyShoot[i+1]+=Shoot.enemyShoot[i+3];
    281.         }
    282.     }
    283.     /**画坦克*/
    284.     public void paintTank(int x,int y,Graphics g,int direct,int who,boolean isLive){
    285.         Color color = null;//设置颜色
    286.         if(isLive){
    287.             if(who==0){//我的坦克
    288.                 color=Color.green;
    289.             }else if(who==-1){
    290.                 color=Color.yellow;
    291.             }else if(who==1){//1,2,3敌人的坦克,3种颜色
    292.                 color=Color.red;  
    293.             }else if(who==2){
    294.                 color=Color.magenta;
    295.             }else if(who==3){
    296.                 color=Color.CYAN;
    297.             }  
    298.             switch(direct){//根据方向画出不同方向的坦克
    299.             case 1:g.setColor(color);paintUpTank(x,y,g);break;
    300.             case 2:g.setColor(color);paintRightTank(x,y,g);break;
    301.             case 3:g.setColor(color);paintDownTank(x,y,g);break;
    302.             case 4:g.setColor(color);paintLeftTank(x,y,g);break;
    303.             }
    304.         }
    305.     }
    306.     /**纯画图打造坦克*/
    307.     public void paintUpTank(int x,int y,Graphics g){
    308.         g.fill3DRect(x, y, 15, 50, false);
    309.         g.fill3DRect(x+35, y, 15, 50, false);
    310.         g.fill3DRect(x+15, y+10, 20, 30, false);
    311.         g.setColor(Color.black);
    312.         g.fill3DRect(x+23, y-10, 5, 33, false);
    313.         g.setColor(Color.yellow);
    314.         g.fillOval(x+20, y+18, 10, 10);
    315.     }
    316.     public void paintLeftTank(int x,int y,Graphics g){
    317.         g.fill3DRect(x, y, 50, 15, false);
    318.         g.fill3DRect(x, y+35, 50, 15, false);
    319.         g.fill3DRect(x+10, y+15, 30, 20, false);
    320.         g.setColor(Color.black);
    321.         g.fill3DRect(x-10, y+22, 33, 5, false);
    322.         g.setColor(Color.yellow);
    323.         g.fillOval(x+20, y+18, 10, 10);
    324.     }
    325.     public void paintDownTank(int x,int y,Graphics g){
    326.         g.fill3DRect(x, y, 15, 50, false);
    327.         g.fill3DRect(x+35, y, 15, 50, false);
    328.         g.fill3DRect(x+15, y+10, 20, 30, false);
    329.         g.setColor(Color.black);
    330.         g.fill3DRect(x+23, y+25, 5, 33, false);
    331.         g.setColor(Color.yellow);
    332.         g.fillOval(x+20, y+18, 10, 10);
    333.     }
    334.     public void paintRightTank(int x,int y,Graphics g){
    335.         g.fill3DRect(x, y, 50, 15, false);
    336.         g.fill3DRect(x, y+35, 50, 15, false);
    337.         g.fill3DRect(x+10, y+15, 30, 20, false);
    338.         g.setColor(Color.black);
    339.         g.fill3DRect(x+23, y+22, 33, 5, false);
    340.         g.setColor(Color.yellow);
    341.         g.fillOval(x+20, y+18, 10, 10);
    342.     }
    343.     /**重新开始游戏的时候会清空子弹数组里面的数据*/
    344.     public void cleanShoot(){
    345.         for (int i = 0; i < Shoot.enemyShoot.length; i++) {
    346.             Shoot.enemyShoot[i]=0;
    347.         }
    348.         for (int i = 0; i < Shoot.myShoot.length; i++) {
    349.             Shoot.myShoot[i]=0;
    350.         }
    351.     }
    352.     /**子弹消失了*/
    353.     public void ShootDispeal(){
    354.         for (int i = 0; i < Shoot.myShoot.length; i+=4) {//撞到边缘
    355.             if(Shoot.myShoot[i]< 0||Shoot.myShoot[i]>WIDTH||Shoot.myShoot[i+1]<
    356.                         0||Shoot.myShoot[i+1]>HEIGHT){
    357.                 Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;
    358.                 Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
    359.                 shootNum++;
    360.                 shootNum1++;
    361.             }
    362.             int x=Shoot.myShoot[i];
    363.             int y=Shoot.myShoot[i+1];
    364.             //撞到柱子
    365.             if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){
    366.                 Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0;
    367.                 Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0;
    368.                 shootNum++;
    369.                 shootNum1++;
    370.             }
    371.         }
    372.         for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {//撞到边缘
    373.             if(Shoot.enemyShoot[i]< 0||Shoot.enemyShoot[i]>WIDTH||Shoot.enemyShoot[i+1]<
    374.                         0||Shoot.enemyShoot[i+1]>HEIGHT){
    375.                 Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0;
    376.                 Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0;
    377.             }
    378.             int x=Shoot.enemyShoot[i];
    379.             int y=Shoot.enemyShoot[i+1];
    380.             //撞到柱子
    381.             if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){
    382.                 Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0;
    383.                 Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0;
    384.             }
    385.         }
    386.     }
    387. }
    复制代码
    /********************Tanks类***********************/
    1. import java.util.Random;
    2. /**坦克父类,继承了线程*/
    3. public class Tanks extends Thread{
    4.     private int x;//坦克坐标x
    5.     private int y;//坦克坐标y
    6.     private int step;//坦克的速度
    7.     private int direct;//方向,1表示向上,2表示向右,3表示向下,4表示向左
    8.     private int who;//坦克标识,0和-1表示自己的坦克,1,2,3表示敌人的坦克,颜色随机.
    9.     private boolean isLive = true;//判断坦克是否死亡
    10.     Shoot shoot;//shoot坦克的射击
    11.     public Tanks(int x, int y, int step, int direct,int who ,boolean isLive) {
    12.         super();
    13.         this.x = x;
    14.         this.y = y;
    15.         this.step = step+2;
    16.         this.direct = direct;
    17.         this.who = who;
    18.         this.isLive = isLive;
    19.     }
    20.     public boolean canMove(){//boolean返回值类型.判断坦克是否能移动,比如撞墙了.
    21.         if(x< 0){
    22.             x=0;
    23.             return false;
    24.         }else if(x>690){
    25.             x=690;
    26.             return false;
    27.         }else if(y< 0){
    28.             y=0;
    29.             return false;
    30.         }else if(y>450){
    31.             y=450;
    32.             return false;
    33.         }else if(x>270&&x<370&&y>100&&y<380){//撞到竖着的柱子
    34.             if(x-270< 20){
    35.                 x=270;
    36.             }else{
    37.                 x=370;
    38.             }
    39.             return false;
    40.         }else if(x>85&&x<570&&y>195&&y<290){//撞到横着的柱子
    41.             if(y-195< 10){
    42.                 y=190;
    43.             }else{
    44.                 y=290;
    45.             }
    46.             return false;
    47.         }
    48.         return true;
    49.     }
    50.     public void moveUp(){//坦克向上移动一步
    51.         if(canMove()){
    52.             y-=step;
    53.             direct=1;  
    54.         }
    55.     }
    56.     public void moveDown(){//坦克向下移动一步
    57.         if(canMove()){
    58.             y+=step;
    59.             direct=3;  
    60.         }
    61.     }
    62.     public void moveLeft(){//坦克向左移动一步
    63.         if(canMove()){
    64.             x-=step;
    65.             direct=4;  
    66.         }
    67.     }
    68.     public void moveRight(){//坦克向右移动一步
    69.         if(canMove()){
    70.             x+=step;
    71.             direct=2;
    72.         }
    73.     }
    74.     public void shoot(){//每次射击调用此函数
    75.         shoot = new Shoot(x,y,direct,step,who);
    76.     }
    77.     public String toString() {//用于调试
    78.         return "Tanks [direct=" + direct + ", isLive="
    79.                 + isLive + ", step=" + step + ", x=" + x + ", y=" + y + "]";
    80.     }
    81.     /**以下是get() set()函数*/
    82.     public int getX() {
    83.         return x;
    84.     }
    85.     public void setX(int x) {
    86.         this.x = x;
    87.     }
    88.     public int getY() {
    89.         return y;
    90.     }
    91.     public void setY(int y) {
    92.         this.y = y;
    93.     }
    94.     public int getStep() {
    95.         return step;
    96.     }
    97.     public void setStep(int step) {
    98.         this.step = step;
    99.     }
    100.     public int getDirect() {
    101.         return direct;
    102.     }
    103.     public void setDirect(int direct) {
    104.         this.direct = direct;
    105.     }
    106.     public boolean isLive() {
    107.         return isLive;
    108.     }
    109.     public void setLive(boolean isLive) {
    110.         this.isLive = isLive;
    111.     }
    112.     public void setWho(int who) {
    113.         this.who = who;
    114.     }
    115.     public int getWho() {
    116.         return who;
    117.     }
    118. }
    119. class MyTank extends Tanks{//我的坦克继承了Tanks
    120.     public MyTank(int x, int y, int step, int direct, int who ,boolean isLive) {
    121.         super(x, y, step, direct, who, isLive);
    122.     }
    123. }
    124. class EnemyTanks extends Tanks{//敌人的坦克继承了tanks,也继承了父类的线程
    125.     private int time = 500;//线程占用时间500毫秒
    126.     boolean s=true;//定义一个boolean的开关,坦克死亡,关闭开关,线程死亡.
    127.     public EnemyTanks(int x, int y, int step, int direct, int who, boolean isLive) {
    128.         super(x, y, step, direct, who,isLive);
    129.     }
    130.     public void move(){//敌人的坦克自己移动
    131.         Random r = new Random();
    132.         int random = r.nextInt(50);
    133.         int r1=r.nextInt(4);
    134.         if(!(r1==0)){//如果r1=0,就射击一次
    135.             shoot();
    136.         }
    137.         for (int i = 0; i < random; i++) {//random表示坦克的一次随机移动的距离
    138.             try {
    139.                 Thread.sleep(50);//线程sleep,移动使移动看起来自然一点
    140.             } catch (InterruptedException e) {
    141.                 e.printStackTrace();
    142.             }
    143.             if(r1==0){//根据r1的值判断,此次移动往那个方向
    144.                 if(this.getY()==0||this.getY()==290){
    145.                     r1=3;
    146.                 }else{
    147.                     moveUp();
    148.                 }
    149.             }else if(r1==1){
    150.                 if(this.getX()==690||this.getX()==270){
    151.                     r1=2;
    152.                 }else{
    153.                     moveRight();
    154.                 }
    155.             }else if(r1==2){
    156.                 if(this.getX()==0||this.getX()==370){
    157.                     r1=1;
    158.                 }else{
    159.                     moveLeft();
    160.                 }
    161.             }else if(r1==3){
    162.                 if(this.getY()==450||this.getY()==190){
    163.                     r1=0;
    164.                 }else{
    165.                     moveDown();
    166.                                                                  
    167.                 }
    168.             }
    169.         }
    170.     }
    171.     public void run(){//继承线程功能,必须实现run()函数
    172.         while(s){//s=true表示坦克没有死亡.s=false跳出死循环,坦克死亡
    173.             move();
    174.             try {
    175.                 Thread.sleep(time);//每次线程占用时间500毫秒
    176.             } catch (InterruptedException e) {
    177.                 // TODO Auto-generated catch block
    178.                 e.printStackTrace();
    179.             }
    180.         }
    181.     }
    182. [b]}[/b]
    复制代码
    /********************Shoot类***********************/ ?
    1. /**Shoot类,处理坦克的射击*/
    2. public class Shoot {
    3.     static int[] myShoot = new int[40];//我的坦克子弹放在数组里面
    4.     static int[] enemyShoot = new int[100];//所有敌人共享这个数组
    5.     private int direct;//坦克的方向
    6.     private int who;//是敌人的坦克?还是自己的坦克
    7.     private int x1;//根据情况调整后子弹x的坐标
    8.     private int y1;//根据情况调整后子弹y的坐标
    9.     private int stepx;//子弹在x轴上移动的速度
    10.     private int stepy;//子弹在y轴上移动的速度
    11.     private int speed;//坦克的速度
    12.     static int indexMy=0;//我的子弹索引,数组满了之后indexMy=0;每次加4
    13.     static int indexEnemy=0;//敌人子弹索引,数组满了之后indexEnemy=0;每次加4
    14.     public Shoot(int x,int y,int direct,int step,int who){
    15.         speed=step+8;//根据坦克的速度,设定子弹的速度
    16.         this.direct=direct;
    17.         this.who=who;
    18.         if(direct==1){//if else用来调整子弹射击出去的位置
    19.             x1=x+20;
    20.             y1=y;
    21.             stepx=0;
    22.             stepy=-speed;
    23.         }else if(direct==2){
    24.             x1=x+40;
    25.             y1=y+20;
    26.             stepx=speed;
    27.             stepy=0;
    28.         }else if(direct==3){
    29.             x1=x+20;
    30.             y1=y+40;
    31.             stepx=0;
    32.             stepy=speed;
    33.         }else if(direct==4){
    34.             x1=x;
    35.             y1=y+20;
    36.             stepx=-speed;
    37.             stepy=0;
    38.         }
    39.         if(indexEnemy==enemyShoot.length-4){//表示数组满了
    40.             indexEnemy=0;
    41.         }if(indexMy==myShoot.length-4){//表示数组满了
    42.             indexMy=0;
    43.         }
    44.         if(who==1||who==2||who==3){//敌人的坦克
    45.             enemyShoot[indexEnemy]=x1;//子弹的坐标x
    46.             enemyShoot[indexEnemy+1]=y1;//子弹的坐标y
    47.             enemyShoot[indexEnemy+2]=stepx;//子弹在x轴移动的步伐
    48.             enemyShoot[indexEnemy+3]=stepy;//在t轴移动的步伐
    49.             indexEnemy+=4;
    50.         }else if(who==0||who==-1){//我的坦克
    51.             myShoot[indexMy]=x1;
    52.             myShoot[indexMy+1]=y1;
    53.             myShoot[indexMy+2]=stepx;
    54.             myShoot[indexMy+3]=stepy;
    55.             indexMy+=4;
    56.         }
    57.     }
    58.                                    
    59. }
    复制代码


      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/10/16/150721062.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-16 10:27 , Processed in 0.402948 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表