e (Blocker != null) {
}
}
...
}
F. 程序逻辑:下面给出程序清单。程序中我们使用一个数组来存储方块的信息,一共有十九种,还有一个数组来存储当前的画面方格的内容.在程序中
有一个paint方法来实现重画,注意绘制的先后次序,当程序规模变得很大的时候,重画的效率就非常重要,需要进行优化.我们在程序中使用了背景,
在没有背景的情况下,程序仅5k,采用背景后,程序47k,可见对图片的优化至关重要.
/*
* TetrisCanvas.java
*
* Created on 2005年7月13日, 上午11:31
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package Tetris;
import java.util.*;
import java.lang.Math;
import javax.microedition.lcdui.*;
/**
*
* @author lin
*/
public class TetrisCanvas extends Canvas implements Runnable{
private Thread Blocker = null;
private Random generator;
private int FutureBlockType, BlockType,LastType,LastX,LastY,BlockX,BlockY ;
private int BlockLines,BlockScore;
private int BlockSpeed,CurSpeed;
private static final int COLOR_GRAY = 0x00eeeeee;
private static final int COLOR_RED = 0x00ff0000;
private static final int COLOR_BLACK = 0x00000000;
private static final int COLOR_WHITE = 0x00ffffff;
private static final int COLOR_BLUE = 0x000000ff;
private static final int COLOR_LIGHT_BLUE= 0x0089a5d1;
private static final int COLOR_DARK_GRAY = 0x00808080;
private static final int COLOR_BACKGROUND= COLOR_LIGHT_BLUE;
private static final int BLOCK_SIZE = 7;
private static final int CANVAS_SIZE_WIDTH = 12;
private static final int CANVAS_SIZE_HEIGHT = 22;
private static final int CANVAS_OFFSET_X = 5;
private static final int CANVAS_OFFSET_Y = 7;
/**
* The paint status.
*/
boolean ISCLEAR = false;
boolean ISDOWN = false;
boolean ISDEL = false;
/**
* the block information matrix.
*/
int BlockInfo[][]={{1,0,1,1,1,2,1,3,0xff0000,2},
{0,1,1,1,2,1,3,1,0xff0000,4},
{0,0,0,1,1,1,1,2,0x0000ff,2},
{0,1,1,0,1,1,2,0,0x0000ff,3},
{0,1,0,2,1,0,1,1,0x00ff00,2},
{0,0,1,0,1,1,2,1,0x00ff00,3},
{0,0,0,1,1,0,1,1,0xffff00,2},
{0,1,1,0,1,1,1,2,0x00ffff,2},
{0,1,1,0,1,1,2,1,0x00ffff,3},
{1,0,1,1,1,2,2,1,0x00ffff,3},
{0,1,1,1,1,2,2,1,0x00ffff,3},
{0,1,0,2,1,1,2,1,0xff00ff,3},
{0,0,1,0,1,1,1,2,0xff00ff,3},
{0,1,1,1,2,0,2,1,0xff00ff,3},
{1,0,1,1,1,2,2,2,0xff00ff,3},
{0,0,0,1,1,1,2,1,0xffffff,3},
{1,0,1,1,1,2,2,0,0xffffff,3},
{0,1,1,1,2,1,2,2,0xffffff,3},
{0,2,1,0,1,1,1,2,0xffffff,3},
};
// Gridmatrix 中只存储颜色信息
int Gridmatrix[][]=new int[CANVAS_SIZE_HEIGHT][CANVAS_SIZE_WIDTH];
/**
* Initialize the applet. Resize and load images.
*/
public void init() {
BlockType=Math.abs(generator.nextInt()%19);
FutureBlockType=Math.abs(generator.nextInt()%19);
LastType=BlockType;
BlockLines=0;
BlockScore=0;
BlockSpeed=1;
CurSpeed=BlockSpeed;
BlockX=4; LastX=BlockX;
BlockY=0; LastY=BlockY;
//初始化Gridmatrix矩阵,内容为带边框的主绘图区。
for(int i=0;i
for(int j=0;j
Gridmatrix[i][j]=0;
for(int i=0;i
Gridmatrix[CANVAS_SIZE_HEIGHT-1][i]=COLOR_DARK_GRAY;
for(int i
|