Skip to content

Instantly share code, notes, and snippets.

@homelinen
Created September 25, 2011 16:30
Show Gist options
  • Save homelinen/1240805 to your computer and use it in GitHub Desktop.
Save homelinen/1240805 to your computer and use it in GitHub Desktop.
My absolutely horrible keyBoard inut method.
/**
* Check the keyboard input and set the player to move
*/
public void keyboardInput() {
int playerx = thePlayer.getxPos() /theDisplay.getCellWidth();
int playery = thePlayer.getyPos() / theDisplay.getCellHeight();
if (theInputManager.isAnyKeyPressed()) {
System.out.println("x: " + playerx + " y: " + playery);
if (theInputManager.isKeyPressed(InputManager.VK_KP_LEFT)) {
//Check to see if the left cell is passable
if (theMaze.isCellPassable((playerx - 1), playery)) {
thePlayer.setChangex(-1);
}
} else if (theInputManager.isKeyPressed(InputManager.VK_KP_RIGHT)) {
//Check to see if the right cell is passable
if (theMaze.isCellPassable((playerx + 1), playery)) {
thePlayer.setChangex(1);
}
} else if (theInputManager.isKeyPressed(InputManager.VK_KP_UP)) {
//Check to see if the top cell is passable
if (theMaze.isCellPassable(playerx, (playery - 1))) {
thePlayer.setChangey(1);
}
} else if (theInputManager.isKeyPressed(InputManager.VK_KP_DOWN)) {
//Check to see if the bottom cell is passable
if (theMaze.isCellPassable(playerx, (playery-1))) {
thePlayer.setChangey(-1);
}
}
} else {
boolean xAxesRel = theInputManager.isKeyReleased(InputManager.VK_KP_LEFT) || theInputManager.isKeyReleased(InputManager.VK_KP_RIGHT);
boolean yAxesRel = theInputManager.isKeyReleased(InputManager.VK_KP_UP) || theInputManager.isKeyReleased(InputManager.VK_KP_DOWN);
//Check for when keys are released
if (xAxesRel && yAxesRel) {
//Check the both axes are released
thePlayer.setChangex(0);
thePlayer.setChangey(0);
} else if (yAxesRel) {
//Check if the yaxes controls are released
thePlayer.setChangey(0);
} else if (xAxesRel) {
// check if only the xAxes controls are released
thePlayer.setChangex(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment