Keyboard and mouse input - Gornova/MarteEngine GitHub Wiki
This tutorial assumes you have completed the Entity and World tutorial.
For every type of videogame you can create, the player can interact with it using keyboard or mouse: this tutorial explains how this is possible with MarteEngine!
Keyboard
In the last tutorial you learned how to place your player on the screen, but how to move it? MarteEngine let you work your way but offers some utility methods to simplify it:
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
bindToKey("RIGHT", Input.KEY_RIGHT);
}
}
As you can see it is very easy: just write a command name (for example here "RIGHT") and associate it with a key on your keyboard. Thanks to the Slick library you can access every key on your keyboard (for example here Input.KEY_RIGHT).
However, defining a command is not enough: what you need is to deal with it when the player presses a key. You can do it in the overridden update method of your Player Entity:
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
bindToKey("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;
}
}
}
In the code above you can see how simple it is to check if a command you named was triggered: you don't need to worry anymore about what key was pressed, the focus is on action. Even better, using commands allows you to map several different keys on the same command!
In this example you move the player 10 units/pixels to the right!
Obviously you can define any command you like and handle it the way you prefer!
Mouse
Mouse input follows the same concepts already explained: define a command (click with left mouse button) and action!
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
bindToMouse("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+")");
}
}
}
Now you are ready to follow the Basic collision tutorial !
MarteEngine version 0.3