Tank游戏源码

 

Midlet:

Code:
  1. package STF;   
  2.   
  3. import javax.microedition.lcdui.Display;   
  4. import javax.microedition.midlet.MIDlet;   
  5. import javax.microedition.midlet.MIDletStateChangeException;   
  6.   
  7. public class Midlet extends MIDlet {   
  8.   
  9.     Display display;   
  10.     MainCanvas main;   
  11.   
  12.     public Midlet() {   
  13.         display = Display.getDisplay(this);   
  14.         main = new MainCanvas(this);   
  15.     }   
  16.   
  17.     protected void destroyApp(boolean arg0) throws MIDletStateChangeException {   
  18.   
  19.     }   
  20.   
  21.     protected void pauseApp() {   
  22.   
  23.     }   
  24.   
  25.     protected void startApp() throws MIDletStateChangeException {   
  26.         display.setCurrent(main);   
  27.     }   
  28.   
  29. }  

 

MainCanvas:

Code:
  1. package STF;   
  2.   
  3. import java.io.IOException;   
  4. import java.util.Random;   
  5.   
  6. import javax.microedition.lcdui.Graphics;   
  7. import javax.microedition.lcdui.Image;   
  8. import javax.microedition.lcdui.game.GameCanvas;   
  9. import javax.microedition.lcdui.game.LayerManager;   
  10. import javax.microedition.lcdui.game.Sprite;   
  11.   
  12. public class MainCanvas extends GameCanvas implements Runnable {   
  13.   
  14.     Image imgTank,imgE,imgMenu,imgLogo;//资源图片   
  15.     int[] oldRgb;//实现进入时渐变效果的时的数组   
  16.     int alpha = 0;//透明度   
  17.     boolean isOK=false;//记录是否载入完成   
  18.     static boolean isDie=false;//记录我方坦克是否死亡   
  19.     static boolean isETDie=false;//记录敌方坦克是否死亡   
  20.     Map map;//创建地图对象   
  21.     EnemyTank ets[]=new EnemyTank[10];//创建一个长度为10的敌方坦克数组   
  22.     MainTank mt;//创建一个我方坦克   
  23.     LayerManager lm;//创建图层管理对象   
  24.     static int score=0;//得分   
  25.     static int level=1;//级别   
  26.     static int lifeCount=3;//我方坦克生命数   
  27.     static int enemyCount=1;//敌方坦克记数器,用于记录死亡坦克的数目   
  28.     int waitCount=0;//显示信息的等待时间   
  29.     int controller=0;//开始菜单目录选择控制器   
  30.     Random random;   
  31.        
  32.     //游戏的几种状态   
  33.     static final int LOADING=0;//载入游戏   
  34.     static final int WAITSTART=1;//等待开始   
  35.     static final int PLAYING=2;//游戏进行中   
  36.     static final int NEXTLEVEL=3;//提示进入下一关   
  37.     static final int GAMEOVER=4;//游戏线束   
  38.     static int gameState=LOADING;//记录游戏状态,初值为载入游戏   
  39.     Midlet m;//创建一个Midlet对象,用于实现退出游戏操作   
  40.        
  41.     //构造函数,传入参数一个Midlet对象   
  42.     protected MainCanvas(Midlet m) {   
  43.         super(true);   
  44.         random=new Random();   
  45.         this.m=m;   
  46.         map=new Map();//初始化地图对象   
  47.         map.loadMap();//载入地图   
  48.         lm=new LayerManager();//初始化图层管理器   
  49.            
  50.         //加载图片资源   
  51.         try {   
  52.             imgTank=Image.createImage("/tank.png");   
  53.             imgE = Image.createImage("/enemytank.png");   
  54.             imgMenu = Image.createImage("/menu.png");   
  55.             imgLogo = Image.createImage("/tank_logo.png");   
  56.                
  57.         } catch (IOException e) {   
  58.             e.printStackTrace();   
  59.         }   
  60.            
  61.         //保存图像信息,用于实现渐变效果   
  62.         oldRgb = new int[imgLogo.getWidth() * imgLogo.getHeight()];   
  63.         imgLogo.getRGB(oldRgb, 0, imgLogo.getWidth(), 00, imgLogo.getWidth(), imgLogo   
  64.                 .getHeight());   
  65.            
  66.         mt=new MainTank(imgTank,13,13); //初始化我方坦克对象   
  67.         mt.setPosition(16*3+1316*13+2);//设置我方坦克位置   
  68.         mt.setTransform(Sprite.TRANS_MIRROR_ROT90);//设置坦克朝向   
  69.            
  70.         //初始化敌方坦克数组中的所有对象   
  71.         for(int i=0;i<ets.length;i++ ){   
  72.             ets[i]=new EnemyTank(imgE,13,13);   
  73.             ets[i].setVisible(false);   
  74.         }   
  75.            
  76.         //在图层管理器中依次加入对象   
  77.         lm.append(map.ty_grass);//最上层为草地   
  78.         lm.append(mt);   
  79.         //lm.append(et);   
  80.            
  81.         for(int i=0;i<ets.length;i++ ){   
  82.             lm.append(ets[i]);//敌方坦克   
  83.         }   
  84.                
  85.         for(int i=0;i<ets.length;i++ ){   
  86.             for(int j=0;j<ets[i].bullets.length;j++ ){   
  87.                 lm.append(ets[i].bullets[j]);//所有子弹   
  88.                 lm.append(ets[i].bullets[j].s_exp);//所有敌方坦克子弹爆炸精灵   
  89.             }   
  90.         }   
  91.            
  92.         for(int i=0;i<mt.bullets.length;i++)   
  93.         {   
  94.             lm.append(mt.bullets[i]);//我方坦克子弹   
  95.             lm.append(mt.bullets[i].s_exp);//我方坦克子弹爆炸精灵   
  96.         }   
  97.            
  98.         lm.append(map.ty_oldhome);//老窝   
  99.         lm.append(map.ty_brick);//砖块   
  100.         lm.append(map.ty_shuini);//水泥   
  101.         lm.append(map.ty_water);//水泽   
  102.         new Thread(this).start();//启动线程   
  103.   
  104.     }   
  105.   
  106.     //run方法,根据游戏状态执行不同的"按键输入,逻辑处理,绘制图像"操作   
  107.     public void run() {   
  108.         while (true) {   
  109.             switch(gameState){   
  110.             case LOADING:   
  111.                 loadingInput();   
  112.                 loadingLogic();   
  113.                 loadingPaint();   
  114.                 break;   
  115.             case WAITSTART:   
  116.                 waitStartInput();   
  117.                 waitStartLogic();   
  118.                 waitStartPaint();   
  119.                 break;   
  120.             case PLAYING:   
  121.                 input();   
  122.                 logic();   
  123.                 paint();   
  124.                 break;   
  125.             case NEXTLEVEL:   
  126.                 nextLevelInput();   
  127.                 nextLevelLogic();   
  128.                 nextLevelPaint();   
  129.                 break;   
  130.             case GAMEOVER:   
  131.                 gameOverInput();   
  132.                 gameOverLogic();   
  133.                 gameOverPaint();   
  134.                 break;   
  135.                
  136.             }   
  137.             try {   
  138.                 Thread.sleep(100);   
  139.             } catch (InterruptedException e) {   
  140.                 e.printStackTrace();   
  141.             }   
  142.         }   
  143.     }   
  144.        
  145.     //游戏结束的绘制方法,显示相关信息   
  146.     private void gameOverPaint() {   
  147.         Graphics g = getGraphics();   
  148.         g.setColor(0);   
  149.         g.fillRect(00, getWidth(), getHeight());   
  150.         g.setColor(0x00ffffff);   
  151.         g.drawString("游戏结束!"this.getWidth() / 2this.getHeight() / 2-20,   
  152.             Graphics.HCENTER | Graphics.TOP);   
  153.         g.drawString("总得分:    "+score+"    分"this.getWidth() / 2this.getHeight() / 2+20,   
  154.                 Graphics.HCENTER | Graphics.TOP);   
  155.         g.drawString("按任意键返回"this.getWidth() / 2this.getHeight() / 2+40,   
  156.                 Graphics.HCENTER | Graphics.TOP);   
  157.         flushGraphics();   
  158.     }   
  159.   
  160.     //开始菜单选择   
  161.     private void loadingInput() {   
  162.         int k=this.getKeyStates();   
  163.         if ((k & GameCanvas.UP_PRESSED) != 0){   
  164.             controller--;   
  165.             //循环效果   
  166.             if(controller<0){   
  167.                 controller=4;   
  168.             }   
  169.         }   
  170.         if ((k & GameCanvas.DOWN_PRESSED) != 0){   
  171.             controller++;   
  172.             //循环效果   
  173.             if(controller>4){   
  174.                 controller=0;   
  175.             }   
  176.         }   
  177.         if((k & GameCanvas.FIRE_PRESSED) != 0){   
  178.             switch(controller){   
  179.             case 0://NewGame   
  180.                 gameState=NEXTLEVEL;   
  181.                 break;   
  182.             case 4://Exit   
  183.                 m.notifyDestroyed();   
  184.                 break;   
  185.             }   
  186.         }   
  187.     }   
  188.        
  189.     //实现图片的渐变效果   
  190.     private void loadingLogic() {   
  191.         for (int i = 0; i < oldRgb.length; i++) {   
  192.             oldRgb[i] = (oldRgb[i] & 0x00ffffff) | (alpha << 24);   
  193.         }   
  194.         if(alpha<250){   
  195.             alpha += 10;   
  196.             if(alpha==250){   
  197.                 isOK=true;//图片完全显示,载入完毕   
  198.             }   
  199.         }   
  200.     }   
  201.        
  202.     private void loadingPaint() {   
  203.         Graphics g = getGraphics();   
  204.         g.setColor(0);   
  205.         g.fillRect(00, getWidth(), getHeight());   
  206.         g.drawRGB(oldRgb, 0, imgLogo.getWidth(), (this.getWidth()-imgLogo.getWidth())/240, imgLogo.getWidth(), imgLogo   
  207.                 .getHeight(), true);   
  208.         if(!isOK){   
  209.             g.setColor(0x00ffffff);   
  210.             g.drawString("游戏载入中……"this.getWidth() / 2this.getHeight() / 2+imgLogo.getHeight(),   
  211.                 Graphics.HCENTER | Graphics.TOP);   
  212.         }else{   
  213.             //绘制菜单图片   
  214.             g.drawImage(imgMenu, (this.getWidth()-imgMenu.getWidth())/280+imgLogo.getHeight(), 20);   
  215.                
  216.             //根据控制器绘制选择箭头的显示   
  217.         switch(controller){   
  218.         case 0:   
  219.             g.setColor(0x00ffffff);   
  220.             g.fillArc((this.getWidth()-imgMenu.getWidth())/2-1585+imgLogo.getHeight(), 1010270180);   
  221.             break;   
  222.         case 1:   
  223.             g.setColor(0x00ffffff);   
  224.             g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15103+imgLogo.getHeight(), 1010270180);   
  225.             break;   
  226.         case 2:   
  227.             g.setColor(0x00ffffff);   
  228.             g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15121+imgLogo.getHeight(), 1010270180);   
  229.             break;   
  230.         case 3:   
  231.             g.setColor(0x00ffffff);   
  232.             g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15139+imgLogo.getHeight(), 1010270180);   
  233.             break;   
  234.         case 4:   
  235.             g.setColor(0x00ffffff);   
  236.             g.fillArc((this.getWidth()-imgMenu.getWidth())/2-15157+imgLogo.getHeight(), 1010270180);   
  237.             break;   
  238.         }   
  239.         }   
  240.         flushGraphics();   
  241.     }   
  242.        
  243.     private void waitStartInput() {   
  244.            
  245.     }   
  246.        
  247.     private void waitStartLogic() {   
  248.            
  249.     }   
  250.        
  251.     private void waitStartPaint() {   
  252.         flushGraphics();   
  253.     }   
  254.        
  255.     private void nextLevelPaint() {   
  256.         Graphics g = getGraphics();   
  257.         g.setColor(0);   
  258.         g.fillRect(00, getWidth(), getHeight());   
  259.         g.setColor(0x00ffffff);   
  260.         g.drawString("第    "+level+"    关"this.getWidth() / 2this.getHeight() / 2-20,   
  261.             Graphics.HCENTER | Graphics.TOP);   
  262.         g.drawString("目标:"+enemyCount+"个坦克"this.getWidth() / 2this.getHeight() / 2+20,   
  263.                 Graphics.HCENTER | Graphics.TOP);   
  264.         flushGraphics();   
  265.     }   
  266.   
  267.     private void nextLevelLogic() {   
  268.         mt.setPosition(16*3+1316*13+2);   
  269.         mt.setTransform(Sprite.TRANS_MIRROR_ROT90);   
  270.         waitCount++;   
  271.         if(waitCount==20){   
  272.   
  273.             waitCount=0;   
  274.             gameState=PLAYING;   
  275.             for(int i=0;i<mt.bullets.length;i++){   
  276.                 mt.bullets[i].setVisible(false);   
  277.                 mt.bullets[i].s_exp.setVisible(false);   
  278.             }   
  279.             for(int i=0;i<ets.length;i++){   
  280.                 ets[i].setVisible(false);   
  281.                 for(int j=0;j<ets[i].bullets.length;j++){   
  282.                     ets[i].bullets[j].setVisible(false);   
  283.                     ets[i].bullets[j].s_exp.setVisible(false);             
  284.                 }   
  285.             }   
  286.                
  287.             map.loadMap();   
  288.             createET(32,20);   
  289.             createET(50,20);   
  290.             createET(100,20);   
  291.         }   
  292.     }   
  293.   
  294.     private void nextLevelInput() {   
  295.            
  296.     }   
  297.   
  298.     private void gameOverLogic() {   
  299.         for(int i=0;i<ets.length;i++){   
  300.             ets[i].setVisible(false);   
  301.             for(int j=0;j<ets[i].bullets.length;j++){   
  302.                 ets[i].bullets[j].setVisible(false);   
  303.                 ets[i].bullets[j].s_exp.setVisible(false);             
  304.             }   
  305.         }   
  306.     }   
  307.   
  308.     private void gameOverInput() {   
  309.         int k=this.getKeyStates();   
  310.         if(k!=0){   
  311.             gameState = LOADING;   
  312.             init();   
  313.         }   
  314.     }   
  315.   
  316.     private void init() {   
  317.         score=0;   
  318.         level=1;   
  319.         lifeCount=3;   
  320.         enemyCount=1;   
  321.         waitCount=0;   
  322.         controller=0;   
  323.         isDie=false;   
  324.         map.loadMap();   
  325.         mt.setVisible(true);   
  326.         mt.setPosition(16*3+1316*13+2);   
  327.         mt.setTransform(Sprite.TRANS_MIRROR_ROT90);   
  328.         for(int i=0;i<mt.bullets.length;i++){   
  329.             mt.bullets[i].setVisible(false);   
  330.             mt.bullets[i].s_exp.setVisible(false);   
  331.         }   
  332.            
  333.     }   
  334.   
  335.     private void paint()   
  336.     {   
  337.         Graphics g = getGraphics();   
  338.         g.setColor(0);   
  339.         g.fillRect(00, getWidth(), getHeight());   
  340.         lm.paint(g, 2323);   
  341.         g.setColor(0x00ffffff);   
  342.         g.drawString("第  "+level+"  关"4010,   
  343.                 Graphics.HCENTER | Graphics.TOP);   
  344.         g.drawString("得分: "+score, this.getWidth()-80,10,   
  345.                 Graphics.HCENTER | Graphics.TOP);   
  346.         g.drawString("生命: "+lifeCount+" ×"16*3,this.getHeight()-20,   
  347.                 Graphics.HCENTER | Graphics.TOP);   
  348.         g.setColor(0x00ffffff);   
  349.         g.drawRegion(imgTank, 0131313, Sprite.TRANS_MIRROR_ROT90, 16*5this.getHeight()-180);   
  350.         g.drawString("任务: "+enemyCount+" ×"16*11,this.getHeight()-20,   
  351.                 Graphics.HCENTER | Graphics.TOP);   
  352.         g.setColor(0x00ffffff);   
  353.         g.drawRegion(imgE, 0131313, Sprite.TRANS_MIRROR_ROT90, 16*13this.getHeight()-180);   
  354.         flushGraphics();   
  355.     }   
  356.        
  357.     private void input()   
  358.     {   
  359.         int k = getKeyStates();   
  360.         mt.input(k);   
  361.     }   
  362.        
  363.     private void logic()   
  364.     {   
  365.         if(isDie){   
  366.             mt.setVisible(true);   
  367.             mt.setPosition(16*3+1316*13+2);   
  368.             mt.setTransform(Sprite.TRANS_MIRROR_ROT90);   
  369.             isDie=false;   
  370.         }   
  371.         if(isETDie){   
  372.             createET(32*(random.nextInt(4)+1),20);   
  373.             isETDie=false;   
  374.         }   
  375.         if(enemyCount==0){   
  376.             gameState=NEXTLEVEL;   
  377.             level++;   
  378.             enemyCount=(level-1)*5;   
  379.         }   
  380.         mt.checkCollidesWith(map);   
  381.         for(int i=0;i<ets.length;i++){   
  382.             if(ets[i].isVisible()&&mt.collidesWith(ets[i], true)){   
  383.                 mt.move(-mt.lastX, -mt.lastY);   
  384.             }   
  385.         }   
  386.         for(int i=0;i<ets.length;i++){   
  387.             if(ets[i].isVisible()){   
  388.                 ets[i].checkCollidesWith(map);   
  389.                 for(int j=0;j<ets.length;j++){   
  390.                     if(i!=j&&ets[j].isVisible()){   
  391.                         checkCollidesWith(ets[i],ets[j]);   
  392.                     }   
  393.                 }   
  394.                 ets[i].logic(map,mt);   
  395.                 mt.logic(map,ets[i]);   
  396.             }   
  397.         }   
  398.     }   
  399.        
  400.     private void checkCollidesWith(EnemyTank et1, EnemyTank et2) {   
  401.         if(et1.collidesWith(et2, true)){   
  402.             et1.move(-et1.lastX, -et1.lastY);   
  403.             et2.move(-et2.lastX, -et2.lastY);   
  404.             et1.dir = (et1.dir+2)%4;//碰撞后方向反向   
  405.             et2.dir =(et2.dir+2)%4;   
  406.         }   
  407.            
  408.     }      
  409.   
  410.     public void createET(int x,int y){   
  411.         for (int i = 0; i < ets.length; i++) {   
  412.             if (!ets[i].isVisible()) {   
  413.                 ets[i].setPosition(x,y);   
  414.                 ets[i].setVisible(true);   
  415.                 break;   
  416.             }   
  417.         }   
  418.     }   
  419. }   

 

 

Map:

Code:
  1. package STF;   
  2.   
  3. import java.io.IOException;   
  4.   
  5. import javax.microedition.lcdui.Image;   
  6. import javax.microedition.lcdui.game.TiledLayer;   
  7.   
  8. public class Map {   
  9.   
  10.     Image img;   
  11.     TiledLayer ty_grass, ty_water, ty_shuini, ty_brick, ty_oldhome;   
  12.        
  13.     int[][] map = {   
  14.             { 3333333333333 },   
  15.             { 3000000000003 },   
  16.             { 3000000000003 },   
  17.             { 3000400040003 },   
  18.             { 3110011100113 },   
  19.             { 3110011100113 },   
  20.             { 3000000000003 },   
  21.             { 3000000000003 },   
  22.             { 3304030304033 },   
  23.             { 3020000000203 },   
  24.             { 3020000000203 },   
  25.             { 3024444444203 },   
  26.             { 3000000000003 },   
  27.             { 3040044400403 },   
  28.             { 3000045400003 },   
  29.             { 3333333333333 }   
  30.             };   
  31.        
  32.     public Map() {   
  33.         try {   
  34.             img = Image.createImage("/bg.png");   
  35.         } catch (IOException e) {   
  36.             // TODO Auto-generated catch block   
  37.             e.printStackTrace();   
  38.         }   
  39.         ty_brick = new TiledLayer(1316, img, 1515);   
  40.         ty_grass = new TiledLayer(1316, img, 1515);   
  41.         ty_shuini = new TiledLayer(1316, img, 1515);   
  42.         ty_oldhome = new TiledLayer(1316, img, 1515);   
  43.         ty_water = new TiledLayer(1316, img, 1515);   
  44.   
  45.     }   
  46.        
  47.     public void loadMap() {   
  48.   
  49.         for (int i = 0; i < map.length; i++) {   
  50.             for (int j = 0; j < map[i].length; j++) {   
  51.                 // ty.setCell(j, i, map[i][j]);   
  52.                 if (map[i][j] == 1) {   
  53.                     ty_grass.setCell(j, i, map[i][j]);   
  54.                 }   
  55.                 if (map[i][j] == 2) {   
  56.                     ty_water.setCell(j, i, map[i][j]);   
  57.                 }   
  58.                 if (map[i][j] == 3) {   
  59.                     ty_shuini.setCell(j, i, map[i][j]);   
  60.                 }   
  61.                 if (map[i][j] == 4) {   
  62.                     ty_brick.setCell(j, i, map[i][j]);   
  63.                 }   
  64.                 if (map[i][j] == 5) {   
  65.                     ty_oldhome.setCell(j, i, map[i][j]);   
  66.                 }   
  67.             }   
  68.         }   
  69.     }   
  70. }   

 

GameSprite:

Code:
  1. package STF;   
  2.   
  3. import java.io.IOException;   
  4. import java.util.Random;   
  5.   
  6. import javax.microedition.lcdui.Image;   
  7. import javax.microedition.lcdui.game.Sprite;   
  8. /****************************************************  
  9.  * GameSprite类继承自Sprite类,封装坦克的公共属性和方法。  
  10.  ****************************************************/  
  11. public class GameSprite extends Sprite{   
  12.        
  13.     //坦克运动的四个方向   
  14.     static final int UP = 0;   
  15.     static final int RIGHT = 1;   
  16.     static final int DOWN = 2;   
  17.     static final int LEFT = 3;   
  18.        
  19.     int dir=0;//记录坦克正在运动的方向   
  20.        
  21.     int lastX, lastY;//记录坦克最后一次运动的距离,为防止坦克与碰到的东西重叠,使其返回   
  22.        
  23.     Random ran;//随机数,用于敌方坦克的随机产生,方向的随机变化等   
  24.        
  25.     int bCount=0;//子弹记数器,用于实现连发的间断效果   
  26.        
  27.     Image imgBullet;//子弹图片   
  28.        
  29.     Bullet bullets[]=new Bullet[20];//子弹数组(参见子弹类Bullet)   
  30.        
  31.     //构造函数   
  32.     public GameSprite(Image image, int frameWidth, int frameHeight) {   
  33.         super(image, frameWidth, frameHeight);   
  34.            
  35.         //设置相对坐标点为精灵的中心   
  36.         this.defineReferencePixel(this.getWidth() / 2this.getHeight() / 2);   
  37.            
  38.         ran=new Random();//初始化随机数   
  39.            
  40.         try {   
  41.             //导入子弹图片   
  42.             imgBullet = Image.createImage("/bullet.png");          
  43.         } catch (IOException e) {   
  44.             e.printStackTrace();   
  45.         }   
  46.            
  47.         //初始化所有子弹   
  48.         for(int i=0;i<bullets.length;i++){   
  49.             bullets[i]=new Bullet(imgBullet,3,3);   
  50.             bullets[i].setVisible(false);   
  51.         }   
  52.     }   
  53.        
  54.     //检查并处理坦克与地图相碰   
  55.     public void checkCollidesWith(Map map){   
  56.         if (this.collidesWith(map.ty_brick, true) || this.collidesWith(map.ty_shuini, true)   
  57.                 || this.collidesWith(map.ty_water, true)) {   
  58.             this.move(-lastX, -lastY);//相碰后返回最后次运动的距离   
  59.         }   
  60.     }   
  61. }   

 

MainTank:

Code:
  1. package STF;   
  2.   
  3. import javax.microedition.lcdui.Image;   
  4. import javax.microedition.lcdui.game.GameCanvas;   
  5. import javax.microedition.lcdui.game.Sprite;   
  6. /**************************************************  
  7.  * MainTank类继承自GamSprite类,封装我方坦克的特有方法。  
  8.  *************************************************/  
  9. public class MainTank extends GameSprite{   
  10.        
  11.     //构造函数   
  12.     public MainTank(Image image, int frameWidth, int frameHeight) {   
  13.         super(image, frameWidth, frameHeight);     
  14.     }   
  15.        
  16.     /*我方坦克的逻辑处理,包括子弹的移动,子弹的碰撞处理,子弹的爆炸  
  17.      *传入地图参数map,处理与子弹与地图的碰撞  
  18.      *传入敌方坦克参数et,处理与子弹与敌方坦克的碰撞 */  
  19.     public void logic(Map map,EnemyTank et){   
  20.         for (int i = 0; i < bullets.length; i++) {   
  21.             bullets[i].move();//子弹移动   
  22.         }   
  23.         for (int i = 0; i < bullets.length; i++) {   
  24.             bullets[i].checkCollidesWith(map);//子弹与地图的碰撞处理   
  25.                
  26.             bullets[i].checkCollidesWith(et);//子弹与敌方坦克的碰撞处理   
  27.         }   
  28.         for (int i = 0; i < bullets.length; i++) {   
  29.             bullets[i].exp();//子弹的爆炸   
  30.         }   
  31.     }   
  32.   
  33.     //我坦克的按键处理   
  34.     public void input(int k){   
  35.         //首先设置最后一次移动的距离为0   
  36.         lastX = 0;   
  37.         lastY = 0;   
  38.            
  39.         //按向上键   
  40.         if ((k & GameCanvas.UP_PRESSED) != 0) {   
  41.             this.setTransform(Sprite.TRANS_MIRROR_ROT90);//设置坦克炮筒朝向   
  42.             this.nextFrame();//实现坦克的运动时闪动效果   
  43.             this.move(0, -2);//向上移动2   
  44.             lastY = -2;//设置最后一次在y方向向上移动的距离为2   
  45.             dir=UP;//设置坦克正在运动的方向为向上   
  46.         }   
  47.            
  48.         //按向下键   
  49.         if ((k & GameCanvas.DOWN_PRESSED) != 0) {   
  50.             this.setTransform(Sprite.TRANS_MIRROR_ROT270);//设置坦克炮筒朝向   
  51.             this.nextFrame();//实现坦克的运动时闪动效果   
  52.             this.move(02);//向下移动2   
  53.             lastY = 2;//设置最后一次在y方向向下移动的距离为2   
  54.             dir=DOWN;//设置坦克正在运动的方向为向下   
  55.         }   
  56.            
  57.         //按向左键   
  58.         if ((k & GameCanvas.LEFT_PRESSED) != 0) {   
  59.             this.setTransform(Sprite.TRANS_MIRROR);//设置坦克炮筒朝向   
  60.             this.nextFrame();//实现坦克的运动时闪动效果   
  61.             this.move(-20);//向左移动2   
  62.             lastX = -2;//设置最后一次在x方向各左移动的距离为2   
  63.             dir=LEFT;//设置坦克正在运动的方向为向左   
  64.         }   
  65.            
  66.         //按向右键   
  67.         if ((k & GameCanvas.RIGHT_PRESSED) != 0) {   
  68.             this.setTransform(Sprite.TRANS_NONE);//设置坦克炮筒朝向   
  69.             this.nextFrame();//实现坦克的运动时闪动效果   
  70.             this.move(20);//向右移动2   
  71.             lastX = 2;//设置最后一次在x方向向右移动的距离为2   
  72.             dir=RIGHT;//设置坦克正在运动的方向为向右   
  73.         }   
  74.            
  75.         //按向上键   
  76.         if((k & GameCanvas.FIRE_PRESSED) != 0){   
  77.             createBullets();//产生子弹   
  78.         }      
  79.     }   
  80.        
  81.     //产生子弹方法   
  82.     public void createBullets(){   
  83.         bCount++;//记数器加1   
  84.            
  85.         if(bCount==2)//两次记数后再产生子弹   
  86.         {   
  87.             //遍历   
  88.         for (int i = 0; i < bullets.length; i++) {   
  89.             //先判断子弹不可见再产生   
  90.             if (!bullets[i].isVisible()) {   
  91.                    
  92.                 //设置子弹初始位置   
  93.                 bullets[i].setPosition(this.getRefPixelX(), this  
  94.                         .getRefPixelY());   
  95.                    
  96.                 bullets[i].setVisible(true);//设置子弹可见   
  97.                 bullets[i].dir=this.dir;//设置子弹初始位置   
  98.                 bullets[i].launcher=Bullet.MT;//设置子弹为我坦克发出   
  99.                 break;//产生一个子弹后,立即跳出循环   
  100.             }   
  101.         }   
  102.         bCount=0;//产生子弹设设置记数器为0   
  103.         }   
  104.     }   
  105.        
  106.     public void bulletLogic(Bullet b) {   
  107.         b.move();   
  108.         //b.checkCollidesWith(map);   
  109.     }   
  110. }   

 

