本教程假定你已经完成了Entity and World教程。
对于你创建的游戏的每一种类型,玩家都要通过键盘和鼠标与之交互:本教程解释了如何使用MarteEngine使之成为可能!
Keyboard
在上一个教程中你学习了如何在屏幕上放置你的英雄,但是怎样移动它呢?MarteEngine让你以你自己的方式工作但是提供了一些工具方法以简化它:public class Player extends Entity {
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("RIGHT", Input.KEY_RIGHT);
}
}
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("RIGHT", Input.KEY_RIGHT);
}
}
是不是非常简单:仅仅写了一个命令的名字(这里是“Right”)并把它关联到键盘上的一个按键。谢谢slick这个库你可以访问键盘上的每个键(例如这里的Input.KEY_RIGHT)
然而,定义一个命令还不够, 你所需要的是当玩家按一个键的时候处理它,你可以通过覆盖玩家Entity的update方法实现它:
public class Player extends Entity {
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("RIGHT", Input.KEY_RIGHT);
}
@Override
public void update(GameContainer container, int delta)
throws SlickException {
super.update(container, delta);
// check if a key is down
if(check("RIGHT")){
// do anything you like, for example:
x = x+10;
}
}
}
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("RIGHT", Input.KEY_RIGHT);
}
@Override
public void update(GameContainer container, int delta)
throws SlickException {
super.update(container, delta);
// check if a key is down
if(check("RIGHT")){
// do anything you like, for example:
x = x+10;
}
}
}
通过上面的代码可以看到检查一个你命名过的按键是否被触发是多么简单:你不必关心哪个键被按下,焦点是处理的动作。更好的是,使用命令允许你映射不同的按键到一个命令上。
在这个例子中,你向右移动英雄10个单位/像素。
显然你可以定义任何你喜欢的命令并以你喜欢的方式处理。
Mouse
鼠标输入遵循同样的概念:定义命令和动作。 public class Player extends Entity {
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("ATTACK", Input.MOUSE_LEFT_BUTTON);
}
@Override
public void update(GameContainer container, int delta)
throws SlickException {
super.update(container, delta);
Input input = container.getInput();
// check if a key is down or mouse is clicked
if(check("ATTACK")){
// do anything you like, for example:
float mouseX = input.getMouseX();
float mouseY = input.getMouseY();
System.out.println("Player fire at ("+mouseX+","+mouseY+")");
}
}
}
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define a command to handle input
define("ATTACK", Input.MOUSE_LEFT_BUTTON);
}
@Override
public void update(GameContainer container, int delta)
throws SlickException {
super.update(container, delta);
Input input = container.getInput();
// check if a key is down or mouse is clicked
if(check("ATTACK")){
// do anything you like, for example:
float mouseX = input.getMouseX();
float mouseY = input.getMouseY();
System.out.println("Player fire at ("+mouseX+","+mouseY+")");
}
}
}
现在你已经准备好Basic collision教程。