如何使用J2ME中的线程(2)

                  private void draw3DBlock(Graphics g, int c, int x, int y, int width, int height){<o:p></o:p>

                      int color = g.getColor();<o:p></o:p>

                      g.setColor( COLOR_WHITE );<o:p></o:p>

                      g.drawRect( x, y, width, height );<o:p></o:p>

                      g.setColor(c);<o:p></o:p>

                      g.fillRect( x + 1, y + 1, width-2, height-2 );<o:p></o:p>

                      g.setColor( COLOR_BLACK );<o:p></o:p>

                      g.drawLine( x + width-1, y, x + width-1, y + height-1 );<o:p></o:p>

                      g.drawLine( x, y + height-1, x + width-1, y + height-1 );<o:p></o:p>

                      g.setColor(color);<o:p></o:p>

                  }<o:p></o:p>

                  <o:p></o:p>

                  public static boolean drawText(Graphics g, String str, int x, int y, int anchor, int color, int size) {<o:p></o:p>

                      Font f_old,f_new;<o:p></o:p>

                      int c_old;<o:p></o:p>

                      try {<o:p></o:p>

                          f_old = g.getFont();<o:p></o:p>

                          f_new =  Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,size);<o:p></o:p>

                          g.setFont(f_new);<o:p></o:p>

                          c_old = g.getColor();<o:p></o:p>

                          g.setColor(color);<o:p></o:p>

                          <o:p></o:p>

                          g.drawString(str, x, y, anchor );<o:p></o:p>

                          <o:p></o:p>

                          g.setColor(c_old);<o:p></o:p>

                          g.setFont(f_old);<o:p></o:p>

                          <o:p></o:p>

                          return true;       <o:p></o:p>

                      }catch (Exception ex) {<o:p></o:p>

                          return false;<o:p></o:p>

                      }<o:p></o:p>

                  }    <o:p></o:p>

                  <o:p></o:p>

                  protected void paint(Graphics g){<o:p></o:p>

                      //画背景<o:p></o:p>

                      try{<o:p></o:p>

                          Image image_Splash = Image.createImage("/back.png");<o:p></o:p>

                          g.drawImage(image_Splash, 0, 0,Graphics.TOP | Graphics.LEFT);<o:p></o:p>

                      }<o:p></o:p>

                      catch(Exception ex) {<o:p></o:p>

                      }<o:p></o:p>

                      <o:p></o:p>

                      //画下一个要出现的方块<o:p></o:p>

                      drawText(g, "下一个", 91, 5, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);<o:p></o:p>

                      g.setColor(COLOR_GRAY);<o:p></o:p>

                      g.drawRoundRect(91, 18, 26, 30, 2, 2);<o:p></o:p>

                      g.setColor(COLOR_DARK_GRAY);<o:p></o:p>

                      g.fillRoundRect(92, 19, 24, 28, 2, 2);<o:p></o:p>

                                          for(int i=0;i<=3;i++)<o:p></o:p>

                                              draw3DBlock(g, BlockInfo[FutureBlockType][8],<o:p></o:p>

                                     93+BlockInfo[FutureBlockType][i*2+1]*BLOCK_SIZE,<o:p></o:p>

                                     20+BlockInfo[FutureBlockType][i*2]*BLOCK_SIZE,<o:p></o:p>

                                     BLOCK_SIZE,BLOCK_SIZE);  <o:p></o:p>

                      <o:p></o:p>

                      drawText(g, "速度:"+String.valueOf(CurSpeed), 91, 60, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);<o:p></o:p>

                      drawText(g, "行数:"+String.valueOf(BlockLines), 91, 75, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);<o:p></o:p>

                      drawText(g, "成绩:", 91, 90, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);<o:p></o:p>

                      g.setColor(COLOR_GRAY);<o:p></o:p>

                      g.drawRoundRect(91, 105, 26, 20, 2, 2);<o:p></o:p>

                      g.setColor(COLOR_DARK_GRAY);<o:p></o:p>

                      g.fillRoundRect(92, 106, 24, 18, 2, 2);     <o:p></o:p>

                      drawText(g, String.valueOf(BlockScore), 93, 107, Graphics.TOP| Graphics.LEFT, COLOR_WHITE, Font.SIZE_MEDIUM);<o:p></o:p>

                      <o:p></o:p>

                      //画当前战况<o:p></o:p>

                                          for(int i=0;i<CANVAS_SIZE_HEIGHT-1;i++)<o:p></o:p>

                          for(int j=1;j<CANVAS_SIZE_WIDTH-1;j++)<o:p></o:p>

                              if (Gridmatrix[i][j]!=0)<o:p></o:p>

                                      draw3DBlock(g,Gridmatrix[i][j],CANVAS_OFFSET_X+j*BLOCK_SIZE,<o:p></o:p>

                                              CANVAS_OFFSET_Y+i*BLOCK_SIZE,<o:p></o:p>

                                              BLOCK_SIZE,BLOCK_SIZE);<o:p></o:p>

                      <o:p></o:p>

                                          if (!ISDOWN){<o:p></o:p>

                                          //画上新的方块<o:p></o:p>

                                               LastX=BlockX; LastY=BlockY; LastType=BlockType;<o:p></o:p>

                                               for(int i=0;i<=3;i++)<o:p></o:p>

                                                      draw3DBlock(g,BlockInfo[BlockType][8],<o:p></o:p>

                                                             CANVAS_OFFSET_X+BlockX*BLOCK_SIZE+BlockInfo[BlockType][i*2+1]*BLOCK_SIZE,<o:p></o:p>

                                                             CANVAS_OFFSET_Y+BlockY*BLOCK_SIZE+BlockInfo[BlockType][i*2]*BLOCK_SIZE,<o:p></o:p>

                                                             BLOCK_SIZE,BLOCK_SIZE);<o:p></o:p>

                                          }<o:p></o:p>

                  }<o:p></o:p>

              <o:p></o:p>

                  private boolean feasible(){<o:p></o:p>

                                   for(int i=0;i<=3;i++)<o:p></o:p>

                               if (Gridmatrix[BlockY+BlockInfo[BlockType][i*2]][BlockX+BlockInfo[BlockType][i*2+1]]!=0)<o:p></o:p>

                                      return false;<o:p></o:p>

                                   return true;<o:p></o:p>

                  }<o:p></o:p>

                  <o:p></o:p>

                  private void delline(){<o:p></o:p>

                                   for(int i=0;i<=3;i++)<o:p></o:p>

                                        Gridmatrix[BlockY+BlockInfo[BlockType][i*2]][BlockX+BlockInfo[BlockType][i*2+1]]=BlockInfo[BlockType][8];<o:p></o:p>

                                   int temp=4;<o:p></o:p>

                                   boolean CanSkip=false;  <o:p></o:p>

                                   int i=CANVAS_SIZE_HEIGHT-2;<o:p></o:p>

                                   while((temp>0)&&(i>=1)){     <o:p></o:p>

                                        CanSkip=false;<o:p></o:p>

                                        label1: for(int j=1;j<=CANVAS_SIZE_WIDTH-2;j++){<o:p></o:p>

                                            if (Gridmatrix[i][j]==0)  {CanSkip=true; i--; break label1;}<o:p></o:p>

                                        }<o:p></o:p>

                                        if (!CanSkip){<o:p></o:p>

                                            temp--;<o:p></o:p>

                                             for(int k=i;k>=1;k--)<o:p></o:p>

                                             for(int l=1;l<=CANVAS_SIZE_WIDTH-2;l++)<o:p></o:p>

                                                  Gridmatrix[k][l]=Gridmatrix[k-1][l];<o:p></o:p>

                                            BlockLines++;<o:p></o:p>

                                            BlockScore+=200;<o:p></o:p>

                                            if((BlockScore%2000)<200) CurSpeed++;      <o:p></o:p>

                                        }<o:p></o:p>

                                   }<o:p></o:p>

                  }<o:p></o:p>

                  <o:p></o:p>

                  public void run() {<o:p></o:p>

                    while (Blocker != null) {<o:p></o:p>

                                           if(!ISDOWN){<o:p></o:p>

                                               BlockY++;<o:p></o:p>

                                               if (!feasible()) {<o:p></o:p>

                                                    ISDOWN=true; BlockY--; delline();<o:p></o:p>

                                                    try {Thread.sleep(400);} catch (InterruptedException e){}<o:p></o:p>

                                               }<o:p></o:p>

                                               else{<o:p></o:p>

                                                                 repaint();<o:p></o:p>

                                                                 try {Thread.sleep(950-100*(int)BlockSpeed);} catch (InterruptedException e){}<o:p></o:p>

                                                    }<o:p></o:p>

                                                 }<o:p></o:p>

                                                 else{   BlockScore+=50;<o:p></o:p>

                                                               if((BlockScore%2000)<50) CurSpeed++;    <o:p></o:p>

                                                               ISDOWN=false;<o:p></o:p>

                                                               repaint();<o:p></o:p>

                                                               BlockSpeed=CurSpeed;<o:p></o:p>

                                                               BlockType=FutureBlockType;<o:p></o:p>

                                                               FutureBlockType=Math.abs(generator.nextInt()%19);<o:p></o:p>

                                                                  BlockX=4;     LastX=BlockX;<o:p></o:p>

                                                               BlockY=0;     LastY=BlockY;<o:p></o:p>

                                                               if (!feasible()) { init();}<o:p></o:p>

                                                 }<o:p></o:p>

                         }<o:p></o:p>

                         Blocker = null;<o:p></o:p>

                  }<o:p></o:p>

              <o:p></o:p>

                  protected void keyPressed(int keyCode) {<o:p></o:p>

                      //处理按下键盘的事件,这是Canvas的实例方法<o:p></o:p>

                      switch (getGameAction(keyCode)) {//将按键的值转化成方向常量<o:p></o:p>

                          case Canvas.UP://向上<o:p></o:p>

              <o:p></o:p>

                              break;<o:p></o:p>

                          case Canvas.DOWN://向下<o:p></o:p>

                              BlockY++;<o:p></o:p>

                              if (!feasible()) BlockY--;<o:p></o:p>

                                  repaint();<o:p></o:p>

                              BlockSpeed=9;<o:p></o:p>

                              //Blocker.run();<o:p></o:p>

                              break;<o:p></o:p>

                          case Canvas.LEFT://向左<o:p></o:p>

                                                                      BlockX--;<o:p></o:p>

                                                                      if (!feasible()) BlockX++;<o:p></o:p>

                                                                      break;<o:p></o:p>

                          case Canvas.RIGHT://向右<o:p></o:p>

                                                                      BlockX++; <o:p></o:p>

                                                                      if (!feasible()) BlockX--; <o:p></o:p>

                              break;<o:p></o:p>

                          case Canvas.FIRE:<o:p></o:p>

                                                                      int tempBlockType=BlockType; <o:p></o:p>

                                                                      if (BlockType==1) BlockType=-1;<o:p></o:p>

                                  else if (BlockType==3) BlockType=1;<o:p></o:p>

                                  else if (BlockType==5) BlockType=3;<o:p></o:p>

                                  else if (BlockType==6) BlockType=5;<o:p></o:p>

                                  else if (BlockType==10) BlockType=6;<o:p></o:p>

                                  else if (BlockType==14) BlockType=10;<o:p></o:p>

                                  else if (BlockType==18) BlockType=14;<o:p></o:p>

                                                                      BlockType++; <o:p></o:p>

                                                                      if (!feasible()) BlockType=tempBlockType; <o:p></o:p>

                              break;<o:p></o:p>

                          default:<o:p></o:p>

                              break;<o:p></o:p>

                      }<o:p></o:p>

                      repaint(); return;<o:p></o:p>

                  }   <o:p></o:p>

              }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值