⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blocks.java

📁 This is the java code of elsifk,if you find some bugs ,please contact me! Thank you!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
class StatisticsCanvas extends Canvas implements ImageObserver {
    public static final Color textColor = Color.black;
    public static final int MaxLevel = 9;
    public static final int myFontHeight = 16;

    protected Font BlocksFont;
    protected FontMetrics BlocksFontMetrics;
    protected int Level, Lines, Score;
    protected Shape NextShape = null;

    public void InitNewGame() {
        Level = Lines = Score = 0;
    }

    public void GameOver() {
	}

    public int GetGameSpeed() {
        switch (Level) {
            case 0:     return 700;
            case 1:     return 600;
            case 2:     return 500;
            case 3:     return 400;
            case 4:     return 350;
            case 5:     return 300;
            case 6:     return 250;
            case 7:     return 200;
            case 8:     return 150;
            case 9:     return 100;
            default:    return 100;
        }
    }

    public StatisticsCanvas() {
        reshape(0, 0, 100, 100);		
        BlocksFont = new Font("TimesRoman",Font.PLAIN,20);
        setFont(BlocksFont);
        BlocksFontMetrics = getFontMetrics(BlocksFont);

        InitNewGame();
    }

    public void AddScore(int s) {
        Score += s;
        repaint();
    }

    public void AddLines(int ln) {
        switch (ln) {
            case 1:
                AddScore (10);
                break;
            case 2:
                AddScore (20);
                break;
            case 3:
                AddScore (30);
                break;
            case 4:
                AddScore (40);
                break;
        }

        Lines += ln;
        if (Lines > (10 * (Level + 1))) AddLevel();
        repaint();
    }

    public void AddLevel() {
        Blocks.play (Blocks.SOUND_NEXTLEVEL);
        if (Level < MaxLevel) Level++;
        repaint();
    }

    public void DisplayNextShape(Shape s) {
        NextShape =  s;
        repaint();
    }

    public void paint(Graphics g) {
        g.setColor(textColor);
        g.drawString("Level: " + Level, 0, myFontHeight);
        g.drawString("Lines: " + Lines, 0, myFontHeight * 3);
        g.drawString("Score: " + Score, 0, myFontHeight * 5);
        g.drawString("Next:", 0, myFontHeight * 7);

        if (NextShape != null) {
            NextShape.DisplayAbs(g, 10, myFontHeight * 7 + 10);
        }
    }
}

class PlayFieldCanvas extends Canvas implements ImageObserver {
    public static final int fh1 = 17, fh2 = 17;
    public static final int BorderWidth = 5;
    public static final Color BorderColor = Color.blue;
	protected Shape FallingShape = null;

    boolean FallingShapeNeedRepaint = false;
    boolean PlayFieldNeedRepaint = false;
    boolean DiscardGame = false;
    boolean ShowAboutBox = false;

    protected Font BlocksFont1, BlocksFont2;

    Color PlayField[][] = null;

    public void About() {
        ShowAboutBox = true;
        repaint();
    }

    public void GameOver() {
        DiscardGame = true;
        repaint();
    }

    public void RepaintPlayField() {
        PlayFieldNeedRepaint = true;
        repaint();
    }

    public void SetPlayField(Color pv[][]) {
        PlayField = pv;
    }

    public PlayFieldCanvas() {
        BlocksFont1 = new Font("TimesRoman",Font.BOLD,20);
        FontMetrics BlocksFontMetrics1 = getFontMetrics(BlocksFont1);
        BlocksFont2 = new Font("TimesRoman",Font.PLAIN,14);
        FontMetrics BlocksFontMetrics2 = getFontMetrics(BlocksFont2);
        reshape(0, 0, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, Blocks.ElementSize * Blocks.rows + BorderWidth );
    }

    public void repaint(Shape Shp) {
       FallingShape = Shp;
       FallingShapeNeedRepaint = true;
       repaint();
    }

    public void DrawLines (Graphics g, int y1, int y2) {
        for (int y = y1 * Blocks.ElementSize; y < y2 * Blocks.ElementSize; y++) {
            g.drawLine (BorderWidth, y, BorderWidth + Blocks.cols * Blocks.ElementSize, y);
        }
    }

    public void GraphicsEffect(Graphics g, int y1, int y2) {
        for (int l = 0; l < 10; l++) {
            g.setColor(Color.red);
            DrawLines (g, y1, y2);
            g.setColor(Color.green);
            DrawLines (g, y1, y2);
            g.setColor(Color.blue);
            DrawLines (g, y1, y2);
            g.setColor(Color.black);
            DrawLines (g, y1, y2);
        }
    }

    public void DiscardIt(Graphics g) {
        DiscardGame = false;
        GraphicsEffect (g, 0, Blocks.rows);
    }

    public void DisplayAboutBox(Graphics g)
    {
        int y = 1;
        // Clear background
        g.setColor(Color.black);
        g.fillRect (BorderWidth, 0, Blocks.ElementSize * Blocks.cols, Blocks.ElementSize * Blocks.rows);
        g.setFont(BlocksFont1);
        g.setColor(Color.red);
        g.drawString("BLOCKS", 20, fh1);
        g.setFont(BlocksFont2);
        g.setColor(Color.cyan);
        g.drawString("v 1.01", BorderWidth + 110, fh1);
        g.drawString("Copyright (c) 1996", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("Iwan van Rienen. The", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("program is postcard", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("ware. If you like it", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("buy a stamp and", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("a postcard and send : ", BorderWidth + 2, fh1 + fh2 * y++);        g.setColor(Color.green);
        g.setColor(Color.yellow);
        g.drawString("it to Iwan van Rienen", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("J. Maetsuyckerstr. ", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("145 2593 ZG The Hague", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("The Netherlands", BorderWidth + 2, fh1 + fh2 * y++);
        g.setColor(Color.cyan);
        g.drawString("This source is free", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("Visit my homepage at", BorderWidth + 2, fh1 + fh2 * y++);
        g.setColor(Color.green);
        g.drawString("http://www.bart.nl/", BorderWidth + 2, fh1 + fh2 * y++);
        g.drawString("E-mail to ivr@bart.nl", BorderWidth + 2, fh1 + fh2 * y++);
    }

    public void update(Graphics g) {
        if (DiscardGame) {
            DiscardIt(g);
        }

        if (PlayFieldNeedRepaint) {
            DrawPlayField(g);
        }

        DrawFallingShape(g);

        if (ShowAboutBox) {
            DisplayAboutBox(g);
        }
    }

    public void paint(Graphics g) {
        if (ShowAboutBox) {
            DisplayAboutBox(g);
        }		else {
            DrawPlayField(g);
            FallingShapeNeedRepaint = true;
            DrawFallingShape(g);
        }
    }

    public void DrawFallingShape(Graphics g) {
        if (FallingShapeNeedRepaint && FallingShape != null) {
            FallingShape.hide(g, BorderWidth);

            while (!FallingShape.IsReady()) ;
            FallingShape.Display(g, BorderWidth);
            FallingShapeNeedRepaint = false;
        }
    }

    public void DrawPlayField(Graphics g) {
        int x, y;

        g.setColor(BorderColor);
        // Draw left border
        g.fillRect (0, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
        // Draw right border
        g.fillRect (Blocks.ElementSize * Blocks.cols + BorderWidth, 0, BorderWidth, Blocks.ElementSize * Blocks.rows);
        // Draw bottom border
        g.fillRect (0, Blocks.ElementSize * Blocks.rows, Blocks.ElementSize * Blocks.cols + BorderWidth * 2, BorderWidth);

        for (x = 0; x < Blocks.cols; x++) {
            for (y = 0; y < Blocks.rows; y++) {
                if (PlayField[x][y] != Color.black) {
                    g.setColor(PlayField[x][y]);
                    g.fillRect(BorderWidth + x * Blocks.ElementSize + 1, y * Blocks.ElementSize + 1, Blocks.ElementSize - 2, Blocks.ElementSize - 2);
                    g.setColor(Color.white);
                    g.drawRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize - 1, Blocks.ElementSize - 1);
                } 				else {
                    g.setColor(Color.black);
                    g.fillRect(BorderWidth + x * Blocks.ElementSize, y * Blocks.ElementSize, Blocks.ElementSize, Blocks.ElementSize);
                }
            }
        }

    PlayFieldNeedRepaint = false;
    }
}

class Element {
    protected int x, y;
    int oldX, oldY;
    protected int xInShape, yInShape;
    protected int OriginalX, OriginalY;
    protected int OriginalXInShape, OriginalYInShape;
    protected Color clr;
    protected boolean ErasePossible;

    public Element(int xPos, int yPos, Color c) {
        ErasePossible = false;		
        xInShape = OriginalXInShape = xPos;
        yInShape = OriginalYInShape = yPos;
        x = OriginalX = xPos + Blocks.cols / 2 - (Blocks.MaxElement + 1)/ 2;
        y = OriginalY = yPos;		
        clr = c;
    }

    public void Init() {
        ErasePossible = false;

        x = OriginalX; y = OriginalY;

        xInShape = OriginalXInShape;
        yInShape = OriginalYInShape;
    }

    public void hide (Graphics g, int xOffs, int yOffs) {
        if (ErasePossible) {
            int Size = Blocks.ElementSize;

            g.setColor(Color.black);
            g.fillRect(xOffs + oldX * Size, yOffs + oldY * Size, Size, Size);

            ErasePossible = false;
        }
    }

    public void Display (Graphics g, int xOffs, int yOffs) {
        int Size = Blocks.ElementSize;

        g.setColor(clr);
        g.fillRect(xOffs + x * Size + 1, yOffs + y * Size + 1, Size - 2, Size - 2);
        g.setColor(Color.white);
        g.drawRect(xOffs + x * Size, yOffs + y * Size, Size - 1, Size - 1);

        oldX = x;
        oldY = y;

        ErasePossible = true;
    }

     public void DisplayAbs (Graphics g, int xOffs, int yOffs) {
        int Size = Blocks.ElementSize;

        g.setColor(clr);
        g.fillRect(xOffs + OriginalXInShape * Size + 1, yOffs + OriginalYInShape * Size + 1, Size - 2, Size - 2);
        g.setColor(Color.white);
        g.drawRect(xOffs + OriginalXInShape * Size, yOffs + OriginalYInShape * Size, Size - 1, Size - 1);
	}

    public boolean CheckIfElementFits(Color PlayField[][],
                                      int xOffs, int yOffs, boolean Rotate) {
       if (Rotate) {
            xOffs += Blocks.MaxElement - yInShape - xInShape;
            yOffs += xInShape - yInShape;
        }

        if (x + xOffs < 0 || x + xOffs >= Blocks.cols) return false;
        if (y + yOffs >= Blocks.rows ) return false;
        if (PlayField[x + xOffs][y + yOffs] != Color.black) return false;

        return true;
    }

    public void ChangeElementPosition(int xOffs, int yOffs, boolean Rotate) {
        if (Rotate) {
            xOffs += Blocks.MaxElement - yInShape - xInShape;
            yOffs += xInShape - yInShape;

            int tempxInShape = xInShape;

            xInShape = Blocks.MaxElement - yInShape;
            yInShape = tempxInShape;
        }

        x += xOffs;
        y += yOffs;
    }

    public int GetXPos() { return x; }
    public int GetYPos() { return y; }
    public Color GetColor() { return clr; }
}

class Shape {
    protected Vector Elements;
    protected int Value;
    protected boolean DrawReady = true;

    public Shape() {
        DrawReady = true;
    }

    public void Init() {
        DrawReady = true;
        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement = (Element) Elements.elementAt(ix);
            WalkElement.Init();
        }
   }

    public Shape(int a, int b, int c, int d, Color clr, int v) {
        Value = v;
        Elements = new Vector();

        AddElements(0, a, clr);
        AddElements(1, b, clr);
        AddElements(2, c, clr);
        AddElements(3, d, clr);

        Init();
    }

    protected void AddElements (int row, int a, Color clr) {
        if ((a & 0xf000) > 0) Elements.addElement (new Element(0, row, clr));
        if ((a & 0x0f00) > 0) Elements.addElement (new Element(1, row, clr));
        if ((a & 0x00f0) > 0) Elements.addElement (new Element(2, row, clr));
        if ((a & 0x000f) > 0) Elements.addElement (new Element(3, row, clr));
	}

    public void hide (Graphics g, int xOffs) {
        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement = (Element) Elements.elementAt(ix);
            WalkElement.hide(g, xOffs, 0);
        }
    }

    public void Display (Graphics g, int xOffs) {
        Element WalkElement;
        DrawReady = false;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement = (Element) Elements.elementAt(ix);
            WalkElement.Display(g, xOffs, 0);
		}

        DrawReady = true;
    }



   public void DisplayAbs (Graphics g, int xAbs, int yAbs) {

        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement = (Element) Elements.elementAt(ix);
            WalkElement.DisplayAbs(g, xAbs, yAbs);
		}
	}



    public boolean CheckIfShapeFits(Color PlayField[][], int xOffs, int yOffs,
                                    boolean Rotate) {

        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement= (Element) Elements.elementAt(ix);
            if (!WalkElement.CheckIfElementFits
                   (PlayField, xOffs, yOffs, Rotate)) return false;
        }

        return true;
    }

    public void ChangePosition(int xOffs, int yOffs, boolean Rotate) {
        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement = (Element) Elements.elementAt(ix);
            WalkElement.ChangeElementPosition(xOffs, yOffs, Rotate);
        }
    }

    public void PlaceInPlayField(Color PlayField[][]) {
        Element WalkElement;

        for (int ix = 0; ix < Elements.size(); ix++) {
            WalkElement= (Element) Elements.elementAt(ix);
            PlayField[WalkElement.GetXPos()]
                     [WalkElement.GetYPos()] = WalkElement.GetColor();
        }
    }

    public int GetValue() { return Value; }
    public boolean IsReady() { return DrawReady; }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -