⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetrismap.java

📁 这是我们学校教的j2me程序开发实例从入门到精通自带关盘的源代码
💻 JAVA
字号:
package game.tetris;




import javax.microedition.lcdui.*;

import java.io.*;
import javax.microedition.media.*;


/**
 * 游戏地图,地图高16个小砖块,宽16小砖块,但是游戏容器高16,宽12(包括左右2堵墙)
 * 所以容器的内直径为10
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author: an unknown Japanese,Jagie
 * @version 1.0
 */

public class TetrisMap implements Serialization{

  //地图数据
  protected int mapdata[][];
  protected boolean mapBlockExist[]; //长度为16的boolean数组,如果mapBlockExist[i]=true,则第i+1行有砖块
  public int score; //分数
  public static final Font SCOREFONT = Font.getFont(Font.FACE_SYSTEM,
      Font.STYLE_PLAIN,
      Font.SIZE_LARGE);
  
  public TetrisCanvas canvas;
  
  public int gamearea_x;
  public int gamearea_y;
  public int brick_Width;
  public boolean isMaster;
  
  public TetrisBlock block;

  public static Player player;

  static {
    try {
      InputStream is =
          TetrisMap.class.getResourceAsStream("/chimes.wav");      
      player = Manager.createPlayer(is, "audio/x-wav");
    }
    catch (IOException ioe) {
      ioe.printStackTrace();
    }
    catch (MediaException me) {
      me.printStackTrace();
    }
  }

  public TetrisMap( TetrisCanvas canvas , boolean _isMaster) {
	  
	this.canvas = canvas;	  
    mapdata = new int[16][12];
    mapBlockExist = new boolean[16];    
    setParameters(_isMaster);

  }
  
  public void setParameters( boolean _isMaster)
  {
	  isMaster = _isMaster;
	  
	  if( isMaster )
	  {
		  gamearea_x = canvas.GAMEAREA_X;
		  gamearea_y = canvas.GAMEAREA_Y;
		  brick_Width = canvas.BRICK_WIDTH;  
	  }else
	  {
		  gamearea_x = canvas.GAMEAREA_X_REMOTE;
		  gamearea_y = canvas.GAMEAREA_Y_REMOTE;
		  brick_Width = canvas.BRICK_WIDTH_REMOTE;		  
		  //outputParameters();		  
	  }
  }
  
  public void outputParameters()
  {
	  bug.println("map_2 parameters:");
	  bug.println("gamearea_x = " + gamearea_x);
	  bug.println("gamearea_y = " + gamearea_y);			  
	  bug.println("brick_Width = "+ brick_Width);
  }
  
  public void setTetrisBlock( TetrisBlock block )
  {
	  this.block = block;
  }
  

  public void init() {
    //清除计分
    score = 0;
    //先把全部元素清0
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 12; j++) {
        mapdata[i][j] = 0;
      }
      mapBlockExist[i] = false;
    }

    //设置2堵墙
    for (int i = 0; i < 16; i++) {
      mapdata[i][0] = 8;
      mapdata[i][11] = 8;
    }

    //设置容器底
    for (int i = 0; i < 12; i++) {
      mapdata[15][i] = 8;
    }

    mapBlockExist[15] = true;
  }

  /**
   * 获取地图某行某列的数据
   * @param x int 行号
   * @param y int 列号
   * @return int 地图数据,非0表示有砖块
   */
  public int get(int x, int y) {
    int data = mapdata[y][x];

    return data;
  }

  /* 设置地图数据 */
  public void set(int x, int y, int val) {
    if (x >= 0 && y >= 0) {
      mapdata[y][x] = val;
      mapBlockExist[y] = true;
    }

  }

  /**
   * 该方法其实只负责非运动砖块
   * @param g Graphics
   * 本地、远端用户均需调用,
   * 绘制的是墙
   */
  
  public void paint(Graphics g) {
    //清屏
	  if( isMaster )
	  {
		  TetrisCanvas.clear(g);
	  }
	  else 
	  {
		  TetrisCanvas.clear_Remote(g);
	  }
    

    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 12; j++) {
        if (mapdata[i][j] == 8) {        	
            block.drawBrick(gamearea_x + j * brick_Width,
                    gamearea_y + i * brick_Width, g, 7);
        }
      }
    }
  }

  

  /*
   * 本地用户方法
   */
  
  int deleteRowNum ;
  
  /*
   * 检测是否需要消行,
   * 需要返回true,
   * 否则返回假
   */
  /*
   * 本地方法
   */
  public boolean check(Graphics g, int row) {
    boolean deleteFlag = false;

    deleteRowNum = 0;
    //最多可以连消4行
    int tmpRowNo;
    if (row + 4 >= 15) {
      tmpRowNo = 15;
    }
    else {
      tmpRowNo = row + 4;
    }

    for (int y = row; y < tmpRowNo; y++) {
      boolean flag = true;

      for (int x = 1; x < 11; x++) {
        if (mapdata[y][x] == 0) { /* 空白区 */
          flag = false;
        }
      }//这个循环的是一行,

      /* 需要消行 */
      if (flag) {
        mapBlockExist[y] = false;
        for (int x = 1; x < 11; x++) {
          mapdata[y][x] = 0;
        }//这一行的mapdata全部置为0

        deleteRow(g, y);

        deleteFlag = true;
        
        deleteRowNum ++ ;
        
        //加分
        score += 10;
        paintScore(g);
        //发声
        try {
          if (player != null) {
            player.start();
          }

        }
        catch (MediaException me) {
          
        }

      }
    }//end for

    return deleteFlag;
  }

  //删除行,只是简单的把该行置黑
  /*
   * 本地用户方法
   */
  protected void deleteRow(Graphics g, int y) {
    g.setColor(TetrisCanvas.BACKGROUND);
    g.fillRect(gamearea_x + brick_Width,
               gamearea_y + y * brick_Width,
               10 * brick_Width, brick_Width);

  }

  /* 该方法在有消去行为后调用 */
  /*
   * 本地用户方法
   */
  public void repaintMap(Graphics g) {
    //从容启底开始
    for (int i = 14; i > 0; i--) {
      int tmp;

      //有砖块的行才移动
      if (mapBlockExist[i]) {
        //只有下一行为空白行才进行移动
        if (!mapBlockExist[i + 1]) {
          tmp = i + 1;

          if (!mapBlockExist[i + 2]) {
            tmp = i + 2;

            if (!mapBlockExist[i + 3]) {
              tmp = i + 3;
            }//end if(!mapBlockExist[i+3])
          }//end if(!mapBlockExist[i+2])
          deleteRow(g, i);
          //行复制
          for (int j = 1; j < 11; j++) {
            mapdata[tmp][j] = mapdata[i][j];
            mapdata[i][j] = 0;
          }
          mapBlockExist[i] = false;
          mapBlockExist[tmp] = true;

          drawBlock(g, tmp);
        }// end if(!mapBlockExist[i+1])
      }//end if(mapBlockExist[i])
    }//end for
    
    outputMapdata();
  }
  
  /*
   * 远端用户方法
   */
  public void repaintMap_Remote( Graphics g )
  {  
	  for(int i = 15; i > 0; i--) 
	  {
		  drawBlockAll(g, i);
	  } 
	  paintScore(g);
  }
  
  /*
   * 远端用户方法
   */
  public void drawBlockAll(Graphics g,int y)
  {
	  
//	  outputParameters();
	  
	    for (int x = 1; x < 11; x++) {
		      if (mapdata[y][x] != 0) {
		        block.drawBrick(gamearea_x + x * brick_Width,
		                              gamearea_y + y * brick_Width, g,
		                              mapdata[y][x] - 1);
		      }
		      else
		      {
		    	  g.setColor(TetrisCanvas.BACKGROUND);
			      g.fillRect(gamearea_x + x * brick_Width,
			                  			gamearea_y + y * brick_Width,
			                       brick_Width, brick_Width);
		      }
	    }
  }
  
  
  
  /*
   * 本地方法,
   * 远端用户不会调用它
   */
  public void drawBlock(Graphics g, int y) {
    for (int x = 1; x < 11; x++) {
      if (mapdata[y][x] != 0) {
        block.drawBrick(gamearea_x +
                              x * brick_Width,
                              gamearea_y +
                              y * brick_Width, g,
                              mapdata[y][x] - 1);

      }
    }
  }
  


  /*
   * 应该本地和远端用户都可以调用
   */
  private void paintScore(Graphics g) {
	  
	  if( 0 == score )
	  {
		  return;
	  }
	  
    //清除记分牌
    g.setColor(TetrisCanvas.BACKGROUND);
    g.fillRect(gamearea_x + 12 * brick_Width,
               gamearea_y + 6 * brick_Width,
               brick_Width * 7, brick_Width * 7);
    //计分
    g.setColor(0, 255, 0);
    g.setFont(SCOREFONT);
    g.drawString("" + score,
                 gamearea_x + 14 * brick_Width,
                 gamearea_y + 8 * brick_Width,
                 g.TOP | g.HCENTER);
  }
  
  public void outputMapdata()
  {
	  for( int i = 0; i < 16; i++ )
	  {
		  System.out.println("line "+i+":");
		  
		  for( int j = 0 ; j < 12; j++)
		  {
			  System.out.print( mapdata[i][j] + ", ");			                                    
		  }  
		  
		  System.out.println();
	  }
  }
  
  public void outputdeleteRowNum()
  {
	  bug.println("deleteRowNum = " + deleteRowNum );
  }


  public void caculateScore()
  {
  	score += deleteRowNum * 10;
  }

public void deserialize(byte[] data) throws IOException {
	
	ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
	
	DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);

	  for( int i = 0; i < 16; i++ )
	  {  
		  for( int j = 0 ; j < 12; j++)
		  {
			  mapdata[i][j] = dataInputStream.readInt();
		  }
	  }
	  
	  deleteRowNum = dataInputStream.readInt();
	  
	  caculateScore();
	  outputMapdata();
	  outputdeleteRowNum();
}

public byte[] serialize() throws IOException {
	
	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	
	DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
			
	  for( int i = 0; i < 16; i++ )
	  {  
		  for( int j = 0 ; j < 12; j++)
		  {
			  dataOutputStream.writeInt(mapdata[i][j]);
		  }  
	  }
	  
	  dataOutputStream.writeInt(deleteRowNum);
	
	  outputMapdata();
	  
	  outputdeleteRowNum();
	  
	return byteArrayOutputStream.toByteArray();
	
}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -