CodeGym /Java 博客 /随机的 /CodeGym 上的游戏部分:游戏引擎
John Squirrels
第 41 级
San Francisco

CodeGym 上的游戏部分:游戏引擎

已在 随机的 群组中发布
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