CodeGym /Java Blog /Toto sisi /CodeGym 上的遊戲部分:遊戲引擎
John Squirrels
等級 41
San Francisco

CodeGym 上的遊戲部分:遊戲引擎

在 Toto sisi 群組發布
CodeGym 的“遊戲”是一個新的部分,其中包含涉及編寫流行計算機遊戲的重大任務。它們比看起來更容易創建:每個項目分為 20 個子任務。逐步完成任務,您將編寫自己的遊戲,然後您可以添加獨特的功能並與朋友分享。CodeGym 上的“遊戲”部分:遊戲引擎 - 1這些遊戲使用簡單的 CodeGym 遊戲引擎。在本文中,我們將描述其主要功能以及遊戲編寫過程。

一、簡介

對於開發者來說,電腦遊戲的實施分為三個階段:
  1. 遊戲初始化——這個階段包括準備動作:設置比賽場地的大小、繪製比賽場地、創建遊戲對象並將它們放置在初始位置,以及其他必須在遊戲開始時執行的動作。

  2. 遊戲過程。此階段包括移動遊戲對象、玩家動作、記分以及必須以特定頻率或按下按鈕時執行的其他動作。

  3. 遊戲完成。此階段包括停止動畫、報告輸贏以及必須在遊戲結束時執行的其他操作。

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 個單元格高: CodeGym 上的“遊戲”部分:遊戲引擎 - 2請注意,單元格從左上角開始編號。要設置比賽場地的大小,請使用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);
結果: CodeGym 上的“遊戲”部分:遊戲引擎 - 3要重新打開網格,請調用:

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. 使用運動場的細胞

我們可以將運動場劃分為細胞這一事實很棒,但是我們可以對細胞本身做什麼呢?您可以為運動場的每個單元格設置以下屬性:
  • 單元格顏色(單元格背景色);
  • 文本(文本或數字);
  • 文字顏色;
  • 文本大小占單元格大小的百分比。
讓我們看看使用運動場單元格的方法
  1. 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);
    

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

    
    Color myColor = getCellColor(2, 0);
    

  3. 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, "");
    

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

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

  5. void setCellTextSize(int x, int y, int size)— 設置坐標為 (x, y) 的單元格內容的大小。size 參數是文本高度佔單元格高度的百分比:

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

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

    
    int size = getCellTextSize(2 , 0);
    

  7. 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);
    

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

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

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

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

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

    
    Color textColor = getCellTextColor(1, 3);
    
為方便起見,有幾種setCellValueEx()具有不同參數集的方法:
  1. void setCellValueEx(int x, int y, Color cellColor, String value)— 設置坐標為 (x, y) 的單元格的背景顏色 (cellColor) 和內容 (value):

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

  2. 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);
    

  3. 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.實用方法

在寫遊戲的時候,隨機數用的比較多。為了更容易獲得隨機數,您可以使用遊戲引擎提供的以下實用方法:
  1. int getRandomNumber(int max)— 返回從 0 到 (max-1) 的隨機整數。

  2. int getRandomNumber(int min, int max)— 返回從 min 到 (max-1) 的隨機整數。

目前為止就這樣了!如果您想了解有關“遊戲”部分的更多信息,這裡有一些有用的文檔可以提供幫助:
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION