[Java] 韩顺平坦克大战教学代码,仅供学习分享之用 →→→→→进入此内容的聊天室

来自 , 2020-12-21, 写在 Java, 查看 164 次.
URL http://www.code666.cn/view/faafda66
  1. package com.test3;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. import javax.sound.sampled.AudioFormat;
  7. import javax.sound.sampled.AudioInputStream;
  8. import javax.sound.sampled.AudioSystem;
  9. import javax.sound.sampled.DataLine;
  10. import javax.sound.sampled.SourceDataLine;
  11.  
  12. //播放声音的类
  13. class AePlayWave extends Thread {
  14.  
  15.         private String filename;
  16.         public AePlayWave(String wavfile) {
  17.                 filename = wavfile;
  18.  
  19.         }
  20.  
  21.         public void run() {
  22.  
  23.                 File soundFile = new File(filename);
  24.  
  25.                 AudioInputStream audioInputStream = null;
  26.                 try {
  27.                         audioInputStream = AudioSystem.getAudioInputStream(soundFile);
  28.                 } catch (Exception e1) {
  29.                         e1.printStackTrace();
  30.                         return;
  31.                 }
  32.  
  33.                 AudioFormat format = audioInputStream.getFormat();
  34.                 SourceDataLine auline = null;
  35.                 DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  36.  
  37.                 try {
  38.                         auline = (SourceDataLine) AudioSystem.getLine(info);
  39.                         auline.open(format);
  40.                 } catch (Exception e) {
  41.                         e.printStackTrace();
  42.                         return;
  43.                 }
  44.  
  45.                 auline.start();
  46.                 int nBytesRead = 0;
  47.                 //这是缓冲
  48.                 byte[] abData = new byte[512];
  49.  
  50.                 try {
  51.                         while (nBytesRead != -1) {
  52.                                 nBytesRead = audioInputStream.read(abData, 0, abData.length);
  53.                                 if (nBytesRead >= 0)
  54.                                         auline.write(abData, 0, nBytesRead);
  55.                         }
  56.                 } catch (IOException e) {
  57.                         e.printStackTrace();
  58.                         return;
  59.                 } finally {
  60.                         auline.drain();
  61.                         auline.close();
  62.                 }
  63.  
  64.         }
  65.  
  66.        
  67. }
  68.  
  69. class Node{
  70.         int x;
  71.         int y;
  72.         int direct;
  73.         public Node(int x,int y,int direct)
  74.         {
  75.                 this.x=x;
  76.                 this.y=y;
  77.                 this.direct=direct;
  78.         }
  79. }
  80.  
  81.  
  82. //记录类,同时也可以保存玩家的设置
  83. class Recorder
  84. {
  85.         //记录每关有多少敌人
  86.         private static int enNum=20;
  87.         //设置我有多少可以用的人
  88.         private static int myLife=3;
  89.         //记录总共消灭了多少敌人
  90.         private static int allEnNum=0;
  91.         //从文件中恢复记录点
  92.         static Vector<Node>  nodes=new Vector<Node>();
  93.        
  94.         private static FileWriter fw=null;
  95.         private static BufferedWriter bw=null;
  96.         private static FileReader fr=null;
  97.         private static BufferedReader br=null;
  98.        
  99.         private  Vector<EnemyTank> ets=new Vector<EnemyTank>();
  100.        
  101.        
  102.        
  103.         //完成读取认为
  104.         public Vector<Node> getNodesAndEnNums()
  105.         {
  106.                 try {
  107.                         fr=new FileReader("d:\\myRecording.txt");
  108.                         br=new BufferedReader(fr);
  109.                         String n="";
  110.                         //先读取第一行
  111.                         n=br.readLine();
  112.                         allEnNum=Integer.parseInt(n);
  113.                         while((n=br.readLine())!=null)
  114.                         {
  115.                                 String []xyz=n.split(" ");
  116.                                
  117.                                 Node node=new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1]),Integer.parseInt(xyz[2]));
  118.                                 nodes.add(node);
  119.                         }
  120.                        
  121.                
  122.                 } catch (Exception e) {
  123.                         e.printStackTrace();
  124.                         // TODO: handle exception
  125.                 }finally{
  126.                        
  127.                         try {
  128.                                 //后打开则先关闭
  129.                                 br.close();
  130.                                 fr.close();
  131.                         } catch (Exception e) {
  132.                                 e.printStackTrace();
  133.                                 // TODO: handle exception
  134.                         }
  135.                 }
  136.                
  137.                 return nodes;
  138.                
  139.         }
  140.        
  141.        
  142.         //保存击毁敌人的数量和敌人坦克坐标,方向
  143.        
  144.         public  void keepRecAndEnemyTank()
  145.         {
  146.                 try {
  147.                        
  148.                         //创建
  149.                         fw=new FileWriter("d:\\myRecording.txt");
  150.                         bw=new BufferedWriter(fw);
  151.                        
  152.                         bw.write(allEnNum+"\r\n");
  153.                        
  154.                         System.out.println("size="+ets.size());
  155.                         //保存当前活的敌人坦克的坐标和方向
  156.                         for(int i=0;i<ets.size();i++)
  157.                         {
  158.                                 //取出第一个坦克
  159.                                 EnemyTank et=ets.get(i);
  160.                                
  161.                                 if(et.isLive)
  162.                                 {
  163.                                         //活的就保存
  164.                                         String recode=et.x+" "+et.y+" "+et.direct;
  165.                                        
  166.                                         //写入
  167.                                         bw.write(recode+"\r\n");
  168.                                        
  169.                                 }
  170.                         }
  171.                        
  172.                 } catch (Exception e) {
  173.                         e.printStackTrace();
  174.                         // TODO: handle exception
  175.                 }finally{
  176.                
  177.                         //关闭流
  178.                         try {
  179.                                 //后开先关闭
  180.                                 bw.close();
  181.                                 fw.close();
  182.                         } catch (Exception e) {
  183.                                 e.printStackTrace();
  184.                                 // TODO: handle exception
  185.                         }
  186.                 }
  187.         }
  188.        
  189.        
  190.         //从文件中读取,记录
  191.         public static void getRecoring()
  192.         {
  193.                 try {
  194.                         fr=new FileReader("d:\\myRecording.txt");
  195.                         br=new BufferedReader(fr);
  196.                         String n=br.readLine();
  197.                         allEnNum=Integer.parseInt(n);
  198.                 } catch (Exception e) {
  199.                         e.printStackTrace();
  200.                         // TODO: handle exception
  201.                 }finally{
  202.                        
  203.                         try {
  204.                                 //后打开则先关闭
  205.                                 br.close();
  206.                                 fr.close();
  207.                         } catch (Exception e) {
  208.                                 e.printStackTrace();
  209.                                 // TODO: handle exception
  210.                         }
  211.                 }
  212.         }
  213.        
  214.         //把玩家击毁敌人坦克数量保存到文件中
  215.         public static void keepRecording()
  216.         {
  217.                 try {
  218.                        
  219.                         //创建
  220.                         fw=new FileWriter("d:\\myRecording.txt");
  221.                         bw=new BufferedWriter(fw);
  222.                        
  223.                         bw.write(allEnNum+"\r\n");
  224.                        
  225.                 } catch (Exception e) {
  226.                         e.printStackTrace();
  227.                         // TODO: handle exception
  228.                 }finally{
  229.                
  230.                         //关闭流
  231.                         try {
  232.                                 //后开先关闭
  233.                                 bw.close();
  234.                                 fw.close();
  235.                         } catch (Exception e) {
  236.                                 e.printStackTrace();
  237.                                 // TODO: handle exception
  238.                         }
  239.                 }
  240.         }
  241.        
  242.         public static int getEnNum() {
  243.                 return enNum;
  244.         }
  245.         public static void setEnNum(int enNum) {
  246.                 Recorder.enNum = enNum;
  247.         }
  248.         public static int getMyLife() {
  249.                 return myLife;
  250.         }
  251.         public static void setMyLife(int myLife) {
  252.                 Recorder.myLife = myLife;
  253.         }
  254.        
  255.         //减少敌人数
  256.         public static void reduceEnNum()
  257.         {
  258.                 enNum--;
  259.         }
  260.         //消灭敌人
  261.         public static void addEnNumRec()
  262.         {
  263.                 allEnNum++;
  264.         }
  265.         public static int getAllEnNum() {
  266.                 return allEnNum;
  267.         }
  268.         public static void setAllEnNum(int allEnNum) {
  269.                 Recorder.allEnNum = allEnNum;
  270.         }
  271.  
  272.  
  273.         public  Vector<EnemyTank> getEts() {
  274.                 return ets;
  275.         }
  276.  
  277.  
  278.         public  void setEts(Vector<EnemyTank> ets1) {
  279.                
  280.                 this.ets = ets1;
  281.                 System.out.println("ok");
  282.         }
  283. }
  284.  
  285. //炸弹类
  286. class Bomb
  287. {
  288.         //定义炸弹的坐标
  289.         int x,y;
  290.         //炸弹的生命
  291.         int life=9;
  292.         boolean isLive=true;
  293.         public Bomb(int x,int y)
  294.         {
  295.                 this.x=x;
  296.                 this.y=y;
  297.         }
  298.        
  299.         //减少生命值
  300.         public void lifeDown()
  301.         {
  302.                 if(life>0)
  303.                 {
  304.                         life--;
  305.                 }else{
  306.                         this.isLive=false;
  307.                 }
  308.                
  309.         }
  310.        
  311.        
  312.        
  313. }
  314.  
  315. //子弹类
  316. class Shot implements Runnable  {
  317.         int x;
  318.         int y;
  319.         int direct;
  320.         int speed=1;
  321.         //是否还活着
  322.         boolean isLive=true;
  323.         public Shot(int x,int y,int direct)
  324.         {
  325.                 this.x=x;
  326.                 this.y=y;
  327.                 this.direct=direct;
  328.         }
  329.         public void run() {
  330.                
  331.                 while(true)
  332.                 {
  333.                        
  334.                         try {
  335.                                 Thread.sleep(50);
  336.                         } catch (Exception e) {
  337.                                 // TODO: handle exception
  338.                         }
  339.                        
  340.                         switch(direct)
  341.                         {
  342.                         case 0:
  343.                                 //上
  344.                                 y-=speed;
  345.                                 break;
  346.                         case 1:
  347.                                 x+=speed;
  348.                                 break;
  349.                         case 2:
  350.                                 y+=speed;
  351.                                 break;
  352.                         case 3:
  353.                                 x-=speed;
  354.                                 break;
  355.                         }
  356.                        
  357.                 //      System.out.println("子弹坐标x="+x+" y="+y);
  358.                         //子弹何时死亡???
  359.                        
  360.                         //判断该子弹是否碰到边缘.
  361.                         if(x<0||x>400||y<0||y>300)
  362.                         {
  363.                                 this.isLive=false;
  364.                                 break;
  365.                         }
  366.                 }
  367.         }
  368. }
  369.  
  370.  
  371. //坦克类
  372. class Tank
  373. {
  374.         //表示坦克的横坐标
  375.         int x=0;
  376.         //坦克纵坐标
  377.         int y=0;
  378.        
  379.         //坦克方向
  380.         //0表示上 1表示 右 2表示下  3表示左
  381.         int direct=0;
  382.         int color;
  383.        
  384.         boolean isLive=true;
  385.        
  386.         //坦克的速度
  387.         int speed=1;
  388.         public Tank(int x,int y)
  389.         {
  390.                 this.x=x;
  391.                 this.y=y;
  392.         }
  393.  
  394.         public int getX() {
  395.                 return x;
  396.         }
  397.  
  398.         public void setX(int x) {
  399.                 this.x = x;
  400.         }
  401.  
  402.         public int getY() {
  403.                 return y;
  404.         }
  405.  
  406.         public void setY(int y) {
  407.                 this.y = y;
  408.         }
  409.  
  410.         public int getDirect() {
  411.                 return direct;
  412.         }
  413.  
  414.         public void setDirect(int direct) {
  415.                 this.direct = direct;
  416.         }
  417.  
  418.         public int getSpeed() {
  419.                 return speed;
  420.         }
  421.  
  422.         public void setSpeed(int speed) {
  423.                 this.speed = speed;
  424.         }
  425.  
  426.         public int getColor() {
  427.                 return color;
  428.         }
  429.  
  430.         public void setColor(int color) {
  431.                 this.color = color;
  432.         }
  433.        
  434. }
  435.  
  436. //敌人的坦克,把敌人做成线程类
  437. class EnemyTank extends Tank implements Runnable
  438. {
  439.        
  440.         int times=0;
  441.        
  442.         //定义一个向量,可以访问到MyPanel上所有敌人的坦克
  443.         Vector<EnemyTank> ets=new Vector<EnemyTank>();
  444.        
  445.         //定义一个向量,可以存放敌人的子弹
  446.         Vector<Shot> ss=new Vector<Shot>();
  447.         //敌人添加子弹,应当在刚刚创建坦克和敌人的坦克子弹死亡后
  448.         public EnemyTank(int x,int y)
  449.         {
  450.                 super(x,y);    
  451.         }
  452.        
  453.         //得到MyPanel的敌人坦克向量
  454.         public void setEts(Vector<EnemyTank> vv)
  455.         {
  456.                 this.ets=vv;
  457.         }
  458.  
  459.         //判断是否碰到了别的敌人坦克
  460.         public boolean isTouchOtherEnemy()
  461.         {
  462.                 boolean b=false;
  463.                
  464.                
  465.                 switch(this.direct)
  466.                 {
  467.                 case 0:
  468.                         //我的坦克向上
  469.                         //取出所有的敌人坦克
  470.                         for(int i=0;i<ets.size();i++)
  471.                         {
  472.                                 //取出第一个坦克
  473.                                 EnemyTank et=ets.get(i);
  474.                                 //如果不是自己
  475.                                 if(et!=this)
  476.                                 {
  477.                                         //如果敌人的方向是向下或者向上
  478.                                         if(et.direct==0||et.direct==2)
  479.                                         {
  480.                                                 //左点
  481.                                                 if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
  482.                                                 {
  483.                                                         return true;
  484.                                                 }
  485.                                                 if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
  486.                                                 {
  487.                                                         return true;
  488.                                                 }
  489.                                         }
  490.                                         if(et.direct==3||et.direct==1)
  491.                                         {
  492.                                                 if(this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
  493.                                                 {
  494.                                                         return true;
  495.                                                 }
  496.                                                 if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
  497.                                                 {
  498.                                                         return true;
  499.                                                 }
  500.                                         }
  501.                                 }
  502.                         }
  503.                         break;
  504.                 case 1:
  505.                         //坦克向右
  506.                         //取出所有的敌人坦克
  507.                         for(int i=0;i<ets.size();i++)
  508.                         {
  509.                                 //取出第一个坦克
  510.                                 EnemyTank et=ets.get(i);
  511.                                 //如果不是自己
  512.                                 if(et!=this)
  513.                                 {
  514.                                         //如果敌人的方向是向下或者向上
  515.                                         if(et.direct==0||et.direct==2)
  516.                                         {
  517.                                                 //上点
  518.                                                 if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
  519.                                                 {
  520.                                                         return true;
  521.                                                 }
  522.                                                 //下点
  523.                                                 if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
  524.                                                 {
  525.                                                         return true;
  526.                                                 }
  527.                                         }
  528.                                         if(et.direct==3||et.direct==1)
  529.                                         {
  530.                                                 if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
  531.                                                 {
  532.                                                         return true;
  533.                                                 }
  534.                                                 if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
  535.                                                 {
  536.                                                         return true;
  537.                                                 }
  538.                                         }
  539.                                 }
  540.                         }
  541.                         break;
  542.                 case 2:
  543.                         //坦克向下
  544.                         //取出所有的敌人坦克
  545.                         for(int i=0;i<ets.size();i++)
  546.                         {
  547.                                 //取出第一个坦克
  548.                                 EnemyTank et=ets.get(i);
  549.                                 //如果不是自己
  550.                                 if(et!=this)
  551.                                 {
  552.                                         //如果敌人的方向是向下或者向上
  553.                                         if(et.direct==0||et.direct==2)
  554.                                         {
  555.                                                 //我的左点
  556.                                                 if(this.x>=et.x&&this.x<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)
  557.                                                 {
  558.                                                         return true;
  559.                                                 }
  560.                                                 //我的右点
  561.                                                 if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)
  562.                                                 {
  563.                                                         return true;
  564.                                                 }
  565.                                         }
  566.                                         if(et.direct==3||et.direct==1)
  567.                                         {
  568.                                                 if(this.x>=et.x&&this.x<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
  569.                                                 {
  570.                                                         return true;
  571.                                                 }
  572.                                                
  573.                                                 if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)
  574.                                                 {
  575.                                                         return true;
  576.                                                 }
  577.                                         }
  578.                                 }
  579.                         }
  580.                         break;
  581.                 case 3:
  582.                         //向左
  583.                         //取出所有的敌人坦克
  584.                         for(int i=0;i<ets.size();i++)
  585.                         {
  586.                                 //取出第一个坦克
  587.                                 EnemyTank et=ets.get(i);
  588.                                 //如果不是自己
  589.                                 if(et!=this)
  590.                                 {
  591.                                         //如果敌人的方向是向下或者向上
  592.                                         if(et.direct==0||et.direct==2)
  593.                                         {
  594.                                                 //我的上一点
  595.                                                 if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)
  596.                                                 {
  597.                                                         return true;
  598.                                                 }
  599.                                                 //下一点
  600.                                                 if(this.x>=et.x&&this.x<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)
  601.                                                 {
  602.                                                         return true;
  603.                                                 }
  604.                                         }
  605.                                         if(et.direct==3||et.direct==1)
  606.                                         {
  607.                                                 //上一点
  608.                                                 if(this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)
  609.                                                 {
  610.                                                         return true;
  611.                                                 }
  612.                                                 if(this.x>=et.x&&this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)
  613.                                                 {
  614.                                                         return true;
  615.                                                 }
  616.                                         }
  617.                                 }
  618.                         }
  619.                         break;
  620.                 }
  621.                
  622.                
  623.                 return b;
  624.         }
  625.        
  626.        
  627.        
  628.         public void run() {
  629.                 // TODO Auto-generated method stub
  630.                
  631.                 while(true)
  632.                 {
  633.                        
  634.                
  635.                         switch(this.direct)
  636.                         {
  637.                         case 0:
  638.                                 //说明坦克正在向上
  639.                                 for(int i=0;i<30;i++)
  640.                                 {
  641.                                         if(y>0&&!this.isTouchOtherEnemy())
  642.                                         {
  643.                                                 y-=speed;
  644.                                         }
  645.                                         try {
  646.                                                 Thread.sleep(50);
  647.                                         } catch (Exception e) {
  648.                                                 e.printStackTrace();
  649.                                                 // TODO: handle exception
  650.                                         }
  651.                                 }
  652.                                 break;
  653.                         case 1:
  654.                                 //向右
  655.                                 for(int i=0;i<30;i++)
  656.                                 {
  657.                                         //保证坦克不出边界
  658.                                         if(x<400&&!this.isTouchOtherEnemy())
  659.                                         {
  660.                                                 x+=speed;
  661.                                         }
  662.                                         try {
  663.                                                 Thread.sleep(50);
  664.                                         } catch (Exception e) {
  665.                                                 e.printStackTrace();
  666.                                                 // TODO: handle exception
  667.                                         }
  668.                                 }
  669.                                 break;
  670.                         case 2:
  671.                                 //向下
  672.                                 for(int i=0;i<30;i++)
  673.                                 {
  674.                                         if(y<300&&!this.isTouchOtherEnemy())
  675.                                         {
  676.                                                 y+=speed;
  677.                                         }
  678.                                         try {
  679.                                                 Thread.sleep(50);
  680.                                         } catch (Exception e) {
  681.                                                 e.printStackTrace();
  682.                                                 // TODO: handle exception
  683.                                         }
  684.                                 }
  685.                                 break;
  686.                         case 3:
  687.                                 //向左
  688.                                 for(int i=0;i<30;i++)
  689.                                 {
  690.                                         if(x>0&&!this.isTouchOtherEnemy())
  691.                                         {
  692.                                                 x-=speed;
  693.                                         }
  694.                                         try {
  695.                                                 Thread.sleep(50);
  696.                                         } catch (Exception e) {
  697.                                                 e.printStackTrace();
  698.                                                 // TODO: handle exception
  699.                                         }
  700.                                 }
  701.                                 break;
  702.                        
  703.                         }
  704.                        
  705.                         this.times++;
  706.                        
  707.                         if(times%2==0)
  708.                         {
  709.                                 if(isLive)
  710.                                 {
  711.                                         if(ss.size()<5)
  712.                                         {
  713.                                                 //System.out.println("et.ss.size()<5="+et.ss.size());
  714.                                                 Shot s=null;
  715.                                                 //没有子弹
  716.                                                 //添加
  717.                                                 switch(direct)
  718.                                                 {
  719.                                                 case 0:
  720.                                                         //创建一颗子弹
  721.                                                          s=new Shot(x+10,y,0);
  722.                                                         //把子弹加入向量
  723.                                                         ss.add(s);
  724.                                                         break;
  725.                                                 case 1:
  726.                                                         s=new Shot(x+30,y+10,1);
  727.                                                         ss.add(s);
  728.                                                         break;
  729.                                                 case 2:
  730.                                                          s=new Shot(x+10,y+30,2);
  731.                                                         ss.add(s);
  732.                                                         break;
  733.                                                 case 3:
  734.                                                         s=new Shot(x,y+10,3);
  735.                                                         ss.add(s);
  736.                                                         break;
  737.                                                 }
  738.                                                
  739.                                                 //启动子弹
  740.                                                 Thread t=new Thread(s);
  741.                                                 t.start();
  742.                                         }
  743.                                 }
  744.                         }
  745.                        
  746.                        
  747.                         //让坦克随机产生一个新的方向
  748.                         this.direct=(int)(Math.random()*4);
  749.                        
  750.                         //判断敌人坦克是否死亡
  751.                         if(this.isLive==false)
  752.                         {
  753.                                 //让坦克死亡后,退出线程.
  754.                                 break;
  755.                         }
  756.                        
  757.                        
  758.                        
  759.                        
  760.                        
  761.                 }
  762.                
  763.         }
  764. }
  765.  
  766. //我的坦克
  767. class Hero extends Tank
  768. {
  769.        
  770.         //子弹
  771.        
  772.         //Shot s=null;
  773.         Vector<Shot> ss=new Vector<Shot>();
  774.         Shot s=null;
  775.         public Hero(int x,int y)
  776.         {
  777.                 super(x,y);
  778.                
  779.                
  780.         }
  781.        
  782.         //开火
  783.         public void shotEnemy()
  784.         {
  785.                
  786.                
  787.                 switch(this.direct)
  788.                 {
  789.                 case 0:
  790.                         //创建一颗子弹
  791.                          s=new Shot(x+10,y,0);
  792.                         //把子弹加入向量
  793.                         ss.add(s);
  794.                         break;
  795.                 case 1:
  796.                         s=new Shot(x+30,y+10,1);
  797.                         ss.add(s);
  798.                         break;
  799.                 case 2:
  800.                          s=new Shot(x+10,y+30,2);
  801.                         ss.add(s);
  802.                         break;
  803.                 case 3:
  804.                         s=new Shot(x,y+10,3);
  805.                         ss.add(s);
  806.                         break;
  807.                        
  808.                 }
  809.                 //启动子弹线程
  810.                 Thread t=new Thread(s);
  811.                 t.start();
  812.                
  813.         }
  814.        
  815.        
  816.         //坦克向上移动
  817.         public void moveUp()
  818.         {
  819.                 y-=speed;
  820.         }
  821.         //坦克向右移动
  822.         public void moveRight()
  823.         {
  824.                 x+=speed;
  825.         }
  826.        
  827.         //坦克向下移动
  828.         public void moveDown()
  829.         {
  830.                 y+=speed;
  831.         }
  832.        
  833.         //向左
  834.         public void moveLeft()
  835.         {
  836.                 x-=speed;
  837.         }
  838. }

回复 "韩顺平坦克大战教学代码,仅供学习分享之用"

这儿你可以回复上面这条便签

captcha