
1.使用鼠标
游戏引擎有两种使用鼠标的方法:-
void onMouseLeftClick(int x, int y);
-
void onMouseRightClick(int x, int y);
Game
,然后向其中添加您想要的任何代码。当用户单击鼠标按钮时,游戏引擎将调用它们。
-
onMouseLeftClick(int x, int y)
— 单击鼠标左键时由引擎调用。它的参数是发生点击的运动场单元格的坐标。左上角的单元格坐标为 (0, 0)。您需要覆盖此方法才能使用它。 -
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);
-
onKeyPress(Key key)
— 按下任意键时调用。关键参数是按下的键(或 Key.UNKNOWN)。 -
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()
方法。永远一次又一次——直到定时器被关闭。那么如何使用定时器呢?
-
打开定时器。
为此,有一种特殊的
void setTurnTimer(int timeMs)
方法。该方法以毫秒为单位的调用间隔作为参数(1 毫秒 = 1/1000 秒)。您只需要调用一次,游戏引擎将onTurn()
在每 timeMs 毫秒开始调用该方法。 -
覆盖 onTurn(int) 方法。
为此,您需要
void onTurn(int step)
在继承的类中声明一个方法Game
。该方法将由游戏引擎调用。此外,对于每次调用,游戏引擎都会将调用的顺序标识符(1、2、3、...)传递给该方法。 -
关闭定时器。
如果不再需要计时器,例如,当用户完成游戏时,您可以将其关闭。为此,您只需调用该
stopTurnTimer()
方法。 -
加速或更改计时器。
在某些游戏中,事件发生的速度不断加快,因此加快我们的计时器(减少调用之间的时间)会很方便。没有比这更容易的了:使用
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 秒后,颜色将不再变化。目前为止就这样了!如果您想了解有关“游戏”部分的更多信息,这里有一些有用的文档可以提供帮助:
GO TO FULL VERSION