<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    聚合 管理  

    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 林俊海 閱讀(541) 評論(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"

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


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

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


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

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


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

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


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

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

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

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


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

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

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

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


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

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

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

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

    主站蜘蛛池模板: 在线观看免费大黄网站| 亚洲欧洲日韩极速播放| 精品女同一区二区三区免费播放 | 亚洲人成影院77777| 久久国产乱子精品免费女| 亚洲熟妇无码另类久久久| 国产精品福利片免费看| 最新国产AV无码专区亚洲| 一级有奶水毛片免费看| 亚洲成av人片在线观看无码不卡| 国产在线观看免费av站| 亚洲av女电影网| 91精品成人免费国产片| 亚洲av日韩av无码av| 卡1卡2卡3卡4卡5免费视频| 亚洲av永久无码一区二区三区| 午夜国产大片免费观看| 中国好声音第二季免费播放| 亚洲AV午夜福利精品一区二区| 久久w5ww成w人免费| youjizz亚洲| 亚洲国产成人久久精品99| 久久久WWW成人免费精品| 亚洲国产成人私人影院| 最近免费中文字幕视频高清在线看| 亚洲Aⅴ在线无码播放毛片一线天| 免费大香伊蕉在人线国产| 一级毛片无遮挡免费全部| 亚洲精品美女久久777777| h在线观看视频免费网站| 亚洲国产精品无码中文lv| 久久亚洲中文字幕精品一区| 美丽姑娘免费观看在线观看中文版 | 91视频国产免费| 四虎一区二区成人免费影院网址| 亚洲情XO亚洲色XO无码| 免费无码肉片在线观看| 国产精品免费看久久久香蕉| 亚洲国产品综合人成综合网站| 免费国产小视频在线观看| 1000部禁片黄的免费看|