2012-02-14 66 views
-1
public static final int SQUARE = 0, 
         LINE = 1, 
         S_FIGURE = 2, 
         Z_FIGURE = 3, 
         RIGHT_ANGLE_FIGURE = 4, 
         LEFT_ANGLE_FIGURE = 5, 
         TRIANGLE = 6; 

這裏是我的俄羅斯方塊MIDlet/J2ME代碼塊。但我不知道如何解釋這些參數。任何人都可以給我建議嗎?讓我知道你是否需要完整的代碼才能工作。俄羅斯方塊midlet屬性

這是我的全碼:

public class Block { 
public static final int SQUARE = 0, 
         LINE = 1, 
         S_FIGURE = 2, 
         Z_FIGURE = 3, 
         RIGHT_ANGLE_FIGURE = 4, 
         LEFT_ANGLE_FIGURE = 5, 
         TRIANGLE = 6; 
public int kind; 
public int color; 
/** 
* The horizontal figure position on the board. This value has no 
* meaning when the figure is not attached to a square board. 
*/ 
public 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. 
*/ 
public 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; 

public int maxOrientation; 

public int[] shapeX, shapeY; 

private GameCanvas board = null; 

public Block(int k) { 
    shapeX = new int[4]; 
    shapeY = new int[4]; 
    initialize(k); 
    this.kind = k; 
} 

private void initialize(int kind) { 
    switch (kind) { 
     case SQUARE : 
      maxOrientation = 1; 
      color = 0x0000ff; // blau 
      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 : 
      maxOrientation = 2; 
      color = 0xff0000; // rot 
      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 = 0xff00ff; // lila 
      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 = 0x008000; // grün 
      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 = 0x800080; // violett 
      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 = 0x000000; // schwarz 
      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 : 
      maxOrientation = 4; // gelb 
      color = 0xCECE00; 
      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; 
    } 
} 

/** 
* 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; 
} 

public boolean attach(GameCanvas 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) < 0 && getRelativeY(i, orientation)<newY) { 
       newY = Math.abs(getRelativeY(i, orientation))+1; 
      } 
     } 

    } 

    // Check position 
    this.board = board; 
    if (!canMoveTo(newX, newY, orientation)) { 
     this.board = null; 
     return false; 
    } 

    // Draw figure 
    xPos = newX; 
    yPos = newY; 
    paint(color); 
    board.repaint(); 

    return true; 
} 

/** 
* Checks if the figure is fully visible on the square board. If 
* the figure isn't attached to a board, false will be returned. 
* 
* @return true if the figure is fully visible, or 
*   false otherwise 
*/ 
public boolean isAllVisible() { 
    if (!isAttached()) { 
     return false; 
    } 
    for (int i = 0; i < shapeX.length; i++) { 
     if (yPos + getRelativeY(i, orientation) < 0) { 
      return false; 
     } 
    } 
    return true; 
} 

/** 
* Detaches this figure from its square board. The figure will not 
* be removed from the board by this operation, resulting in the 
* figure being left intact. 
*/ 
public void detach() { 
    board = null; 
} 

/** 
* Checks if the figure has landed. If this method returns true, 
* the moveDown() or the moveAllWayDown() methods should have no 
* effect. If no square board is attached, this method will return 
* true. 
* 
* @return true if the figure has landed, or false otherwise 
*/ 
public boolean hasLanded() { 
    return !isAttached() || !canMoveTo(xPos, yPos + 1, orientation); 
} 

/** 
* Moves the figure one step to the left. If such a move is not 
* possible with respect to the square board, nothing is done. The 
* square board will be changed as the figure moves, clearing the 
* previous cells. If no square board is attached, nothing is 
* done. 
*/ 
public void moveLeft() { 
    if (isAttached() && canMoveTo(xPos - 1, yPos, orientation)) { 
     paint(-1); 
     xPos--; 
     paint(color); 
     board.repaint(); 
    } 
} 

/** 
* Moves the figure one step to the right. If such a move is not 
* possible with respect to the square board, nothing is done. The 
* square board will be changed as the figure moves, clearing the 
* previous cells. If no square board is attached, nothing is 
* done. 
*/ 
public void moveRight() { 
    if (isAttached() && canMoveTo(xPos + 1, yPos, orientation)) { 
     paint(-1); 
     xPos++; 
     paint(color); 
     board.repaint(); 
    } 
} 

/** 
* Moves the figure one step down. If such a move is not possible 
* with respect to the square board, nothing is done. The square 
* board will be changed as the figure moves, clearing the 
* previous cells. If no square board is attached, nothing is 
* done. 
*/ 
public void moveDown() { 
    if (isAttached() && canMoveTo(xPos, yPos + 1, orientation)) { 
     paint(-1); 
     yPos++; 
     paint(color); 
     board.repaint(); 
    } 
} 

/** 
* Moves the figure all the way down. The limits of the move are 
* either the square board bottom, or squares not being empty. If 
* no move is possible with respect to the square board, nothing 
* is done. The square board will be changed as the figure moves, 
* clearing the previous cells. If no square board is attached, 
* nothing is done. 
*/ 
public void moveAllWayDown() { 
    int y = yPos; 

    // Check for board 
    if (!isAttached()) { 
     return; 
    } 

    // Find lowest position 
    while (canMoveTo(xPos, y + 1, orientation)) { 
     y++; 
    } 

    // Update 
    if (y != yPos) { 
     paint(-1); 
     yPos = y; 
     paint(color); 
     board.repaint(); 
    } 
} 

/** 
* Returns the current figure rotation (orientation). 
* 
* @return the current figure rotation 
*/ 
public int getRotation() { 
    return orientation; 
} 

/** 
* Sets the figure rotation (orientation). If the desired rotation 
* is not possible with respect to the square board, nothing is 
* done. The square board will be changed as the figure moves, 
* clearing the previous cells. If no square board is attached, 
* the rotation is performed directly. 
* 
* @param rotation the new figure orientation 
*/ 
public void setRotation(int rotation) { 
    int newOrientation; 

    // Set new orientation 
    newOrientation = rotation % maxOrientation; 

    // Check new position 
    if (!isAttached()) { 
     orientation = newOrientation; 
    } else if (canMoveTo(xPos, yPos, newOrientation)) { 
     paint(-1); 
     orientation = newOrientation; 
     paint(color); 
     board.repaint(); 
    } 
} 

/** 
* Rotates the figure clockwise. If such a rotation is not 
* possible with respect to the square board, nothing is done. 
* The square board will be changed as the figure moves, 
* clearing the previous cells. If no square board is attached, 
* the rotation is performed directly. 
*/ 
public void rotateClockwise() { 
    if (maxOrientation == 1) { 
     return; 
    } else { 
     setRotation((orientation + 1) % maxOrientation); 
    } 
} 

/** 
* Rotates the figure counter-clockwise. If such a rotation 
* is not possible with respect to the square board, nothing 
* is done. The square board will be changed as the figure 
* moves, clearing the previous cells. If no square board is 
* attached, the rotation is performed directly. 
*/ 
public void rotateCounterClockwise() { 
    if (maxOrientation == 1) { 
     return; 
    } else { 
     setRotation((orientation + 3) % 4); 
    } 
} 

/** 
* Checks if a specified pair of (square) coordinates are inside 
* the figure, or not. 
* 
* @param x   the horizontal position 
* @param y   the vertical position 
* 
* @return true if the coordinates are inside the figure, or 
*   false otherwise 
*/ 
private boolean isInside(int x, int y) { 
    for (int i = 0; i < shapeX.length; i++) { 
     if (x == xPos + getRelativeX(i, orientation) 
       && y == yPos + getRelativeY(i, orientation)) { 

      return true; 
     } 
    } 
    return false; 
} 

/** 
* Checks if the figure can move to a new position. The current 
* figure position is taken into account when checking for 
* collisions. If a collision is detected, this method will return 
* false. 
* 
* @param newX   the new horizontal position 
* @param newY   the new vertical position 
* @param newOrientation the new orientation (rotation) 
* 
* @return true if the figure can be moved, or 
*   false otherwise 
*/ 
private boolean canMoveTo(int newX, int newY, int newOrientation) { 
    int x; 
    int y; 

    for (int i = 0; i < 4; i++) { 
     x = newX + getRelativeX(i, newOrientation); 
     y = newY + getRelativeY(i, newOrientation); 
     if (!isInside(x, y) && !board.isSquareEmpty(x, y)) { 
      return false; 
     } 
    } 
    return true; 
} 

/** 
* Returns the relative horizontal position of a specified square. 
* The square will be rotated according to the specified 
* orientation. 
* 
* @param square  the square to rotate (0-3) 
* @param orientation the orientation to use (0-3) 
* 
* @return the rotated relative horizontal position 
*/ 
public int getRelativeX(int square, int orientation) { 
    switch (orientation % 4) { 
     case 0 : 
      return shapeX[square]; 
     case 1 : 
      return -shapeY[square]; 
     case 2 : 
      return -shapeX[square]; 
     case 3 : 
      return shapeY[square]; 
     default: 
      return 0; // Should never occur 
    } 
} 

/** 
* Rotates the relative vertical position of a specified square. 
* The square will be rotated according to the specified 
* orientation. 
* 
* @param square  the square to rotate (0-3) 
* @param orientation the orientation to use (0-3) 
* 
* @return the rotated relative vertical position 
*/ 
public int getRelativeY(int square, int orientation) { 
    switch (orientation % 4) { 
     case 0 : 
      return shapeY[square]; 
     case 1 : 
      return shapeX[square]; 
     case 2 : 
      return -shapeY[square]; 
     case 3 : 
      return -shapeX[square]; 
     default: 
      return 0; // Should never occur 
    } 
} 

/** 
* Paints the figure on the board with the specified color. 
* 
* @param color  the color to paint with, or -1 for clearing 
*/ 
private void paint(int color) { 
    int x, y; 

    for (int i = 0; i < shapeX.length; i++) { 
     x = xPos + getRelativeX(i, orientation); 
     y = yPos + getRelativeY(i, orientation); 
     board.setSquareColor(x, y, color); 
    } 
} 

}

+2

是什麼問題? – vulkanino 2012-02-14 12:08:54

+1

問題是? – 2012-02-14 12:09:34

+0

我沒有得到第一句話。這是*你的*俄羅斯方塊代碼,你不明白它? – home 2012-02-14 15:02:18

回答

-1

首先,我認爲你在尋找什麼是一個枚舉:

public enum Tetrimonos{ 
    SQUARE , LINE, S_FIGURE, Z_FIGURE , 
    RIGHT_ANGLE_FIGURE, LEFT_ANGLE_FIGURE , TRIANGLE 
} 

使用常量是一個壞想法,來自舊語言的遺蹟。其次,你是什麼意思'解釋這些參數'?解釋你命名的每個東西的俄羅斯方塊結構?在這種情況下,你應該去相關的文獻:一個俄羅斯方塊片被稱爲tetrimono,每個被稱爲一封信。看看這裏: http://en.wikipedia.org/wiki/Tetris#Gameplay

你也可以在你的文檔中鏈接到這個。

+4

Java Micro Edition僅與Java 1.3兼容,不支持枚舉。 – 2012-02-14 12:24:23

+0

呃,夥計們,我想感謝你們的幫助。問題是我很難理解在畫布上定位的代碼。我想我可以問你們......但是,好吧,謝謝Gilthans,我現在有一個大概的想法,現在它有點代表了俄羅斯方塊的形狀......並且會考慮它如何在畫布midlet中安排自己的位置。非常感謝幫助人員。項目的到期時間:( – jan 2012-02-15 16:19:34