1. 與運動場的細胞一起工作

很高興我們可以將運動場劃分為單元格。但是我們能用細胞本身做什麼呢?

對於運動場的每個單元格,我們可以設置:

  • 單元格顏色(單元格的背景顏色);
  • 文本(可以是文本或數字);
  • 文字顏色;
  • 文本大小占單元格大小的百分比。

讓我們考慮使用運動場單元格的方法:

void setCellColor(int x, int y, Color color)(x, y)設置坐標等於 的單元格的顏色color

例子:

setCellColor(0, 0, Color.RED);
setCellColor(3, 6, Color.BLACK);
setCellColor(6, 8, Color.NONE);

Color getCellColor(int x, int y)返回坐標為 的單元格的顏色(x, y)

例子:

Color myColor = getCellColor(2, 0);

void setCellValue(int x, int y, String value)將文本分配String value給坐標為 的單元格(x, y)

例子:

setCellValue(3, 3, "text");
setCellValue(0, 8, "W");
setCellValue(4, 1, "2222");
setCellValue(6, 6, "");

String getCellValue(int x, int y)返回坐標為 的單元格中包含的文本(x, y)

例子:

String s = getCellValue(3, 3);
System.out.println(getCellValue(4, 1));

void setCellTextSize(int x, int y, int size)使用坐標設置單元格中文本的大小(x, y),其中size文本高度佔單元格高度的百分比。

例子:

setCellTextSize(2, 0, 70); // 70% of the cell height

int getCellTextSize(int x, int y)返回坐標為 的單元格中內容的大小(x, y)

例子:

int size = getCellTextSize(2 , 0);

void setCellNumber(int x, int y, int value)將數字分配int value給坐標為 的單元格(x, y)

例子:

setCellNumber(3, 3, 40);
setCellNumber(0, 8, -8);
setCellNumber(4, 1, 2222);
setCellNumber(6, 6, 0);

int getCellNumber(int x, int y)返回坐標為 的單元格中包含的數字(x, y)。如果單元格不包含數字,則返回 0。

例子:

int i = getCellNumber(3, 3);
System.out.println(getCellNumber(4, 1));

void setCellTextColor(int x, int y, Color color)設置坐標為 的單元格內容(文本)的顏色(x, y)

例子:

setCellTextColor(2, 1, Color.GREEN);
setCellTextColor(0, 1, Color.NONE);

Color getCellTextColor(int x, int y)返回坐標為 的單元格內容(文本)的顏色(x, y)

例子:

Color textColor = getCellTextColor(1, 3);

為了您的方便,有多種setCellValueEx()具有不同參數集的方法:

void setCellValueEx(int x, int y, Color cellColor, String value)分別設置坐標(x, y)等於cellColor和 的單元格的背景顏色和文本value

例子:

setCellValueEx(0, 2, Color.BLUE, "56");

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor)分別設置坐標(x, y)等於cellColor、 、value和 的單元格的背景顏色、文本和文本顏色textColor

例子:

setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN);

void setCellValueEx(int x, int y, Color cellColor, String value, Color textColor, int textSize);分別設置坐標(x, y)cellColor、、 和 的單元格的背景顏色、文本、value文本顏色和文本大小。textColortextSize

例子:

setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN, 70);


2. 使用顏色

CodeGym 遊戲引擎有一個特殊Color類型,其中包含 148 種顏色的唯一值。它還具有NONE表示沒有顏色的特殊值。

使用顏色的示例

Color myColor = Color.WHITE;  // The color white is assigned to the myColor variable.
Color redColor = Color.RED; // The color red is assigned to the redColor variable.
Color blueColor = Color.BLUE; // The color blue is assigned to the blueColor variable.

您可以使用以下命令將單元格著色為紅色:

setCellColor(0, 2, Color.RED);

您可以使用如下命令檢查單元格是否為特定顏色:

if (getCellColor(0,2) == Color.GREEN)
{
}

有時您可能需要獲得包含每種可能顏色的數組。為此,請使用該values()方法。

例子:

// An array containing every available color is assigned to the colors variable.
Color[] colors = Color.values();

在調色板中獲取顏色的索引非常容易——只需使用以下ordinal()方法:

Color color = Color.RED;
int redIndex = color.ordinal(); // Index of the color red

int blueIndex = Color.BLUE.ordinal(); // Index of the color blue

您還可以通過其索引輕鬆獲取顏色:

 // The color whose index is 10 in the Color enum is assigned to the color variable.
Color color = Color.values()[10];


3.對話框

在遊戲結束時,我們需要讓玩家知道他或她是贏了還是輸了。對於這種情況和其他情況,CodeGym 遊戲引擎有一種特殊的void showMessageDialog(Color cellColor, String message, Color textColor, int textSize)方法,它會顯示一個帶有消息的對話框message
這個方法的參數是:

  • cellColor是對話框的背景顏色
  • message是消息的文本
  • textColor是消息文本的顏色
  • textSize是消息文本的大小

如果用戶按下空格鍵或用鼠標單擊對話框,對話框將自行關閉。

調用此方法的示例:

// Display a dialog box with a message
showMessageDialog(Color.BLACK, "EPIC FAIL", Color.RED, 80);


4.實用方法

在編寫遊戲時,您會經常使用隨機數。為了更容易獲得隨機數,您可以使用遊戲引擎的實用方法:

int getRandomNumber(int max)0返回一個從到包含的隨機數(max–1)

int getRandomNumber(int min, int max)min返回一個從到包含的隨機數(max–1)


5. JDK 11+

從 IntelliJ IDEA 運行程序時,繼承 Game 類的類可能會產生以下錯誤:

Error: JavaFX runtime components are missing, and are required to run this application

在這種情況下,對於每個此類,您需要執行一次這些步驟:
  1. 打開運行編輯配置
  2. 對於VM options的值,輸入以下內容:
    --module-path ./lib/javafx-sdk-16/lib --add-modules=javafx.controls,javafx.fxml,javafx.base

    注意力:

    在最新版本的 IntelliJ IDEA 中,默認情況下不顯示“VM 選項”字段。要顯示它,請按ALT+V

  3. 按:應用確定
  4. 運行遊戲。