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

📄 figure.java

📁 tetris是一款用java编写的小游戏--俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)Figure.java * * This work is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This work is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * Copyright (c) 2003 Per Cederberg. All rights reserved. */package net.percederberg.tetris;import java.awt.Color;/** * A class representing a Tetris square figure. Each figure consists  * of four connected squares in one of seven possible constellations.  * The figures may be rotated in 90 degree steps and have sideways and  * downwards movability.<p> *  * Each figure instance can have two states, either attached to a  * square board or not. When attached, all move and rotation  * operations are checked so that collisions do not occur with other * squares on the board. When not attached, any rotation can be made  * (and will be kept when attached to a new board). * * @version  1.2 * @author   Per Cederberg, per@percederberg.net */public class Figure extends Object {    /**     * A figure constant used to create a figure forming a square.     */    public static final int SQUARE_FIGURE = 1;    /**     * A figure constant used to create a figure forming a line.     */    public static final int LINE_FIGURE = 2;    /**     * A figure constant used to create a figure forming an "S".     */    public static final int S_FIGURE = 3;    /**     * A figure constant used to create a figure forming a "Z".     */    public static final int Z_FIGURE = 4;    /**     * A figure constant used to create a figure forming a right angle.     */    public static final int RIGHT_ANGLE_FIGURE = 5;    /**     * A figure constant used to create a figure forming a left angle.     */    public static final int LEFT_ANGLE_FIGURE = 6;    /**     * A figure constant used to create a figure forming a triangle.     */    public static final int TRIANGLE_FIGURE = 7;    /**     * The square board to which the figure is attached. If this      * variable is set to null, the figure is not attached.     */    private SquareBoard board = null;    /**     * The horizontal figure position on the board. This value has no     * meaning when the figure is not attached to a square board.     */    private int xPos = 0;    /**     * The vertical figure position on the board. This value has no     * meaning when the figure is not attached to a square board.     */    private int yPos = 0;    /**     * The figure orientation (or rotation). This value is normally      * between 0 and 3, but must also be less than the maxOrientation      * value.     *      * @see #maxOrientation     */    private int orientation = 0;    /**     * The maximum allowed orientation number. This is used to reduce      * the number of possible rotations for some figures, such as the     * square figure. If this value is not used, the square figure      * will be possible to rotate around one of its squares, which      * gives an erroneous effect.     *      * @see #orientation     */    private int maxOrientation = 4;    /**     * The horizontal coordinates of the figure shape. The coordinates      * are relative to the current figure position and orientation.     */    private int[] shapeX = new int[4];    /**     * The vertical coordinates of the figure shape. The coordinates      * are relative to the current figure position and orientation.     */    private int[] shapeY = new int[4];    /**     * The figure color.     */    private Color color = Color.white;    /**     * Creates a new figure of one of the seven predefined types. The     * figure will not be attached to any square board and default     * colors and orientations will be assigned.     *     * @param type      the figure type (one of the figure constants)     *      * @see #SQUARE_FIGURE     * @see #LINE_FIGURE     * @see #S_FIGURE     * @see #Z_FIGURE     * @see #RIGHT_ANGLE_FIGURE     * @see #LEFT_ANGLE_FIGURE     * @see #TRIANGLE_FIGURE     *      * @throws IllegalArgumentException if the figure type specified     *             is not recognized     */    public Figure(int type) throws IllegalArgumentException {        initialize(type);    }        /**     * Initializes the instance variables for a specified figure type.     *      * @param type      the figure type (one of the figure constants)     *      * @see #SQUARE_FIGURE     * @see #LINE_FIGURE     * @see #S_FIGURE     * @see #Z_FIGURE     * @see #RIGHT_ANGLE_FIGURE     * @see #LEFT_ANGLE_FIGURE     * @see #TRIANGLE_FIGURE     *      * @throws IllegalArgumentException if the figure type specified     *             is not recognized     */    private void initialize(int type) throws IllegalArgumentException {                // Initialize default variables        board = null;        xPos = 0;        yPos = 0;        orientation = 0;        // Initialize figure type variables        switch (type) {        case SQUARE_FIGURE :            maxOrientation = 1;            color = Configuration.getColor("figure.square", "#ffd8b1");            shapeX[0] = -1;            shapeY[0] = 0;            shapeX[1] = 0;            shapeY[1] = 0;            shapeX[2] = -1;            shapeY[2] = 1;            shapeX[3] = 0;            shapeY[3] = 1;            break;        case LINE_FIGURE :            maxOrientation = 2;            color = Configuration.getColor("figure.line", "#ffb4b4");            shapeX[0] = -2;            shapeY[0] = 0;            shapeX[1] = -1;            shapeY[1] = 0;            shapeX[2] = 0;            shapeY[2] = 0;            shapeX[3] = 1;            shapeY[3] = 0;            break;        case S_FIGURE :            maxOrientation = 2;            color = Configuration.getColor("figure.s", "#a3d5ee");            shapeX[0] = 0;            shapeY[0] = 0;            shapeX[1] = 1;            shapeY[1] = 0;            shapeX[2] = -1;            shapeY[2] = 1;            shapeX[3] = 0;            shapeY[3] = 1;            break;        case Z_FIGURE :            maxOrientation = 2;            color = Configuration.getColor("figure.z", "#f4adff");            shapeX[0] = -1;            shapeY[0] = 0;            shapeX[1] = 0;            shapeY[1] = 0;            shapeX[2] = 0;            shapeY[2] = 1;            shapeX[3] = 1;            shapeY[3] = 1;            break;        case RIGHT_ANGLE_FIGURE :            maxOrientation = 4;            color = Configuration.getColor("figure.right", "#c0b6fa");            shapeX[0] = -1;            shapeY[0] = 0;            shapeX[1] = 0;            shapeY[1] = 0;            shapeX[2] = 1;            shapeY[2] = 0;            shapeX[3] = 1;            shapeY[3] = 1;            break;        case LEFT_ANGLE_FIGURE :            maxOrientation = 4;            color = Configuration.getColor("figure.left", "#f5f4a7");            shapeX[0] = -1;            shapeY[0] = 0;            shapeX[1] = 0;            shapeY[1] = 0;            shapeX[2] = 1;            shapeY[2] = 0;            shapeX[3] = -1;            shapeY[3] = 1;            break;        case TRIANGLE_FIGURE :            maxOrientation = 4;            color = Configuration.getColor("figure.triangle", "#a4d9b6");            shapeX[0] = -1;            shapeY[0] = 0;            shapeX[1] = 0;            shapeY[1] = 0;            shapeX[2] = 1;            shapeY[2] = 0;            shapeX[3] = 0;            shapeY[3] = 1;            break;        default :            throw new IllegalArgumentException("No figure constant: " +                                                type);        }    }    /**     * Checks if this figure is attached to a square board.     *      * @return true if the figure is already attached, or     *         false otherwise     */    public boolean isAttached() {        return board != null;    }    /**     * Attaches the figure to a specified square board. The figure      * will be drawn either at the absolute top of the board, with      * only the bottom line visible, or centered onto the board. In      * both cases, the squares on the new board are checked for      * collisions. If the squares are already occupied, this method     * returns false and no attachment is made.<p>     *     * The horizontal and vertical coordinates will be reset for the      * figure, when centering the figure on the new board. The figure     * orientation (rotation) will be kept, however. If the figure was     * previously attached to another board, it will be detached from     * that board before attaching to the new board.     *     * @param board     the square board to attach to     * @param center    the centered position flag     *      * @return true if the figure could be attached, or     *         false otherwise     */    public boolean attach(SquareBoard board, boolean center) {        int  newX;        int  newY;        int  i;        // Check for previous attachment        if (isAttached()) {            detach();        }        // Reset position (for correct controls)        xPos = 0;        yPos = 0;        // Calculate position        newX = board.getBoardWidth() / 2;        if (center) {            newY = board.getBoardHeight() / 2;        } else {            newY = 0;            for (i = 0; i < shapeX.length; i++) {                if (getRelativeY(i, orientation) - newY > 0) {                    newY = -getRelativeY(i, orientation);                }            }        }        // Check position        

⌨️ 快捷键说明

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