?? tetrismap.java
字號(hào):
package game.tetris;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.media.*;
/**
* 游戲地圖,地圖高16個(gè)小磚塊,寬16小磚塊,但是游戲容器高16,寬12(包括左右2堵墻)
* 所以容器的內(nèi)直徑為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{
//地圖數(shù)據(jù)
protected int mapdata[][];
protected boolean mapBlockExist[]; //長度為16的boolean數(shù)組,如果mapBlockExist[i]=true,則第i+1行有磚塊
public int score; //分?jǐn)?shù)
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() {
//清除計(jì)分
score = 0;
//先把全部元素清0
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 12; j++) {
mapdata[i][j] = 0;
}
mapBlockExist[i] = false;
}
//設(shè)置2堵墻
for (int i = 0; i < 16; i++) {
mapdata[i][0] = 8;
mapdata[i][11] = 8;
}
//設(shè)置容器底
for (int i = 0; i < 12; i++) {
mapdata[15][i] = 8;
}
mapBlockExist[15] = true;
}
/**
* 獲取地圖某行某列的數(shù)據(jù)
* @param x int 行號(hào)
* @param y int 列號(hào)
* @return int 地圖數(shù)據(jù),非0表示有磚塊
*/
public int get(int x, int y) {
int data = mapdata[y][x];
return data;
}
/* 設(shè)置地圖數(shù)據(jù) */
public void set(int x, int y, int val) {
if (x >= 0 && y >= 0) {
mapdata[y][x] = val;
mapBlockExist[y] = true;
}
}
/**
* 該方法其實(shí)只負(fù)責(zé)非運(yùn)動(dòng)磚塊
* @param g Graphics
* 本地、遠(yuǎn)端用戶均需調(diào)用,
* 繪制的是墻
*/
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) { /* 空白區(qū) */
flag = false;
}
}//這個(gè)循環(huán)的是一行,
/* 需要消行 */
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);
//發(fā)聲
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);
}
/* 該方法在有消去行為后調(diào)用 */
/*
* 本地用戶方法
*/
public void repaintMap(Graphics g) {
//從容啟底開始
for (int i = 14; i > 0; i--) {
int tmp;
//有磚塊的行才移動(dòng)
if (mapBlockExist[i]) {
//只有下一行為空白行才進(jìn)行移動(dòng)
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);
//行復(fù)制
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();
}
/*
* 遠(yuǎn)端用戶方法
*/
public void repaintMap_Remote( Graphics g )
{
for(int i = 15; i > 0; i--)
{
drawBlockAll(g, i);
}
paintScore(g);
}
/*
* 遠(yuǎn)端用戶方法
*/
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);
}
}
}
/*
* 本地方法,
* 遠(yuǎn)端用戶不會(huì)調(diào)用它
*/
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);
}
}
}
/*
* 應(yīng)該本地和遠(yuǎn)端用戶都可以調(diào)用
*/
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);
//計(jì)分
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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -