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

📄 blocks.java

📁 使用java实现俄罗斯方块游戏。含有可运行文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.util.Vector;
import java.lang.Math;
import java.lang.Thread;
import java.lang.System;

public class Blocks extends java.applet.Applet implements Runnable {
    public static final int cols = 10;
    public static final int rows = 18;
    public static final int ElementSize = 15;
    public static final int MaxElement = 3;
    public static final Color BackGroundColor = Color.black;
    public static final int SOUND_DROP = 0;
    public static final int SOUND_GAMEOVER = 1;
    public static final int SOUND_NEXTLEVEL = 2;
    public static final int SOUND_LINE = 3;

    PlayFieldCanvas myPlayFieldCanvas;
    StatisticsCanvas Statistics;
    Thread killme = null;
    Vector ShapeSet;
    Shape FallingShape = null;
    Shape NextShape = null;
    Color PlayField[][];
	static AudioClip sounds[];

    public boolean GamePaused = true;

    public void init() {
        setLayout(new BorderLayout());
        Panel grid = new Panel();
        grid.setLayout(new GridLayout(0, 2));
        add("Center", grid);

        myPlayFieldCanvas = new PlayFieldCanvas();
        grid.add(myPlayFieldCanvas);

        Statistics = new StatisticsCanvas();
        grid.add(Statistics);

        Panel p = new Panel();
        p.setLayout(new FlowLayout());
        add("East", p);
        p.add(new Button("About"));
        p.add(new Button("Pause / Resume"));

        ShapeSet = new Vector();
        ShapeSet.addElement(new Shape(  0x0000 ,

                                        0x0FF0 ,

                                        0x0FF0 ,

                                        0x0000 , Color.blue, 3));



        ShapeSet.addElement(new Shape(  0x0F00 ,

                                        0x0F00 ,

                                        0x0FF0 ,

                                        0x0000 , Color.yellow, 5));



        ShapeSet.addElement(new Shape(  0x00F0 ,

                                        0x00F0 ,

                                        0x0FF0 ,

                                        0x0000 , Color.pink, 5));



        ShapeSet.addElement(new Shape(  0x0000 ,

                                        0x0F00 ,

                                        0xFFF0 ,

                                        0x0000 , Color.green, 4));



        ShapeSet.addElement(new Shape(  0x0F00 ,

                                        0x0F00 ,

                                        0x0F00 ,

                                        0x0F00 , Color.red, 4));



        ShapeSet.addElement(new Shape(  0x0F00 ,

                                        0x0FF0 ,

                                        0x00F0 ,

                                        0x0000 , Color.magenta, 4));



        ShapeSet.addElement(new Shape(  0x00F0 ,

                                        0x0FF0 ,

                                        0x0F00 ,

                                        0x0000 , Color.orange, 4));


        sounds = new AudioClip[4];
        sounds[0] = getAudioClip(getCodeBase(), "audio/drop.au");
        sounds[1] = getAudioClip(getCodeBase(), "audio/gameover.au");
        sounds[2] = getAudioClip(getCodeBase(), "audio/nextlevel.au");
        sounds[3] = getAudioClip(getCodeBase(), "audio/line.au");


        PlayField = new Color[cols][rows];
        myPlayFieldCanvas.SetPlayField(PlayField);

        InitNewGame();

        GetNextRandomShape();
    }

    public static void play(int n) {
        if (sounds[n] != null) {
            sounds[n].play();
        }
    }

    public void InitNewGame() {
        ClearPlayField();
        Statistics.InitNewGame();
    }

    public void ClearPlayField() {
        int x, y;

        for (x = 0; x < cols ; x++) {
            for (y = 0 ; y < rows ; y++) {
                PlayField[x][y] = Color.black;
            }
        }
    }

    public int GetRandomShapeNr() {
        int ShapeNr;

        do {
            ShapeNr = (int) (Math.random() * ShapeSet.size());
        } while (ShapeNr >= ShapeSet.size());

        return ShapeNr;
    }
	public void GetNextRandomShape() {
        if (FallingShape == null) {
                FallingShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr());
        } 		else {
                FallingShape = NextShape;
        }

        FallingShape.Init();

        if (!FallingShape.CheckIfShapeFits(PlayField, 0, 0, false)) {
            GameOver();
        }

        NextShape = (Shape) ShapeSet.elementAt(GetRandomShapeNr());

        Statistics.DisplayNextShape(NextShape);
    }

    public void GameOver() {
        play (SOUND_GAMEOVER);
        myPlayFieldCanvas.GameOver();
        Statistics.GameOver();
        InitNewGame();
    }

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

    public void stop() {
        killme = null;
    }



    public void run() {
        while (killme != null) {
            try {
                Thread.sleep(Statistics.GetGameSpeed());
            } 			catch (InterruptedException e){}

            if (!GamePaused) {
                if (FallingShape != null) {
                    if (FallingShape.CheckIfShapeFits(PlayField, 0, 1, false)) {
                        ChangeShapePosition(0, 1, false);
                    }					else {
                        play (SOUND_DROP);
                        FallingShape.PlaceInPlayField(PlayField);
                        myPlayFieldCanvas.RepaintPlayField();
                        Statistics.AddScore(FallingShape.GetValue());
                        CheckForFullLines();
                        GetNextRandomShape();
                    }
                }

                myPlayFieldCanvas.repaint(FallingShape);
            }
        }

        killme = null;
    }

    public void CheckForFullLines() {
        int Lines = 0;
        int x, y, yc;
        boolean Gap;

        for (y = 0; y < rows; y++) {
            Gap = false;
            for (x = 0; x < cols; x++) {
                if (PlayField[x][y] == Color.black) 					Gap = true;
            }

            if (!Gap) {
                Lines++;

                for (yc = y - 1; yc >= 0; yc--) {
                    for (x = 0; x < cols; x++) {
                        PlayField[x][yc + 1] = PlayField[x][yc];
                    }
                }

                for (x = 0; x < cols; x++) {    // Delete top row.
                    PlayField[x][0] = Color.black;
                }
            }
        }

        if (Lines > 0) {
            play (SOUND_LINE);
            myPlayFieldCanvas.RepaintPlayField();
        }

        Statistics.AddLines(Lines);
    }

    public boolean action(Event evt, Object arg) {
        if ("About".equals(arg)) {
            GamePaused = true;
            myPlayFieldCanvas.About();
        }

        if ("Pause / Resume".equals(arg)) {
            if (GamePaused) {
                myPlayFieldCanvas.ShowAboutBox = false;
                myPlayFieldCanvas.RepaintPlayField();
            }

            GamePaused = !GamePaused;
        }

        return true;
    }

    public synchronized boolean handleEvent(Event e) {
        int xChange = 0;
        int yChange = 0;
        boolean Rotate = false;

        switch (e.id) {
        case Event.ACTION_EVENT:
            return action(e, e.arg);			
        case Event.KEY_ACTION:			
        case Event.KEY_PRESS:
            switch (e.key) {
            case 'a':
            case 'A':           // Move block to left
                xChange--;
                break;

            case 'd':
            case 'D':           // Move block to right
                xChange++;
                break;
							case 's':
            case 'S':           // Move block down
                yChange++;
                break;

            case 'n':
            case 'N':           // Rotate block
                Rotate = true;
                break;

            default:
                return false;
            }
            break;

        default:
            return false;
        }

        ChangeShapePosition (xChange, yChange, Rotate);

        return true;
    }

    public void ChangeShapePosition(int xChange, int yChange, boolean Rotate) {
        while (!FallingShape.IsReady()) ;
        if (FallingShape.CheckIfShapeFits(PlayField, xChange, yChange, Rotate)) {
            FallingShape.ChangePosition(xChange, yChange, Rotate);
            myPlayFieldCanvas.repaint(FallingShape);
        }
    }
}

⌨️ 快捷键说明

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