EnemyTank:
 

Code:
  1. package STF;   
  2.   
  3. import javax.microedition.lcdui.Image;   
  4. import javax.microedition.lcdui.game.Sprite;   
  5. /**************************************************  
  6.  * EnemyTank类继承自GamSprite类,封装敌方坦克的特有方法。  
  7.  *************************************************/  
  8. public class EnemyTank extends GameSprite{   
  9.   
  10.     //构造函数   
  11.     public EnemyTank(Image image, int frameWidth, int frameHeight) {   
  12.         super(image, frameWidth, frameHeight);   
  13.     }   
  14.   
  15.     //检查并处理坦克与地图相碰   
  16.     public void checkCollidesWith(Map map){   
  17.         if (this.collidesWith(map.ty_brick, true) || this.collidesWith(map.ty_shuini, true)   
  18.                 || this.collidesWith(map.ty_water, true)) {   
  19.             this.move(-lastX, -lastY);   
  20.             this.dir = ran.nextInt(4);//碰撞后随机变向   
  21.         }   
  22.     }   
  23.        
  24.     /*敌方坦克的逻辑处理,包括坦克的随机移动,子弹的移动,子弹的碰撞处理,子弹的爆炸  
  25.      *传入地图参数map,处理与子弹与地图的碰撞  
  26.      *传入敌方坦克参数mt,处理与子弹与我方坦克的碰撞 */  
  27.     public void logic(Map map,MainTank mt){   
  28.         if(this.isVisible()){   
  29.         checkCollidesWith(map);   
  30.         lastX = 0;   
  31.         lastY = 0;   
  32.         switch (dir) {   
  33.         case UP:   
  34.             this.setTransform(Sprite.TRANS_MIRROR_ROT90);   
  35.             this.nextFrame();   
  36.             this.move(0, -2);   
  37.             lastY = -2;   
  38.             break;   
  39.         case DOWN:   
  40.             this.setTransform(Sprite.TRANS_MIRROR_ROT270);   
  41.             this.nextFrame();   
  42.             this.move(0, 2);   
  43.             lastY = 2;   
  44.             break;   
  45.         case LEFT:   
  46.             this.setTransform(Sprite.TRANS_MIRROR);   
  47.             this.nextFrame();   
  48.             this.move(-2, 0);   
  49.             lastX = -2;   
  50.             break;   
  51.         case RIGHT:   
  52.             this.setTransform(Sprite.TRANS_NONE);   
  53.             this.nextFrame();   
  54.             this.move(2, 0);   
  55.             lastX = 2;   
  56.             break;   
  57.         }   
  58.         this.createBullets();//产生子弹   
  59.         setDir();//设置坦克方向的随机改变   
  60.         }   
  61.         for (int i = 0; i < bullets.length; i++) {   
  62.             bullets[i].move();   
  63.         }   
  64.         for (int i = 0; i < bullets.length; i++) {   
  65.             bullets[i].checkCollidesWith(map);   
  66.             bullets[i].checkCollidesWith(mt);   
  67.         }   
  68.         for (int i = 0; i < bullets.length; i++) {   
  69.             bullets[i].exp();   
  70.         }   
  71.     }   
  72.        
  73.     //设置坦克方向的随机改变的方法   
  74.     public void setDir() {   
  75.         int m = ran.nextInt(3);   
  76.         if (m != 0) {   
  77.             return;   
  78.         }   
  79.         int mx = this.getRefPixelX();   
  80.         int my = this.getRefPixelY();   
  81.         int gezix = 15 / 2;   
  82.         int geziy = 15 / 2;   
  83.         if (mx % 15 != gezix) {   
  84.             return;   
  85.         }   
  86.         if (my % 15 != geziy) {   
  87.             return;   
  88.         }   
  89.         this.dir = ran.nextInt(4);   
  90.     }   
  91.        
  92.     //产生子弹方法   
  93.     public void createBullets(){   
  94.         bCount++;   
  95.         //8次后再产生子弹   
  96.         if(bCount==8)   
  97.         {   
  98.         for (int i = 0; i < bullets.length; i++) {   
  99.             if (!bullets[i].isVisible()) {   
  100.                 bullets[i].setPosition(this.getRefPixelX(), this  
  101.                         .getRefPixelY());   
  102.                 bullets[i].setVisible(true);   
  103.                 bullets[i].s_exp.setVisible(false);   
  104.                 bullets[i].dir=this.dir;   
  105.                 bullets[i].launcher=Bullet.ET;   
  106.                 break;   
  107.             }   
  108.         }   
  109.         bCount=0;   
  110.         }   
  111.     }   
  112.        
  113. }   

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值