This week I have been concentrating on making the character that I have drawn to the screen moving in certain directions decided by which buttons on the keyboard have been pressed. This was a much harder task than I first thought but after reading through the available documentation for Slick2D and looking once again at the sample code that is available to me I have created a working movement system for the game. The code I have written allows the user to press either WASD or The arrow keys on the keyboard to control the movement of the character, seeing if the keys are being pressed is helpfully implemented with the input class of Slick2D allowing me to easily check if a key is either been pressed or held down which is very handy indeed. If the button is pressed down the variables of Y and X which are the current co-ordinates are adjusted accordingly and then the image is redrawn at these co-ordinates once updated. The code I have written is below:
public void update(GameContainer gc, int delta) throws SlickException {
Input input = gc.getInput();
float floatDelta = delta*0.1f;
if (input.isKeyDown(Input.KEY_UP)){
y -= floatDelta;
}
if (input.isKeyDown(Input.KEY_DOWN))
{
y += floatDelta;
}
if (input.isKeyDown(Input.KEY_LEFT))
{
x -= floatDelta;
}
if (input.isKeyDown(Input.KEY_RIGHT))
{
x += floatDelta;
}
}
No comments:
Post a Comment