CodeGym /Java 博客 /随机的 /CodeGym 上的游戏部分:事件处理
John Squirrels
第 41 级
San Francisco

CodeGym 上的游戏部分:事件处理

已在 随机的 群组中发布
CodeGym 的“游戏”是一个新部分,其中包含大量有趣的任务,您可以在其中创建自己的流行游戏版本。很简单:每个游戏项目都分为子任务。一个一个地完成它们,游戏就完成了。您可以在社交网络上分享它并邀请您的朋友一起玩。CodeGym 上的“游戏”部分:事件处理 - 1在本文中,我们将详细描述用于编写游戏的 事件处理方法。

1.使用鼠标

游戏引擎有两种使用鼠标的方法:
  • void onMouseLeftClick(int x, int y);

  • void onMouseRightClick(int x, int y);

您只需在继承的类中声明这些方法Game,然后向其中添加您想要的任何代码。当用户单击鼠标按钮时,游戏引擎将调用它们。
  1. onMouseLeftClick(int x, int y)— 单击鼠标左键时由引擎调用。它的参数是发生点击的运动场单元格的坐标。左上角的单元格坐标为 (0, 0)。您需要覆盖此方法才能使用它。

  2. onMouseRightClick(int x, int y)— 单击鼠标右键时调用。此方法的工作原理与onMouseLeftClick(int x, int y)方法类似。

以下是使用这些方法的示例:

import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import com.codegym.engine.cell.Key;

public class MySuperGame extends Game {
    @Override
    public void initialize() {
        // Set the size of the playing field to 3x3
        setScreenSize(3, 3);

        // Paint the playing field white
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                setCellColor(x, y, Color.WHITE);
            }
        }
    }

    @Override
    public void onMouseLeftClick(int x, int y) {
        // Set "X" in the cell where the left mouse click occurred
        setCellValue(x, y, "X");
    }

    @Override
    public void onMouseRightClick(int x, int y) {
        // Clear the cell where the right mouse click occurred
        setCellValue(x, y, "");
    }
}

2. 使用键盘

游戏引擎有两种使用键盘的方法:
  • void onKeyPress(Key key);

  • void onKeyReleased(Key key);

如果您希望在用户按下某个键时发生某些事情,请在您继承 Game 的类中声明这些方法,并将您的代码添加到它们中。当用户按下或释放一个键时,游戏引擎将调用它们。
  1. onKeyPress(Key key)— 按下任意键时调用。关键参数是按下的键(或 Key.UNKNOWN)。

  2. onKeyReleased(Key key)— 释放任何键时调用。key 参数是相应的键(或 Key.UNKNOWN)。

以下是使用这些方法的示例:

import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;
import com.codegym.engine.cell.Key;

public class MySuperGame extends Game {
    @Override
    public void initialize() {
        // Set the size of the playing field to 3x3
        setScreenSize(3, 3);

        // Paint the playing field white
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                setCellColor(x, y, Color.WHITE);
            }
        }
    }

    @Override
    public void onKeyPress(Key key) {
        // When the space bar is pressed, the center cell turns yellow
        if (key == Key.SPACE) {
            setCellColor(1, 1, Color.YELLOW);
        }
    }

    @Override
    public void onKeyReleased(Key key) {
        // When the space bar is released, the center cell changes back to white
        if (key == Key.SPACE) {
            setCellColor(1, 1, Color.WHITE);
        }
    }
}
重要的! 在当前版本的游戏引擎中,Key 类型只有九个值:
价值 用户按下的键
键.ENTER 用户按下 Enter
键.ESCAPE 用户按下 Esc
键.PAUSE 用户按下暂停
键空间 用户按下空格键
键.左 用户按下左箭头键
键.RIGHT 用户按下右箭头键
键.UP 用户按下向上箭头键
按键.DOWN 用户按下了向下键
钥匙.未知 用户按下了上述以外的键

3.使用定时器

许多游戏是实时发生的,即即使用户什么都不做,游戏中仍然会发生事件。为了让您能够实现此类游戏,我们在游戏引擎中添加了一个计时器。它的工作原理是这样的:你打开定时器并设置定时器间隔。例如,500 毫秒。然后,游戏引擎每半秒调用一次该onTurnTimer()方法。永远一次又一次——直到定时器被关闭。那么如何使用定时器呢?
  1. 打开定时器。

    为此,有一种特殊的void setTurnTimer(int timeMs)方法。该方法以毫秒为单位的调用间隔作为参数(1 毫秒 = 1/1000 秒)。您只需要调用一次,游戏引擎将onTurn()在每 timeMs 毫秒开始调用该方法。

  2. 覆盖 onTurn(int) 方法。

    为此,您需要void onTurn(int step)在继承的类中声明一个方法Game。该方法将由游戏引擎调用。此外,对于每次调用,游戏引擎都会将调用的顺序标识符(1、2、3、...)传递给该方法。

  3. 关闭定时器。

    如果不再需要计时器,例如,当用户完成游戏时,您可以将其关闭。为此,您只需调用该stopTurnTimer()方法。

  4. 加速或更改计时器。

    在某些游戏中,事件发生的速度不断加快,因此加快我们的计时器(减少调用之间的时间)会很方便。没有比这更容易的了:使用setTurnTimer(int timeMs)新值再次调用,onTurn()调用之间的时间将会改变。

例如:

import com.codegym.engine.cell.Color;
import com.codegym.engine.cell.Game;

public class MySuperGame extends Game {
    …
    @Override
    public void initialize() {
    	// Create a playing field that is 3 cells x 3 cells
        setScreenSize(3, 3);
        showGrid(false);
        setCellValueEx(1, 1, Color.BLUE, "X", Color.ORANGE, 50);

        setTurnTimer(500);   // Turn on the timer, the interval between calls is 500ms.
    }

    @Override
    public void onTurn(int step) {
        if(step == 100) {
            stopTurnTimer();  // If 100 calls have been made, turn off the timer
        }

        if (step % 2 == 1) {
            // If this call is odd, set the cell background to red
            setCellColor(1, 1, Color.RED);
        } 
        else {
            // If this call is even, set the cell background to blue
            setCellColor(1, 1, Color.BLUE);

        }
    }
    …
}
在这个简单的示例中,我们创建了一个 3 单元格 x 3 单元格的字段。onTurn()然后我们启动了一个定时器,每半秒调用一次该方法。每半秒,单元格颜色会发生变化,但其内容不会改变。50 秒后,颜色将不再变化。目前为止就这样了!如果您想了解有关“游戏”部分的更多信息,这里有一些有用的文档可以提供帮助:
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION