学习J2ME写了一个俄罗斯方块的半成品,贴上来大家批评

用MIDP 2.0的Nokia S60 J2ME开发包在Eclipse下面写的,感叹啊,几百年没有用Java发现Eclipse居然这么好用。写了写J2ME用了GameCanvas,Sprite等类型,好用啊,不过也感觉到要精通需要花些功夫的。

游戏实现了一般Tetris的功能,方块的图片是某著名门户的游戏里面抓来的写了一半一些Bug和未完成的功能懒得改了,包括没有退出程序的功能。贴出代码大家见笑了

package com;
import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class TetrisKing extends MIDlet implements CommandListener {
 private Display display;
 public TetrisKing() {
  super();
  // TODO 自动生成构造函数存根
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  exit();
 }
 private void exit() throws MIDletStateChangeException {
  System.gc();
  destroyApp(false);
  notifyDestroyed();
 }
 protected void startApp() throws MIDletStateChangeException {
  display = Display.getDisplay(this);
  TetrisGame gameCanvas;
  try {
   gameCanvas = new TetrisGame();
   gameCanvas.start();
   display.setCurrent(gameCanvas);
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
 }
 public Display getDisplay() {
  return display;
 }
 protected void pauseApp() {
 }
 public void commandAction(Command arg0, Displayable arg1) {
 }
}

---------------------------
package com;
import java.io.IOException;

import java.util.Random;
import java.lang.Math;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Graphics;

public class TetrisGame extends GameCanvas implements Runnable, CommandListener{
        //CONSTANTS DEFINITION
        private Image arrObjImages[] = {
          Image.createImage("/obj1.png"),
          Image.createImage("/obj2.png"),
          Image.createImage("/obj3.png"),
          Image.createImage("/obj4.png"),
          Image.createImage("/obj5.png"),
          Image.createImage("/obj6.png"),
          Image.createImage("/obj7.png"),
        };
       
        private Image ObjDropObjImage = Image.createImage("/dobj.png");
       
        //共七对象,1棒子、2方块、3左折、4右折、5左横、6右横、7三角
        private static int OBJSMETR[][] = {
        //参数一高,参数二宽,参数三游戏面板中起始的位置,均以块为单位
          {1, 4, 4}, //[0]:1棒子
          {2, 2, 5}, //[1]:2方块
          {2, 3, 5}, //[2]:3左折
          {2, 3, 5}, //[3]:4右折
          {2, 3, 5}, //[4]:5左横
          {2, 3, 5}, //[4]:5左横
          {2, 3, 5}, //[4]:6右横
          {2, 3, 5}, //[4]:7三角
        };
   
        private static final int OBJSIZE = 9;
        private static final int OBJCNTHOR = 12;
        private static final int OBJCNTVER = 15;
        private static final int PANELLEFT = 5;
        private static final int PANELTOP = 5;
        private static final int PANELWIDTH = (OBJCNTHOR*OBJSIZE);
        private static final int PANELHEIGHT = (OBJCNTVER*OBJSIZE);

        private static final int NXTPANELLEFT = 118;
        private static final int NXTPANELTOP = 39;

        private static final int MINOBJSIZE = 2;
        private static final int USR1PANELLEFT = 121;
        private static final int USR1PANELTOP = 5;
        private static final int USR2PANELLEFT = 148;
        private static final int USR2PANELTOP = 5;
        private static final int USRPANELWIDTH = (OBJCNTHOR*MINOBJSIZE);
        private static final int USRPANELHEIGHT = (OBJCNTVER*MINOBJSIZE);
       
        private static final int BTNSTARTLEFT = 123;
        private static final int BTNSTARTTOP = 124;

        private int intDropTime = 500;
        private int intDelayTime = 100;
        //CONSTANTS DEFINITION
       
        private int dropcount;
        private int randint, randint2;
        private int bottomdelay; //到底之后可以再左右移动下再固定
       
        private boolean playingflag;// Game Loop runs when isPlay is true
        private int gamestate;
        private int objstate;
        private int gamedroplevelcnt;
        private boolean objmapindex[][] = new boolean[OBJCNTHOR][OBJCNTVER];

        //共七对象,1棒子、2方块、3左折、4右折、5左横、6右横、7三角
        private static final boolean arrSmapindex[][][] = {
          {{true, true, true, true}},
          {{true, true}, {true, true}},
          {{true, true, false}, {false, true, true}},
          {{false, true, true}, {true, true, false}},
          {{true, true, true}, {false, false, true}},
          {{true, true, true}, {true, false, false}},
          {{false, true, false}, {true, true, true}}
        };
               
        private Command exitCommand;
        private Command startCommand;
        private Random objrand = new Random();
        private Image imgNextPanel = Image.createImage("/next.png");

        //private Sprite CubeT; //for sprite testing
        private Sprite fallingobj;
        private int objindex;
        private int objdir; //0,1,2,3
        private int objx, objy;
        private int objindexx, objindexy;
        private int offsetx, offsety;

        protected TetrisGame() throws IOException{
          super(true);

          exitCommand = new Command("Close Me", Command.EXIT, 2);
          addCommand(exitCommand);
          startCommand = new Command("Start the Game", Command.OK, 1);
          addCommand(startCommand);
          setCommandListener(this);

          dropcount = 0;
          gamestate = -1;//<0 means game not started
          objstate = -1;//game object falling state
        }

        public void start(){
          playingflag = true;
          Thread t = new Thread(this);
          t.start();
        }
       
        public void stop(){
          //playingflag = false;
        }
       
        public void run(){
          Graphics g = getGraphics();
          drawScreen(g);
          while(playingflag == true){
            input(g);
            //System.out.println("Before drawGameObjs");
            if(gamestate==0){
              drawGameObjs(g);
              //System.out.println("After drawGameObjs");
              if(IsAtBottom() == true){
                System.out.println("Judging bottom");
                if(bottomdelay==0 && dropcount<(intDropTime/intDelayTime)){
                  System.out.println("bottomdelay = 1");
                 bottomdelay = 1;
                }else{
                  ObjDropDown();
                  objindexx = objindexy = objx = objy = 0;
                  objstate = 1;
                  System.out.println("At the bottom, set outstate to 1");
                }
              }
            }
            try{ Thread.sleep(intDelayTime);
            }catch (InterruptedException ie){
              System.out.println("Thread sleep exception");
            }
          }
        }

        private void drawPanel(Graphics g){
          g.setColor(0);
          g.fillRect(PANELLEFT-1, PANELTOP-1, PANELWIDTH+2, PANELHEIGHT+2);

          for(int i=0; i<OBJCNTHOR; i++){
            for(int j=0; j<OBJCNTVER; j++){
              if(objmapindex[i][j]==true){
                g.drawImage(ObjDropObjImage, PANELLEFT+i*OBJSIZE, PANELTOP+j*OBJSIZE, Graphics.TOP|Graphics.LEFT);
              }
            }
          }
          DeleteDropDownObjs();

          g.setColor(0x428DC2);
          g.fillRect(BTNSTARTLEFT, BTNSTARTTOP, getWidth()-BTNSTARTLEFT, getHeight()-BTNSTARTTOP);
          g.setColor(0xFFFFFF);
          g.drawString(""+gamedroplevelcnt, BTNSTARTLEFT, BTNSTARTTOP, Graphics.TOP|Graphics.LEFT);
        }

  private void DeleteDropDownObjs(){
    int i, j, k;
    boolean linefull;
          for(j=OBJCNTVER-1; j>=0; j--){
            linefull = true;
            for(i=0; i<OBJCNTHOR; i++){
              if(objmapindex[i][j]==false){
               linefull = false;
               break;
              }
            }
            if(linefull){
              gamedroplevelcnt ++;
              for(k=j; k>=1; k--){
                for(i=0; i<OBJCNTHOR; i++){
                  objmapindex[i][k] = objmapindex[i][k-1];
                }
              }
            }
          }
  }

        private void drawScreen(Graphics g){
          g.setColor(0x428DC2);
          g.fillRect(0, 0, getWidth(), getHeight());
          g.setColor(0);
          g.fillRect(PANELLEFT-1, PANELTOP-1, PANELWIDTH+2, PANELHEIGHT+2);
          //g.fillRect(USR1PANELLEFT-1, USR1PANELTOP-1, USRPANELWIDTH+2, USRPANELHEIGHT+2);
          //g.fillRect(USR2PANELLEFT-1, USR2PANELTOP-1, USRPANELWIDTH+2, USRPANELHEIGHT+2);

          g.setColor(0x2BC0DF);
          g.drawRect(PANELLEFT-2, PANELTOP-2, PANELWIDTH+3, PANELHEIGHT+3);
          g.setColor(0x00457B);
          g.drawRect(PANELLEFT-3, PANELTOP-3, PANELWIDTH+5, PANELHEIGHT+5);
          //g.drawRect(USR1PANELLEFT-2, USR1PANELTOP-2, USRPANELWIDTH+3, USRPANELHEIGHT+3);
          //g.drawRect(USR2PANELLEFT-2, USR2PANELTOP-2, USRPANELWIDTH+3, USRPANELHEIGHT+3);

          g.drawImage(imgNextPanel, NXTPANELLEFT, NXTPANELTOP, Graphics.TOP|Graphics.LEFT);

          g.setColor(0xFFFFFF);
          //g.drawString("开始游戏", BTNSTARTLEFT, BTNSTARTTOP, Graphics.TOP|Graphics.LEFT);
          //g.drawString("START", BTNSTARTLEFT, BTNSTARTTOP, Graphics.TOP|Graphics.LEFT);
          flushGraphics();
        }

        private void NewObject(Graphics g){
          System.out.println("In NewObject");
         
          objindex = randint;
          randint = randint2;
          randint2 = Math.abs(objrand.nextInt())%7;
          fallingobj = new Sprite(arrObjImages[objindex]);
             
          objindexx = OBJSMETR[objindex][2];
          objindexy = 0;
          objx = objindexx*OBJSIZE;
          objy = objindexy*OBJSIZE;
          offsetx = offsety = 0;
          objdir = 0;
          fallingobj.setPosition(PANELLEFT+objx+offsetx, PANELTOP+objy+offsety);
          fallingobj.paint(g);
          objstate = 0;//starting to fall
          bottomdelay = 0;
          drawNextObjScreen(g);
         
          if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
           //Game Over
           System.out.println("Yes, game is over.");
           gamestate = -1;
          }
        }
       
  private void drawNextObjScreen(Graphics g){
          g.drawImage(imgNextPanel, NXTPANELLEFT, NXTPANELTOP, Graphics.TOP|Graphics.LEFT);
          g.drawImage(arrObjImages[randint], NXTPANELLEFT+11, NXTPANELTOP+18, Graphics.TOP|Graphics.LEFT);
          g.drawImage(arrObjImages[randint2], NXTPANELLEFT+11, NXTPANELTOP+48, Graphics.TOP|Graphics.LEFT);
  }

  private void drawGameObjs(Graphics g){
          drawPanel(g);
          switch(gamestate){
            case 0:
              switch(objstate){
                case 0:
                  fallingobj.setPosition(PANELLEFT+objx+offsetx, PANELTOP+objy+offsety);
                  fallingobj.paint(g);
                  break;
                case 1:
                  fallingobj = null;
                  System.gc();
                  objstate = -1;
                  System.out.println("Set outstate to -1");
                  break;
                case -1:
                  System.out.println("Again in here...");
                  NewObject(g);
                  break;
              }
              break;
            case 1:
              System.out.println("Game over now");
              fallingobj = null;
              System.gc();
              break;
            default:
              break;
          }
          flushGraphics();
        }
       
        private void input(Graphics g){
          int keyStates = getKeyStates();
          //CubeT.setFrame(0); for sprite testing
         
          switch(gamestate){
            case 0:
              if ((keyStates & LEFT_PRESSED) != 0){
                if(IsAtLeft() != true){
                  if(IsAtInterLeft()!=true){
                    objindexx --;
                    objx = objindexx*OBJSIZE;
                    bottomdelay = 0;
                  }
                }
              }else
              if ((keyStates & RIGHT_PRESSED) != 0){
                if(IsAtRight() != true){
                  if(IsAtInterRight()!=true){
                    objindexx ++;
                    objx = objindexx*OBJSIZE;
                    bottomdelay = 0;
                  }
                }
              }else
              if ((keyStates & DOWN_PRESSED) != 0){
                if(bottomdelay==0){
                 objindexy ++;
                  objy = objindexy*OBJSIZE;
                }
              }else
              if ((keyStates & UP_PRESSED) != 0){
                ChangeDirection();
                fallingobj.setPosition(PANELLEFT+objx+offsetx, PANELTOP+objy+offsety);
                fallingobj.paint(g);
                bottomdelay = 0;
                System.out.print("After Up Pressed");
              }else{
                if(bottomdelay==0){
                  //每隔半秒钟下落一次
                  dropcount++;
                  if(dropcount>=(intDropTime/intDelayTime)){
                    dropcount = 0;
                    objindexy ++;
                    objy = objindexy*OBJSIZE;
                  }
                }
              }
              break;
            default:
              break;
          }
        }

     private void ObjDropDown() {
       System.out.println("In ObjDropDown");

       int i, j, localx, localy, desx, desy;  
          //System.out.println("#objindex:" + objindex + ";objdir:" + objdir);
          //System.out.println("#OBJSMETR[objindex][0]:" + OBJSMETR[objindex][0] + ";OBJSMETR[objindex][1]:" + OBJSMETR[objindex][1]);
          for(i=0; i<OBJSMETR[objindex][1]; i++){
            for(j=0; j<OBJSMETR[objindex][0]; j++){
              switch(objdir){
                default:
                case 0:
                  localx = objindexx+i;
                  localy = objindexy+j;
                  desx = i;
                  desy = j;
                  break;
                case 3://case 1:
                  localx = objindexx+j;
                  localy = objindexy+i;
                  desx = i;
                  desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 2:
                  localx = objindexx+i;
                  localy = objindexy+j;
                  desx = OBJSMETR[objindex][1]-1-i;
                  desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 1://case 3:
                  localx = objindexx+j;
                  localy = objindexy+i;
                  desx = OBJSMETR[objindex][1]-1-i;
                  desy = j;
                  break;
              }
              if(IsOverFlow(localx, localy)){
               continue;
              }
              objmapindex[localx][localy] = objmapindex[localx][localy]|
                                arrSmapindex[objindex][desy][desx];
            }
          }
  }

  private void ChangeDirection(int tdir) {
          System.out.println("In ChangeDirection(int tdir)");
          //变换方块的方向
          System.out.println("The objindex is: "+objindex);

          if(objindex==1){
          }else{
            if(objindex==0){
              switch(tdir){
                case 0:
                case 2:
                  offsetx = 0;
                  offsety = 0;
                  fallingobj.setTransform(Sprite.TRANS_NONE);
                  break;
                case 1:
                case 3:
                  offsetx = -3*OBJSIZE/2;
                  offsety = 3*OBJSIZE/2+1;
                  fallingobj.setRefPixelPosition(0, 0);
                  fallingobj.setTransform(Sprite.TRANS_ROT90);
                  break;
              }
            }else{
              switch(tdir){
                case 0:
                  offsetx = 0;
                  offsety = 0;
                  fallingobj.setTransform(Sprite.TRANS_NONE);
                  break;
                case 3://case 1:
                  offsetx = -OBJSIZE/2;
                  offsety = OBJSIZE/2;
                  fallingobj.setRefPixelPosition(OBJSIZE*3/2, OBJSIZE*3/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT90);
                  break;
                case 2:
                  offsetx = 1;
                  offsety = 0;
                  fallingobj.setRefPixelPosition(fallingobj.getWidth()/2, fallingobj.getHeight()/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT180);
                  break;
                case 1://case 3:
                  offsetx = -OBJSIZE/2;
                  offsety = OBJSIZE/2+1;
                  fallingobj.setRefPixelPosition(OBJSIZE*3/2, OBJSIZE*3/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT270);
                  break;
              }
            }
          }
        }

        private void ChangeDirection() {
          System.out.println("In ChangeDirection");
          //变换方块的方向
          int oridir;
          boolean changefail=true;
          int oriix, orix;
          System.out.println("The objindex is: "+objindex);

          oridir = objdir;
          oriix = objindexx;
          orix = objx;

          if(objindex==1){
          }else{
            objdir = (objdir+1)%4;
            if(objindex==0){
              switch(objdir){
                case 0:
                case 2:
                  offsetx = 0;
                  offsety = 0;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  fallingobj.setTransform(Sprite.TRANS_NONE);
                  break;
                case 1:
                case 3:
                  offsetx = -3*OBJSIZE/2;
                  offsety = 3*OBJSIZE/2+1;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  fallingobj.setRefPixelPosition(0, 0);
                  fallingobj.setTransform(Sprite.TRANS_ROT90);
                  break;
              }
            }else{
              System.out.println("The objdir is: "+objdir);
              switch(objdir){
                case 0:
                  System.out.println("case 0");
                  offsetx = 0;
                  offsety = 0;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  System.out.println("Transform before");
                  fallingobj.setTransform(Sprite.TRANS_NONE);
                  System.out.println("Transform ok");
                  break;
                case 3://case 1:
                  System.out.println("case 1");
                  offsetx = -OBJSIZE/2;
                  offsety = OBJSIZE/2;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  System.out.println("Transform before");
                  fallingobj.setRefPixelPosition(OBJSIZE*3/2, OBJSIZE*3/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT90);
                  System.out.println("Transform ok");
                  break;
                case 2:
                  System.out.println("case 2");
                  offsetx = 1;
                  offsety = 0;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  System.out.println("Transform before");
                  fallingobj.setRefPixelPosition(fallingobj.getWidth()/2, fallingobj.getHeight()/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT180);
                  System.out.println("Transform ok");
                  break;
                case 1://case 3:
                  System.out.println("case 3");
                  offsetx = -OBJSIZE/2;
                  offsety = OBJSIZE/2+1;

                  if(IsOverLapped(objindex, objdir, objindexx, objindexy)){
                    System.out.println("Changing back...");
                    objdir = oridir;
                    objx = orix;
           objindexx = oriix;
           ChangeDirection(objdir);
           return;
                  }
                  System.out.println("Transform before");
                  fallingobj.setRefPixelPosition(OBJSIZE*3/2, OBJSIZE*3/2);
                  fallingobj.setTransform(Sprite.TRANS_ROT270);
                  System.out.println("Transform ok");
                  break;
              }
            }
          }
        }

        private boolean IsAtRight() {
          if(objdir%2==0){
            if(objindexx+OBJSMETR[objindex][1]>=OBJCNTHOR){
              return true;
            }
          }else{
            if(objindexx+OBJSMETR[objindex][0]>=OBJCNTHOR){
              return true;
            }
          }
          return false;
               
          /* judged by objx
          if(objdir%2==0){
            if(objx+offsetx+OBJSMETR[objindex][1]*OBJSIZE+1<PANELWIDTH){
              return false;
            }
          }else{
            if(objx+offsetx+OBJSMETR[objindex][1]*OBJSIZE/2+OBJSMETR[objindex][0]*OBJSIZE/2+1<PANELWIDTH){
              return false;
            }
          }*/
        }

  private boolean IsOverFlow(int x, int y) {
    boolean isflow = false;
          if(x>=OBJCNTHOR || x<0){//数组溢出
            System.out.println("x Overflow");
            isflow = true;
          }
          if(y>=OBJCNTVER || y<0){//数组溢出
            System.out.println("y Overflow");
            isflow = true;
          }
          if(isflow) return true;
    return false;
  }

  private boolean IsAtInterRight(){
          //判断是否右移动的时候,碰到右边已经落下的方块
          //必须先使用IsAtRight()判断是否已经到右边边界,否则会数组溢出
          int i, j, localx, localy, desx, desy;  
          for(i=0; i<OBJSMETR[objindex][1]; i++){
            for(j=0; j<OBJSMETR[objindex][0]; j++){
              switch(objdir){
                default:
                case 0:
                  localx = objindexx+i+1;
                  localy = objindexy+j;
                  desx = i; desy = j;
                  break;
                case 3://case 1:
                  localx = objindexx+j+1;
                  localy = objindexy+i;
                  desx = i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 2:
                  localx = objindexx+i+1;
                  localy = objindexy+j;
                  desx = OBJSMETR[objindex][1]-1-i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 1://case 3:
                  localx = objindexx+j+1;
                  localy = objindexy+i;
                  desx = OBJSMETR[objindex][1]-1-i; desy = j;
                  break;
              }
                 
              if(IsOverFlow(localx, localy)==true){//数组溢出
               System.out.println("Robjdir "+objdir+" Overflow");
               return true;
              }
              if(objmapindex[localx][localy]==true &&
                arrSmapindex[objindex][desy][desx]==true){
                return true;
              }
            }
          }
          return false;
        }

  private boolean IsAtInterLeft(){
          //判断是否右移动的时候,碰到右边已经落下的方块
          //必须先使用IsAtRight()判断是否已经到右边边界,否则会数组溢出
          int i, j, localx, localy, desx, desy;
          for(i=0; i<OBJSMETR[objindex][1]; i++){
            for(j=0; j<OBJSMETR[objindex][0]; j++){
              switch(objdir){
                default:
                case 0:
                  localx = objindexx+i-1;
                  localy = objindexy+j;
                  desx = i; desy = j;
                  break;
                case 3://case 1:
                  localx = objindexx+j-1;
                  localy = objindexy+i;
                  desx = i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 2:
                  localx = objindexx+i-1;
                  localy = objindexy+j;
                  desx = OBJSMETR[objindex][1]-1-i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 1://case 3:
                  localx = objindexx+j-1;
                  localy = objindexy+i;
                  desx = OBJSMETR[objindex][1]-1-i; desy = j;
                  break;
              }
              if(IsOverFlow(localx, localy)==true){//数组溢出
               System.out.println("Lobjdir "+objdir+" Overflow");
               return true;
              }
              if(objmapindex[localx][localy]==true &&
                 arrSmapindex[objindex][desy][desx]==true){
                return true;
              }
            }
          }
          return false;
        }

        private boolean IsOverLapped(int oindex, int odir, int ix, int iy){
          int i, j, localx, localy, desx, desy;
          for(i=0; i<OBJSMETR[oindex][1]; i++){
            for(j=0; j<OBJSMETR[oindex][0]; j++){
              switch(odir){
                default:
                case 0:
                  localx = ix+i;
                  localy = iy+j;
                  desx = i; desy = j;
                  break;
                case 3://case 1:
                  localx = ix+j;
                  localy = iy+i;
                  desx = i; desy = OBJSMETR[oindex][0]-1-j;
                  break;
                case 2:
                  localx = ix+i;
                  localy = iy+j;
                  desx = OBJSMETR[oindex][1]-1-i; desy = OBJSMETR[oindex][0]-1-j;
                  break;
                case 1://case 3:
                  localx = ix+j;
                  localy = iy+i;
                  desx = OBJSMETR[oindex][1]-1-i; desy = j;
                  break;
              }
              System.out.println("localx:"+localx+";localy:"+localy);
             
              if(IsOverFlow(localx, localy)==true){//数组溢出
               System.out.println("Lobjdir "+objdir+" Overflow");
               return true;
              }
              if(objmapindex[localx][localy]==true &&
                 arrSmapindex[oindex][desy][desx]==true){
                return true;
              }
            }
          }
          return false; 
        }
       
        private boolean IsAtLeft() {
          if(objindexx>0){
            return false;
          }
          return true;
        }

        private boolean IsAtBottom() {
          System.out.println("In IsAtBottom");

          int i, j, localx, localy, desx, desy;
          //temp for judging the bottom
          //at bottom judged by objindexx,y
          /*if(objdir%2==0){
            if(objy+(OBJSMETR[objindex][0]-1)*OBJSIZE>=PANELHEIGHT){
              return true;
            }
          }else{
            if(objy+(OBJSMETR[objindex][1]-1)*OBJSIZE>=PANELHEIGHT){
              return true;
            }
          }*/
          if(objdir%2==0){
            if(objindexy+OBJSMETR[objindex][0]>=OBJCNTVER){
              return true;
            }
          }else{
            if(objindexy+OBJSMETR[objindex][1]>=OBJCNTVER){
              return true;
            }
          }

          for(i=0; i<OBJSMETR[objindex][1]; i++){
            for(j=0; j<OBJSMETR[objindex][0]; j++){
              switch(objdir){
                default:
                case 0:
                  localx = objindexx+i;
                  localy = objindexy+j+1;
                  desx = i; desy = j;
                  break;
                case 3://case 1:
                  localx = objindexx+j;
                  localy = objindexy+i+1;
                  desx = i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 2:
                  localx = objindexx+i;
                  localy = objindexy+j+1;
                  desx = OBJSMETR[objindex][1]-1-i; desy = OBJSMETR[objindex][0]-1-j;
                  break;
                case 1://case 3:
                  localx = objindexx+j;
                  localy = objindexy+i+1;
                  desx = OBJSMETR[objindex][1]-1-i; desy = j;
                  break;
              }
              if(IsOverFlow(localx, localy)==true){//数组溢出
               System.out.println("Bobjdir "+objdir+" Overflow");
               return true;
              }
              if(objmapindex[localx][localy]==true &&
                 arrSmapindex[objindex][desy][desx]==true){
                return true;
              }
            }
          }
          return false;
        }

        public void commandAction(Command cmd, Displayable arg1) {
          Alert alert;
          if(cmd == exitCommand){
           
          }else if(cmd == startCommand){
            /*if(gamestate>=0){
              System.out.println("Game is already started!");
            }else{*/
             NewGame();
            //}
          }
        }

  private void NewGame() {
          gamestate = 0;
          objstate = -1;
          gamedroplevelcnt = 0;

          randint = Math.abs(objrand.nextInt())%7;
          randint2 = Math.abs(objrand.nextInt())%7;

          for(int i=0; i<OBJCNTHOR; i++){
            for(int j=0; j<OBJCNTVER; j++){
              objmapindex[i][j] = false;
            }
          }
          System.out.println("Game started! setting gamestate to 0.");
  }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值