/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tetris;
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
/**
* @author Administrator
*/
public class tetris extends MIDlet {
GamePanel tetrisGame;
Display display;
public tetris(){
tetrisGame=new GamePanel(this);
display=Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(tetrisGame);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class GamePanel extends GameCanvas implements Runnable,CommandListener{
GridPanel[][] gridpanel;
private int randomnum;
private int s_width;
private int s_height;
private int grid_width;
private int grid_height;
private int grid_x=10;
private int grid_y=20;
private int point_x=0,point_y=0;
private int movevalue_LR=0,movevalue_UD=0;
private int STARTVALUE=0,CHANGESTEP=1;
private Graphics publicGraphics;
private Command command_Exit,command_Resume;
public static int color_Stick=0xCC3333;
public static int color_Rect=0xFFFF99;
public static int color_T=0x9933CC;
public static int color_Z_L=0x009900;
public static int color_Z_R=0x0033CC;
public static int color_L_L=0x3399FF;
public static int color_L_R=0xCCFF66;
String[] rect_Name={"T","Z_L","Z_R","L_L","L_R","Rect","Stick"};
Random random;
boolean roundover,bandover=false,stop=false;
tetris RussiaTetris;
Matrix matrix;
Thread thread;
public void paint(Graphics g){
g.setClip(0, 0, s_width, s_height);
g.setColor(0xFFFFFF);
g.fillRect(0, 0, s_width, s_height);
g.setColor(211, 208, 192);
for( int i=0; i<=grid_x; i++ ) // ||| //画 map_y+1条竖线
{
g.drawLine(i*grid_width, 0, i*grid_width, grid_width*grid_y);
}
for( int i=0; i<=grid_y; i++ ) // === //画 map_x+1条横线
{
g.drawLine(0, i*grid_height, grid_height*grid_x, i*grid_height);
}
g.drawRect(0, 0, grid_x*grid_width, grid_y*grid_height);
for(int i=0;i<grid_x;i++){
for(int j=0;j<grid_y;j++){
switch(gridpanel[i][j].status){
case 0:
g.setColor(0xFFFFFF);
g.fillRect(i*grid_width+1,j*grid_height+1, grid_width-1, grid_height-1);
break;
case 2:
g.setColor(0x0000FF);
g.fillRect(i*grid_width+1,j*grid_height+1, grid_width-1, grid_height-1);
break;
case 3:
g.setColor(0x0000FF);
g.fillRect(i*grid_width+1,j*grid_height+1, grid_width-1, grid_height-1);
break;
}
}
}
}
public GamePanel(tetris model){
super(true);
setFullScreenMode(true);
s_width=this.getWidth();
s_height=this.getHeight()-5;
grid_height=s_height/20;
grid_width=grid_height;
gridpanel=new GridPanel[grid_x][grid_y];
thread=new Thread(this);
roundover=true;
RussiaTetris=model;
random=new Random(System.currentTimeMillis());
matrix=new Matrix(this);
command_Exit=new Command("Exit",Command.BACK,1);
command_Resume=new Command("Pause",Command.ITEM,10);
addCommand(command_Resume);
addCommand(command_Exit);
addCommand(new Command("fff",Command.ITEM,10));
setCommandListener(this);
for(int i=0;i<grid_x;i++){
for(int j=0;j<grid_y;j++){
gridpanel[i][j]=new GridPanel();
gridpanel[i][j].setStatus(0);
}
}
thread.start();
}
public void tetrisTransform(){
if(roundover){
optionJudge();
randomnum=Math.abs(random.nextInt())%7;
gridpanel=matrix.createMatrix(gridpanel,rect_Name[randomnum], 3, 0, 0);
roundover=false;
}
commonRun();
}
public void run(){
while(true){
while(!stop){
try{
tetrisTransform();
repaint();
thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void commonRun(){
int i=0,j=0;
boolean finished=false,once=true,left_Border=false,right_Border=false;
for(j=grid_y-1;j>=0;j--){
if(roundover)
break;
for(i=0;i<grid_x;i++){
if(gridpanel[i][j].getStatus()==3){
if(i==0){
bandover=true;
left_Border=true;
}
else if(i==grid_x-1){
bandover=true;
right_Border=true;
}
else if(gridpanel[i-1][j].getStatus()==2){
bandover=true;
left_Border=true;
}
else if(gridpanel[i+1][j].getStatus()==2){
bandover=true;
right_Border=true;
}
if(j==grid_y-1){
gridpanel[i][j].setStatus(2);
roundover=true;
break;
}
else if(gridpanel[i][j+1].getStatus()==2&&j<grid_y-1){
gridpanel[i][j].setStatus(2);
roundover=true;
break;
}
}
}
}
for(j=grid_y-1;j>=0;j--){
for(i=STARTVALUE;i<grid_x&&i>=0;i=i+CHANGESTEP){
if(gridpanel[i][j].getStatus()==3){
if(roundover){
gridpanel[i][j].setStatus(2);
}
else{
if(bandover&&movevalue_LR<0&&left_Border){
movevalue_LR=0;
}
else if(bandover&&movevalue_LR>0&&right_Border){
movevalue_LR=0;
}
gridpanel[i][j].setStatus(0);
gridpanel[i+movevalue_LR][j+movevalue_UD+1].setStatus(3);
}
}
}
}
matrix.s_x=matrix.s_x+movevalue_LR;
matrix.s_y=matrix.s_y+movevalue_UD+1;
repaint();
movevalue_LR=0;
movevalue_UD=0;
bandover=false;
}
private void optionJudge(){
boolean status=false,failed=false;
for(int j=grid_y-1;j>=0;j--){
for(int i=0;i<grid_x;i++){
if(gridpanel[i][j].getStatus()==0){
failed=true;
}
else if(gridpanel[i][j].getStatus()==2){
status=true;
}
}
if(failed&&status){
gridpanel[0][j].setline_Status("stability");
roundJudge(j);
}
else{
gridpanel[0][j].setline_Status("empty");
clearLine(j);
}
failed=false;
status=false;
}
}
private void roundJudge(int lineNumber){
if(lineNumber==grid_y-1){
return;
}
else if(gridpanel[0][lineNumber+1].getlline_Status().equals("empty")){
for(int i=0;i<grid_x;i++){
gridpanel[i][lineNumber+1].setStatus(gridpanel[i][lineNumber].getStatus());
}
for(int j=0;j<grid_x;j++){
gridpanel[j][lineNumber].setStatus(0);
}
gridpanel[0][lineNumber].setline_Status("empty");
// clearLine(lineNumber);
gridpanel[0][lineNumber+1].setline_Status("stability");
lineNumber++;
roundJudge(lineNumber);
}
else if(gridpanel[0][lineNumber+1].getlline_Status().equals("stability")){
return;
}
}
private void clearLine(int line_Number){
for(int i=0;i<grid_x;i++){
gridpanel[i][line_Number].setStatus(0);
gridpanel[i][line_Number].setline_Status("empty");
}
}
protected void keyPressed(int key){
try {
switch (key) {
case KEY_NUM0:
move("LEFT");
break;
case KEY_NUM6:
move("RIGHT");
break;
case KEY_NUM2:
move("ROTATE");
break;
case KEY_NUM5:
move("DOWN");
break;
case KEY_NUM1:
thread.join();
break;
case KEY_NUM8:
stop=!stop;
break;
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
public void move(String direction){
if(direction.equals("LEFT")){
movevalue_UD=-1;
movevalue_LR = -1;
STARTVALUE=0;
CHANGESTEP=1;
commonRun();
}
else if(direction.equals("RIGHT")){
movevalue_UD = -1;
movevalue_LR = 1;
STARTVALUE=grid_x-1;
CHANGESTEP=-1;
commonRun();
}
else if(direction.equals("ROTATE")){
boolean allowed=true;
if(matrix.matrix_Name.equals("Stick")&&matrix.s_x>grid_x-4){
allowed=false;
}
if(matrix.s_x>=0&&allowed){
gridpanel=matrix.rotate(gridpanel);
}
repaint();
}
else if(direction.equals("DOWN")){
try {
thread.wait();
repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
class Matrix{
String matrix_Name;
private int s_x,s_y,matrix_Location;
boolean[][] matrix_Record;
GridPanel[][] copy_Use;
GamePanel panel;
Graphics draw;
public Matrix(GamePanel gamepanel){
copy_Use=new GridPanel[3][2];
panel=gamepanel;
for(int j=0;j<2;j++){
for(int i=0;i<3;i++){
copy_Use[i][j]=new GridPanel();
}
}
}
GridPanel[][] createMatrix(GridPanel[][] gridpanel,String name,int x,int y,int location){
matrix_Name=name;
matrix_Location=location;
s_x=x;
s_y=y;
if(name.equals("T")){
generate(gridpanel,2,3,GamePanel.color_T);
gridpanel[s_x][s_y].setStatus(0);
gridpanel[s_x+2][s_y].setStatus(0);
copy(gridpanel);
}
else if(name.equals("Z_L")){
generate(gridpanel,2,3,GamePanel.color_Z_L);
gridpanel[s_x][s_y].setStatus(0);
gridpanel[s_x+2][s_y+1].setStatus(0);
copy(gridpanel);
}
else if(name.equals("Z_R")){
generate(gridpanel,2,3,GamePanel.color_Z_R);
gridpanel[s_x+2][s_y].setStatus(0);
gridpanel[s_x][s_y+1].setStatus(0);
copy(gridpanel);
}
else if(name.equals("L_L")){
generate(gridpanel,2,3,GamePanel.color_L_L);
gridpanel[s_x+1][s_y+1].setStatus(0);
gridpanel[s_x+2][s_y+1].setStatus(0);
copy(gridpanel);
}
else if(name.equals("L_R")){
generate(gridpanel,2,3,GamePanel.color_L_R);
gridpanel[s_x][s_y+1].setStatus(0);
gridpanel[s_x+1][s_y+1].setStatus(0);
copy(gridpanel);
}
else if(name.equals("Rect")){
generate(gridpanel,2,2,GamePanel.color_Rect);
}
else if(name.equals("Stick")){
generate(gridpanel,1,4,GamePanel.color_Rect);
}
//s_x=x+1;
//s_y=y+1;
return gridpanel;
}
private void copy(GamePanel.GridPanel[][] gridpanel) {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 3; i++) {
copy_Use[i][j].setStatus(gridpanel[i + s_x][j + s_y].getStatus());
//gridpanel[i + s_x][j + s_y].setStatus(0);
}
}
}
private void generate(GridPanel[][] gridpanel,int height,int width,int color_Code) {
for(int i=s_x;i<s_x+width;i++){
for(int j=s_y;j<s_y+height;j++){
gridpanel[i][j].setStatus(3);
gridpanel[i][j].setColor_Code(color_Code);
}
}
}
public GridPanel[][] rotate(GridPanel[][] gridpanel){
if(matrix_Name.equals("Rect")){
return gridpanel;
}
else if(matrix_Name.equals("Stick")){
if(matrix_Location==0){
for(int i=0;i<4;i++)
{ gridpanel[s_x+i][s_y].setStatus(0);
gridpanel[s_x+1][s_y-1+i].setStatus(3);
}
matrix_Location++;
}
else {
for(int i=0;i<4;i++){
gridpanel[s_x+1][s_y-1+i].setStatus(0);
gridpanel[s_x+i][s_y].setStatus(3);
}
matrix_Location--;
}
}
else{
for(int j=s_y;j<s_y+3;j++){
for(int i=s_x;i<s_x+3;i++){
if(gridpanel[i][j].getStatus()==2){
}
else{
gridpanel[i][j].setStatus(0);
}
}
}
for(int j=0;j<2;j++){
for(int i=0;i<3;i++){
switch(matrix_Location){
case 0:
gridpanel[s_x+2-j][s_y+i].setStatus(copy_Use[i][j].getStatus());
break;
case 1:
gridpanel[s_x+2-i][s_y+2-j].setStatus(copy_Use[i][j].getStatus());
break;
case 2:
gridpanel[s_x+j][s_y+2-i].setStatus(copy_Use[i][j].getStatus());
break;
case 3:
gridpanel[s_x+i][s_y+j].setStatus(copy_Use[i][j].getStatus());
break;
}
}
}
if(matrix_Name.equals("Z_L")|matrix_Name.equals("Z_R")){
matrix_Location=matrix_Location==0 ? 3:0;
}
else{
this.matrix_Location=(this.matrix_Location+1)%4;
}
}
return gridpanel;
}
}
class GridPanel{
int status,color_Code=0x000000;
String line_Status;
public void setColor_Code(int value){
color_Code=value;
}
public int getStatus(){
return status;
}
public void setStatus(int value){
status=value;
}
public String getlline_Status(){
return line_Status;
}
public void setline_Status(String value){
line_Status=value;
}
}
public void commandAction(Command arg0, Displayable arg1) {
stop=true;
if(arg0==command_Exit){
RussiaTetris.destroyApp(false);
RussiaTetris.notifyDestroyed();
}
else if(arg0==command_Resume){
RussiaTetris.startApp();
}
}
}