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

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


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

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


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

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


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

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


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

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

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

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


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

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

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

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


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

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

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

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

    主站蜘蛛池模板: 亚洲中文无码永久免费| 亚洲乱码一二三四五六区| 亚洲日韩乱码中文字幕| 国产A在亚洲线播放| 免费在线观看黄网| 国产精品综合专区中文字幕免费播放| 国产va精品免费观看| 免费精品一区二区三区第35| 亚洲免费闲人蜜桃| 亚洲av无码无在线观看红杏| 中文字幕无码免费久久99| 久艹视频在线免费观看| 黄 色一级 成 人网站免费| 黄色免费网站在线看| 亚洲一线产品二线产品| 亚洲AV蜜桃永久无码精品| 女人被弄到高潮的免费视频| h视频在线观看免费网站| 无码av免费一区二区三区| 亚洲中文字幕无码mv| 亚洲国产av一区二区三区丶| 久久亚洲精品中文字幕| 国产大片免费观看中文字幕| 国产成人精品免费久久久久| 91av免费在线视频| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 99re免费99re在线视频手机版| 一本一道dvd在线观看免费视频| 911精品国产亚洲日本美国韩国| 亚洲AV永久无码精品一百度影院| 国产偷窥女洗浴在线观看亚洲 | 亚洲av日韩av高潮潮喷无码| 国产亚洲精品观看91在线| 亚洲国产无套无码av电影| 亚洲三区在线观看无套内射| a级亚洲片精品久久久久久久 | 特级毛片A级毛片免费播放| 美美女高清毛片视频黄的一免费 | 亚洲福利中文字幕在线网址| 亚洲精品成人久久久| 成人AV免费网址在线观看|