
一、簡介
對於開發者來說,電腦遊戲的實施分為三個階段:-
遊戲初始化——這個階段包括準備動作:設置比賽場地的大小、繪製比賽場地、創建遊戲對象並將它們放置在初始位置,以及其他必須在遊戲開始時執行的動作。
-
遊戲過程。此階段包括移動遊戲對象、玩家動作、記分以及必須以特定頻率或按下按鈕時執行的其他動作。
-
遊戲完成。此階段包括停止動畫、報告輸贏以及必須在遊戲結束時執行的其他操作。
2.遊戲初始化
遊戲初始化僅包含兩個步驟: 第 1 步:創建遊戲的主類。 要使用 CodeGym 遊戲引擎開發您自己的遊戲,您需要創建繼承類Game
(com.codegym.engine.cell.Game) 的類。這使您的類能夠調用遊戲引擎的方法,並使引擎能夠調用您的方法。例如:
import com.codegym.engine.cell.Game;
public class MySuperGame extends Game {
...
}
第 2 步:覆蓋初始化方法 ()。 開始遊戲所需的所有操作都將在該方法中發生:創建比賽場地、創建遊戲對像等。您只需在繼承該類的類中聲明該方法即可Game
。例如:
import com.codegym.engine.cell.Game;
public class MySuperGame extends Game {
@Override
public void initialize() {
// Here we perform all actions to initialize the game and its objects
}
}
方法initialize()
類似於方法main()
:它是為遊戲編寫的所有代碼的起點。
3.創造一個競爭環境
創建一個運動場也只包含兩個步驟。 第 1 步:將運動場劃分為單元格。 整個比賽場地被遊戲引擎劃分為多個單元格。最小尺寸為 3x3;最大值為 100x100。遊戲畫面的尺寸是不變的。它可以分成不同數量的細胞。例如,7 個單元格寬 x 9 個單元格高:
void setScreenSize(int width, int height)
方法。它設定了比賽場地的大小。它的參數表示單元格水平(寬度)和垂直(高度)的數量。它通常在遊戲開始時被調用一次。例如:
import com.codegym.engine.cell.Game;
public class MySuperGame extends Game {
@Override
public void initialize() {
// Set the field size to 7 cells x 9 cells
setScreenSize(7, 9);
...
}
}
在編寫遊戲時,您可能需要獲取遊戲區域的當前寬度和高度。為此,int getScreenWidth()
和int getScreenHeight()
方法將派上用場。 第 2 步:打開或關閉網格(可選)。 如果您不喜歡在運動場上分隔單元格的黑色網格,您可以將其關閉。該void showGrid(boolean isShow)
方法打開和關閉網格。默認情況下,顯示網格。要關閉它,請使用 false 作為參數調用此方法:
showGrid(false);
結果: 
showGrid(true);
4. 原始程序
下面是一個程序示例:
public class MySuperGame extends Game {
@Override
public void initialize() {
// Create a playing field that is 3 cells x 3 cells
setScreenSize(3, 3);
// Turn off the grid
showGrid(false);
// Change the background of the center cell to blue, and display an "X" in it.
setCellValueEx(1, 1, Color.BLUE, "X", Color.ORANGE, 50);
}
}
在此示例中,遊戲區域設置為 3x3,網格已關閉,並且在中央單元格的藍色背景上設置了單元格大小一半的橙色“X”。這將是玩家在遊戲開始時看到的第一件事。
5. 使用運動場的細胞
我們可以將運動場劃分為細胞這一事實很棒,但是我們可以對細胞本身做什麼呢?您可以為運動場的每個單元格設置以下屬性:- 單元格顏色(單元格背景色);
- 文本(文本或數字);
- 文字顏色;
- 文本大小占單元格大小的百分比。
-
void setCellColor(int x, int y, Color color)
— 設置坐標為 (x, y) 的單元格的顏色: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)
— 將坐標 (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)
— 將坐標 (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)、文本顏色 (textColor) 和文本大小 (textSize):setCellValueEx(0, 2, Color.BLACK, "56", Color.GREEN, 70);
6. 使用顏色
負責Color enum
遊戲引擎中的顏色。它具有 148 種顏色的唯一值。它還有一個特殊值(NONE),表示沒有顏色。這是使用顏色的示例:
Color myColor = Color.WHITE; // The myColor variable is set to white.
Color redColor = Color.RED; // The redColor variable is set to red.
Color blueColor = Color.BLUE; // The blueColor variable is set to light blue.
有時您可能需要獲取所有現有顏色的數組。為此,請使用該values()
方法。例如:
Color[] colors = Color.values(); // The colors variable is assigned an array containing all available colors.
在調色板中獲取顏色索引非常容易:
Color color = Color.RED;
int redIndex = color.ordinal(); // Index of red
int blueIndex = Color.BLUE.ordinal(); // Index of blue
您還可以通過索引獲取顏色:
Color color = Color.values()[10]; // The color variable is assigned the color with index 10 in the Color enum.
7.對話框
遊戲結束時,需要向玩家報告輸贏。為此,有一種特殊的方法可以在遊戲屏幕上顯示一個對話框:
void showMessageDialog(Color cellColor, String message, Color textColor, int textSize)
這裡:
cellColor
是對話框的背景色;message
是消息的文本;textColor
是消息文本的顏色;textSize
是消息文本的大小。
8.實用方法
在寫遊戲的時候,隨機數用的比較多。為了更容易獲得隨機數,您可以使用遊戲引擎提供的以下實用方法:-
int getRandomNumber(int max)
— 返回從 0 到 (max-1) 的隨機整數。 -
int getRandomNumber(int min, int max)
— 返回從 min 到 (max-1) 的隨機整數。
GO TO FULL VERSION