聚合 管理  

          Blog Stats

          News

          我使用新博客啦:
          http://www.linjunhai.com/
          大家到我的新博客上看看吧!

          隨筆分類(28)

          文章分類(4)

          隨筆檔案(53)

          文章檔案(4)

          相冊

          相關鏈接


          林俊海的博客

          超級大菜鳥,每天要自強!

          昨天看同學在做俄羅斯方塊,自己心癢癢的也想做個,費了九牛二虎之力,才寫了這么個簡單的 Applet 來,繼續努力,爭取盡快完善.
          (運行后點擊界面激活窗口后就可以用鍵盤控制了, 方向鍵 上: 旋轉, 下:加速, 左:左移, 右:右移)

          import java.applet.Applet;
          import java.awt.Color;
          import java.awt.Event;
          import java.awt.Graphics;
          import java.awt.Image;

          public class Game extends Applet implements Runnable {
              
          boolean[][] Map = new boolean[21][12];
              
          boolean pause = false;
              
          long sleep_time = 1000;
              Thread runer;
              Image buffer_img;
              Graphics g;
              TShape shape;

              
          public void init() {
                  
          this.resize(170320);
                  
          for (int i = 0; i < 20; i++)
                      
          for (int j = 0; j < 12; j++)
                          Map[i][j] 
          = false;
                  
          for (int i = 0; i < 12; i++)
                      Map[
          20][i] = true;
                  
          for (int i = 0; i < 20; i++{
                      Map[i][
          0= true;
                      Map[i][
          11= true;
                  }

                  buffer_img 
          = createImage(this.size().width, this.size().height);
                  g 
          = buffer_img.getGraphics();
                  shape 
          = new TShape();
                  shape.next();
              }


              
          public void start() {
                  
          if (runer == null{
                      runer 
          = new Thread(this);
                      runer.start();
                  }

              }


              
          public void stop() {
                  
          if (runer != null{
                      runer.stop();
                      runer 
          = null;
                  }

              }


              
          private boolean checkPoint() {
                  Point[] ps 
          = shape.getPoint();
                  
          for (int i = 0; i < ps.length; i++{
                      
          if (ps[i].x < 0)
                          
          return false;
                      
          if (ps[i].x >= 11)
                          
          return false;
                      
          if (ps[i].y >= 0)
                          
          if (Map[ps[i].y][ps[i].x])
                              
          return false;
                  }

                  
          return true;
              }


              
          private void check() {
                  shape.move(
          01);
                  
          if (!checkPoint()) {
                      shape.move(
          0-1);
                      Point[] ps2 
          = shape.getPoint();
                      
          for (int j = 0; j < ps2.length; j++)
                          
          if (ps2[j].x >= 0 && ps2[j].y >= 0)
                              Map[ps2[j].y][ps2[j].x] 
          = true;
                      checkFull();
                      shape.next();
                      System.gc();
                      
          if (!checkPoint()) {
                          runer 
          = null;
                          System.out.println(
          "失敗!游戲結束");
                          pause 
          = true;
                          
          return;
                      }

                  }

              }


              
          private void checkFull() {
                  
          boolean flag;
                  
          for (int i = 19; i >= 0; i--{
                      flag 
          = true;
                      
          for (int j = 1; j < 11; j++)
                          
          if (!Map[i][j])
                              flag 
          = false;
                      
          if (flag) {
                          g.setColor(Color.WHITE);
                          
          for (int k = 1; k < 11; k++)
                              g.fillRect(k 
          * 15 - 312 + i * 151212);
                          myPaint();
                          
          try {Thread.sleep(100);} catch (Exception e) {}
                          g.setColor(Color.RED);
                          
          for (int k = 1; k < 11; k++)
                              g.fillRect(k 
          * 15 - 312 + i * 151212);
                          myPaint();
                          
          try {Thread.sleep(100);} catch (Exception e) {}
                          
          for (int j = i; j > 0; j--)
                              
          for (int k = 1; k < 11; k++)
                                  Map[j][k] 
          = Map[j - 1][k];
                          i
          ++;
                      }

                  }

              }


              
          public boolean keyDown(Event e, int KeyCode) {
                  
          if (KeyCode == 1004{
                      shape.deasil();
                      
          if (!checkPoint()) {
                          shape.anticlockwise();
                          
          return true;
                      }

                      
          this.myPaint();
                  }
           else if (KeyCode == 1005{
                      sleep_time 
          = 100;
                  }
           else if (KeyCode == 1006{
                      shape.move(
          -10);
                      
          if (!checkPoint()) {
                          shape.move(
          10);
                          
          return true;
                      }

                      
          this.myPaint();
                  }
           else if (KeyCode == 1007{
                      shape.move(
          10);
                      
          if (!checkPoint()) {
                          shape.move(
          -10);
                          
          return true;
                      }

                      
          this.myPaint();
                  }

                  
          return true;
              }


              
          public boolean keyUp(Event e, int KeyCode) {
                  
          if (KeyCode == 1005)
                      sleep_time 
          = 1000;
                  
          return true;
              }


              
          public void run() {
                  g.setColor(Color.RED);
                  g.fillRect(
          66159308);
                  
          this.myPaint();
                  
          while (!pause) {
                      
          try { Thread.sleep(sleep_time); } catch (Exception e) {}
                      check();
                      
          this.myPaint();
                  }

              }


              
          public void myPaint() {
                  g.setColor(Color.BLACK);
                  g.fillRect(
          1010151301);
                  g.setColor(Color.WHITE);
                  
          for (int i = 0; i < 20; i++)
                      
          for (int j = 1; j < 11; j++)
                          
          if (Map[i][j])
                              g.drawRect(j 
          * 15 - 510 + i * 151515);
                  g.setColor(Color.GREEN);
                  
          for (int i = 0; i < 20; i++)
                      
          for (int j = 1; j < 11; j++)
                          
          if (Map[i][j])
                              g.fillRect(j 
          * 15 - 312 + i * 151212);
                  Point[] ps 
          = shape.getPoint();
                  g.setColor(Color.WHITE);
                  
          for (int i = 0; i < ps.length; i++)
                      
          if (ps[i].x >= 0 && ps[i].y >= 0)
                          g.drawRect(ps[i].x 
          * 15 - 510 + ps[i].y * 151515);
                  g.setColor(Color.YELLOW);
                  
          for (int i = 0; i < ps.length; i++)
                      
          if (ps[i].x > 0 && ps[i].y >= 0)
                          g.fillRect(ps[i].x 
          * 15 - 312 + ps[i].y * 151212);
                  
          this.repaint();
              }


              
          public void update(Graphics g) {
                  g.drawImage(buffer_img, 
          00this);
              }

          }


          class Point {
              
          public int x, y;
              
          public Point(int x, int y) {
                  
          this.x = x; this.y = y;
              }

          }


          class TShape {
              
          int[][][] data;
              
          int[] a, b, num;
              
          int x, y;
              
          int style, orientation;

              
          public TShape() {
                  b 
          = new int[] 0210111 };
                  data 
          = new int[][][] {
                      
          {0x00ffff000x00ffff000x000000000x00000000 }}
                          
                      
          {0x000000000x000000000xffffffff0x00000000 },
                       
          0x00ff00000x00ff00000x00ff00000x00ff0000 }}
          ,
                          
                      
          {0x00ff00000x00ffff000x0000ff000x00000000 },
                       
          0x000000000x00ffff000xffff00000x00000000 }}
          ,
                              
                      
          {0x0000ff000x00ffff000x00ff00000x00000000 },
                       
          0x000000000x00ffff000x0000ffff0x00000000 }}
          ,
                          
                      
          {0x000000000x00ff00000xffffff000x00000000 },
                       
          0xff0000000xffff00000xff0000000x00000000 },
                       
          0x000000000xffffff000x00ff00000x00000000 },
                       
          0x00ff00000xffff00000x00ff00000x00000000 }}
          ,
              
                      
          {0x000000000x0000ff000xffffff000x00000000 },
                       
          0x00ff00000x00ff00000x00ffff000x00000000 },
                       
          0x000000000xffffff000xff0000000x00000000 },
                       
          0x00ffff000x0000ff000x0000ff000x00000000 }}

                              
                      
          {0x000000000x00ff00000x00ffffff0x00000000 },
                       
          0x00ffff000x00ff00000x00ff00000x00000000 },
                       
          0x000000000x00ffffff0x000000ff0x00000000 },
                       
          0x0000ff000x0000ff000x00ffff000x00000000 }}

                  }
          ;
                  orientation 
          = (int)(Math.random() * 7);
              }


              
          public void next() {
                  x 
          = y = 0;
                  style 
          = (int) (Math.random() * 7);
                  orientation 
          = (int)(Math.random() * (data[style].length));
              }


              
          public void move(int x, int y) {
                  
          this.x += x;
                  
          this.y += y;
              }


              
          public Point[] getPoint() {
                  Point[] ps 
          = new Point[4];
                  
          int l = 0;
                  
          int[] arr = this.data[style][orientation];
                  
          for (int i = 0; i < arr.length; i++)
                      
          for (int n = 0; n < 4; n++)
                          
          if ((0x000000ff & arr[i] >> (n * 8)) == 0x000000ff{
                              ps[l] 
          = new Point(7 - n + x, i - b[style] + y);
                              l
          ++;
                          }

                  
          return ps;
              }


              
          public void deasil() {
                  orientation 
          = (orientation + data[style].length + 1% data[style].length;
              }


              
          public void anticlockwise() {
                  orientation 
          = (orientation + data[style].length - 1% data[style].length;
              }

          }
          posted on 2007-04-15 09:45 林俊海 閱讀(539) 評論(2)  編輯  收藏 所屬分類: JAVA天地

          評論

          # re: 俄羅斯方塊 2007-06-01 15:11 sunboylyg
          我也做了一個,但比你的差遠了:(
          始終看不明白你那個3維數組data是怎么構造出來的,請指教一二……  回復  更多評論
            

          # re: 俄羅斯方塊 2007-06-01 17:49 林志斌
          呵呵 sunboylyg 過獎了,我也是初學,至于那個 data[][][],我們先復制其內容到記事本

          {{ 0x00ffff00, 0x00ffff00, 0x00000000, 0x00000000 }},

          {{ 0x00000000, 0x00000000, 0xffffffff, 0x00000000 },
          { 0x00ff0000, 0x00ff0000, 0x00ff0000, 0x00ff0000 }},

          {{ 0x00ff0000, 0x00ffff00, 0x0000ff00, 0x00000000 },
          { 0x00000000, 0x00ffff00, 0xffff0000, 0x00000000 }},

          {{ 0x0000ff00, 0x00ffff00, 0x00ff0000, 0x00000000 },
          { 0x00000000, 0x00ffff00, 0x0000ffff, 0x00000000 }},

          {{ 0x00000000, 0x00ff0000, 0xffffff00, 0x00000000 },
          { 0xff000000, 0xffff0000, 0xff000000, 0x00000000 },
          { 0x00000000, 0xffffff00, 0x00ff0000, 0x00000000 },
          { 0x00ff0000, 0xffff0000, 0x00ff0000, 0x00000000 }},

          {{ 0x00000000, 0x0000ff00, 0xffffff00, 0x00000000 },
          { 0x00ff0000, 0x00ff0000, 0x00ffff00, 0x00000000 },
          { 0x00000000, 0xffffff00, 0xff000000, 0x00000000 },
          { 0x00ffff00, 0x0000ff00, 0x0000ff00, 0x00000000 }},

          {{ 0x00000000, 0x00ff0000, 0x00ffffff, 0x00000000 },
          { 0x00ffff00, 0x00ff0000, 0x00ff0000, 0x00000000 },
          { 0x00000000, 0x00ffffff, 0x000000ff, 0x00000000 },
          { 0x0000ff00, 0x0000ff00, 0x00ffff00, 0x00000000 }}


          將 "{", "}", " "(空格), "0x" 替換為空串
          將 "ff" 替換為 "■"
          將 "00" 替換為 "□"
          再用其它工具叵手動將 "," 替換為 "\r\n"

          可得到:
          =====================================================
          □■■□
          □■■□
          □□□□
          □□□□


          □□□□
          □□□□
          ■■■■
          □□□□

          □■□□
          □■□□
          □■□□
          □■□□


          □■□□
          □■■□
          □□■□
          □□□□

          □□□□
          □■■□
          ■■□□
          □□□□


          □□■□
          □■■□
          □■□□
          □□□□

          □□□□
          □■■□
          □□■■
          □□□□


          □□□□
          □■□□
          ■■■□
          □□□□

          ■□□□
          ■■□□
          ■□□□
          □□□□

          □□□□
          ■■■□
          □■□□
          □□□□

          □■□□
          ■■□□
          □■□□
          □□□□


          □□□□
          □□■□
          ■■■□
          □□□□

          □■□□
          □■□□
          □■■□
          □□□□

          □□□□
          ■■■□
          ■□□□
          □□□□

          □■■□
          □□■□
          □□■□
          □□□□


          □□□□
          □■□□
          □■■■
          □□□□

          □■■□
          □■□□
          □■□□
          □□□□

          □□□□
          □■■■
          □□□■
          □□□□

          □□■□
          □□■□
          □■■□
          □□□□
          =======================================================
          呵呵,上面每個圖形間有一個空行, 每組間有兩個空行  回復  更多評論
            

          主站蜘蛛池模板: 驻马店市| 鄄城县| 微山县| 延边| 全州县| 车险| 凯里市| 镇原县| 宁河县| 乌拉特前旗| 东港市| 桐庐县| 郓城县| 潢川县| 芦溪县| 松滋市| 万年县| 永兴县| 平南县| 永丰县| 浙江省| 尤溪县| 博客| 乐至县| 兰溪市| 龙井市| 运城市| 裕民县| 全南县| 靖远县| 商河县| 拉萨市| 乌兰察布市| 革吉县| 乌拉特中旗| 武城县| 峨眉山市| 尼勒克县| 呼伦贝尔市| 马鞍山市| 枞阳县|