J2ME实战:蓝牙联网俄罗斯方块(4)—数据传输序列化与游戏地图存储模块( 未完待

来源:互联网 发布:apache httpd.exe闪退 编辑:程序博客网 时间:2024/05/22 20:56

1.数据传输序列化模块(Serialization接口)


在游戏的过程中需要将地图数据传输到远端玩家的手机上,故需进行数据的序列化和反序列化,因此我们这里定义了Serialization接口。

 

该接口的具体代码如下:

 

Java代码 复制代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package game.teris;   
  7. import java.io.*;   
  8.   
  9. /**  
  10.  *  
  11.  * @author dongdong  
  12.  */  
  13. public interface Serialization {   
  14.     public byte[] serialize() throws IOException;   
  15.     public void deserialize(byte[] data) throws IOException;   
  16.   
  17. }  

 

 

该接口中定义的serialize()方法和deserialize()方法会在游戏地图存储模块(TetrisMap)类中作具体的实现。定义Serialization接口的目的是为了对序列化进行规范,使所有的进行序列化传输的类都遵守相同的规则,并不是说不实现Serialization接口就不可以进行传输了。



2.游戏地图存储模块(TetrisMap类)



TetrisMap类提供了如下的功能:


a.通过mapdata[][]和mapBlockExist[]两个数组提供了对游戏地图在数组上的逻辑表示;

b.提供了对游戏地图中对应的方块的消除和添加算法;

c.提供了对方块的绘制方法paint(),供游戏逻辑控制模块TetrisCanvas类(tips:后续章节将讲到)调用,把方法绘制到屏幕上。


具体代码如下:


Java代码 复制代码
  1. /*  
  2.  * To change this template, choose Tools | Templates  
  3.  * and open the template in the editor.  
  4.  */  
  5.   
  6. package game.teris;   
  7.   
  8. import java.io.ByteArrayInputStream;   
  9. import java.io.ByteArrayOutputStream;   
  10. import java.io.DataInputStream;   
  11. import java.io.DataOutputStream;   
  12. import java.io.IOException;   
  13. import javax.microedition.lcdui.Font;   
  14. import javax.microedition.lcdui.Graphics;   
  15. import javax.microedition.media.MediaException;   
  16. import javax.microedition.media.Player;   
  17.   
  18. /**  
  19.  *  
  20.  * @author dongdong  
  21.  */  
  22. public class TetrisMap {   
  23.     int gamearea_x;   
  24.     int gamearea_y;   
  25.     int brick_Width;   
  26.     private TetrisCanvas canvas;   
  27.     private int[][] mapdata;   
  28.     private boolean[] mapBlockExist;   
  29.     private boolean _isSlave;   
  30.     private boolean isMaster;   
  31.     private int score;   
  32.     private int deleteRowNum;   
  33.     private Player player;   
  34.     private Font SCOREFONT;   
  35.     public TetrisMap(TetrisCanvas canvas, boolean _isMater) {   
  36.         this.canvas=canvas;   
  37.         //定义游戏地图为一个高16、宽12的数组,mapBlockExist代表每一行   
  38.         mapdata=new int[16][12];   
  39.         mapBlockExist=new boolean[16];   
  40.         setParameters(_isSlave);   
  41.     }   
  42.     public void setParameters(boolean _isMaster){   
  43.         isMaster=_isMaster;   
  44.         if(isMaster)   
  45.         {   
  46.             gamearea_x=canvas.GAMEAREA_X;   
  47.             gamearea_y=canvas.GAMEAREA_Y;   
  48.             brick_Width=canvas.BRICK_WIDTH;   
  49.         }else  
  50.         {   
  51.             gamearea_x=canvas.GAMEAREA_X_REMOTE;   
  52.             gamearea_y=canvas.GAMEAREA_Y_REMOTE;   
  53.             brick_Width=canvas.BRICK_WIDTH_REMOTE;   
  54.         }   
  55.     }   
  56.     public void init() {//初始化TetrisMap实例中mapdata和mapBlockExist数据   
  57.         //清除计分   
  58.         score=0;   
  59.         //先把全部元素清0   
  60.         for(int i=0; i<16; i++)   
  61.         {   
  62.             for(int j=0; j<12; j++)   
  63.             {   
  64.                 mapdata[i][j]=0;   
  65.             }   
  66.             mapBlockExist[i]=false;   
  67.         }   
  68.         //设置两堵墙   
  69.         for(int i=0; i<16; i++)   
  70.         {   
  71.             mapdata[i][0]=8;   
  72.             mapdata[i][11]=8;   
  73.         }   
  74.         //设置容器底   
  75.         for(int i=0;i<12;i++)   
  76.         {   
  77.             mapdata[15][i]=8;   
  78.         }   
  79.         mapBlockExist[15]=true;   
  80.            
  81.     }   
  82.   
  83.     public int get(int x, int y) {//地图数据的提取   
  84.         int data =mapdata[y][x];   
  85.         return data;   
  86.           
  87.     }   
  88.     public void  set(int x, int y, int val){   
  89.         if(x >= 0 && y >= 0)   
  90.         {   
  91.             mapdata[y][x]= val;   
  92.             mapBlockExist[y]=true;   
  93.         }   
  94.     }   
  95.     public void paint(Graphics g) {/*首先根据TetrisMap代表的是主屏还是附屏清处不同  
  96.      的区域然后绘制非运动砖块*/  
  97.         //清屏   
  98.         if(isMaster)   
  99.         {   
  100.             TetrisCanvas.clear(g);   
  101.         }   
  102.         else  
  103.         {   
  104.             TetrisCanvas.clear_Remote(g);   
  105.         }   
  106.         for(int i=0; i<16; i++)   
  107.         {   
  108.             for(int j=0; j<12; j++)   
  109.             {   
  110.                 if(mapdata[i][j] == 8)   
  111.                 {   
  112.                     block.drawBrick(gamearea_x + j * brick_Width,   
  113.                             gamearea_y + i * brick_Width, g,7);   
  114.                 }   
  115.             }   
  116.         }   
  117.            
  118.     }   
  119.     public boolean check(Graphics g, int row){   
  120.         boolean deleteFlag=false;   
  121.         deleteRowNum=0;   
  122.         //最多可以连销4行   
  123.         int tmpRowNo;   
  124.         if(row + 4>= 15)   
  125.         {   
  126.             tmpRowNo=15;   
  127.         }   
  128.         else{   
  129.             tmpRowNo=row+4;   
  130.         }   
  131.            
  132.         for(int y=row; y<tmpRowNo; y++)   
  133.         {   
  134.             boolean flag = true;   
  135.                
  136.             for(int x=1; x<11; x++)   
  137.             {   
  138.                 if(mapdata[y][x]==0)   
  139.                 {   
  140.                     //空白区   
  141.                     flag=false;   
  142.                 }   
  143.             }   
  144.             //需要消行   
  145.             if(flag)   
  146.             {   
  147.                 mapBlockExist[y] = false;   
  148.                 for(int x=1; x<11; x++)   
  149.                 {   
  150.                     mapdata[y][x] = 0;   
  151.                 }//这一行的地图数据全部置0   
  152.                    
  153.                 deleteRow(g,y);   
  154.                    
  155.                 deleteFlag=true;   
  156.                    
  157.                 deleteRowNum ++;   
  158.                    
  159.                 //加分   
  160.                 score += 10;   
  161.                 paintScore(g);   
  162.                 //发声   
  163.                 try{   
  164.                     if(player != null)   
  165.                     {   
  166.                         player.start();   
  167.                     }   
  168.                 }   
  169.                 catch (MediaException me){ }   
  170.             }   
  171.         }// end for   
  172.         return deleteFlag;   
  173.     }   
  174.   
  175.     public void deleteRow(Graphics g, int y) {//本地方法,用来将需要消去的行简单置黑   
  176.       g.setColor(TetrisCanvas.BACKGROUND);   
  177.       g.fillRect(gamearea_x + brick_Width, gamearea_y + y*brick_Width,    
  178.               10 * brick_Width, brick_Width);   
  179.     }   
  180.        
  181.     public void repaintMap(Graphics g){/*对mapdata和mapBlockExist的值进行检查,几行方块被消完  
  182.         上面的方块依次下降几行*/  
  183.         //从容器底开始   
  184.         for(int i =14; i>0;i--)   
  185.         {   
  186.             int tmp;   
  187.                
  188.             //有砖块的行才移动   
  189.             if(mapBlockExist[i]){   
  190.                 //只有下一行为空白行才进行移动   
  191.                 if(!mapBlockExist[i+1]){   
  192.                     tmp= i+1;   
  193.                        
  194.                     if(!mapBlockExist[i+2]){   
  195.                         tmp=i+2;   
  196.                            
  197.                         if(!mapBlockExist[i+3]){   
  198.                              tmp=i+3;   
  199.                         }//end if(!mapBlockExist[i+3])   
  200.                     }//end if(!mapBlockExist[i+2])   
  201.                     deleteRow(g,i);   
  202.                     //行复制   
  203.                     for(int j=1; j<11; j++){   
  204.                         mapdata[tmp][j] = mapdata[i][j];   
  205.                         mapdata[i][j] = 0;   
  206.                     }   
  207.                     mapBlockExist[i]= false;   
  208.                     mapBlockExist[tmp]= true;   
  209.                        
  210.                     drawBlock(g,tmp);   
  211.                 }//end  if(!mapBlockExist[i+1])   
  212.             }//end  if(!mapBlockExist[i])   
  213.         }//end for   
  214.            
  215.     }   
  216.        
  217.     public void repaintMap_Remote(Graphics g){/*负责远端屏幕绘制,非实时更新,仅本地有方块  
  218.      落下及消去时才被调用*/  
  219.         for(int i=15; i>0; i--)   
  220.         {   
  221.             drawBlockAll(g,i);   
  222.         }   
  223.         paintScore(g);   
  224.     }   
  225.   
  226.     public void drawBlock(Graphics g, int y) {//绘制主屏   
  227.        for(int x=1;x<11;x++)   
  228.        {   
  229.            if(mapdata[y][x]!=0)   
  230.            {   
  231.                block.drawBrick(gamearea_x + x*brick_Width,    
  232.                        gamearea_y + y*brick_Width,   
  233.                        g, mapdata[y][x] -1);   
  234.            }   
  235.        }   
  236.     }   
  237.   
  238.     public void drawBlockAll(Graphics g, int y) {//绘制附屏   
  239.         for(int x=1; x<11; x++)   
  240.         {   
  241.             if(mapdata[y][x] !=0)   
  242.             {   
  243.                 block.drawBrick(gamearea_x + x*brick_Width, gamearea_y + y*brick_Width,    
  244.                         g, mapdata[y][x] -1);   
  245.             }else  
  246.             {   
  247.                 g.setColor(TetrisCanvas.BACKGROUND);   
  248.                 g.fillRect(gamearea_x + x*brick_Width, gamearea_y + y*brick_Width,    
  249.                         brick_Width, brick_Width);   
  250.             }   
  251.         }   
  252.     }   
  253.   
  254.     private void paintScore(Graphics g) {//绘制分数   
  255.           
  256.         if(0 == score)   
  257.         {   
  258.             return;   
  259.         }   
  260.            
  261.         //清除记分牌   
  262.         g.setColor(TetrisCanvas.BACKGROUND);   
  263.         g.fillRect(gamearea_x + 12*brick_Width, gamearea_y + 6*brick_Width,    
  264.                 brick_Width * 7, brick_Width * 7);   
  265.         //计分   
  266.         g.setColor(02550);   
  267.         g.setFont(SCOREFONT);   
  268.         g.drawString("" + score,    
  269.                 gamearea_x + 14*brick_Width,   
  270.                 gamearea_y + 8*brick_Width,   
  271.                 g.TOP | g.HCENTER);   
  272.     }   
  273.        
  274.     public void caculateScore(){//计算分数   
  275.         score += deleteRowNum * 10;   
  276.     }   
  277.        
  278.      public byte[] serialize() throws IOException{/*实现Serialization接口的  
  279.          serialize()方法*/  
  280.             
  281.          ByteArrayOutputStream byteArrayOutStream = new  
  282.                  ByteArrayOutputStream();   
  283.          DataOutputStream dataOutputStream = new    
  284.                  DataOutputStream(byteArrayOutStream);   
  285.             
  286.          forint i=0; i<16; i++)   
  287.          {   
  288.              for(int j=0; j<12; j++)   
  289.              {   
  290.                  dataOutputStream.writeInt(mapdata[i][j]);   
  291.              }   
  292.          }   
  293.          dataOutputStream.writeInt(deleteRowNum);   
  294.             
  295.          return byteArrayOutStream.toByteArray();   
  296.             
  297.      }   
  298.         
  299.      public void deserialize(byte[] data) throws IOException{/*实现Serialization  
  300.       接口的deserialize()方法*/  
  301.          ByteArrayInputStream byteArrayInputStream = new    
  302.                  ByteArrayInputStream(data);   
  303.          DataInputStream dataInputStream = new    
  304.                  DataInputStream(byteArrayInputStream);   
  305.          for(int i=0; i<16; i++)   
  306.          {   
  307.              for(int j=0;j<12;j++)   
  308.              {   
  309.                  mapdata[i][j]= dataInputStream.readInt();   
  310.              }   
  311.          }   
  312.          deleteRowNum= dataInputStream.readInt();   
  313.          caculateScore();   
  314.             
  315.      }   
  316.   
  317. }  

 

http://supersun.javaeye.com/blog/261794

原创粉丝点击