import org.newdawn.slick.Animation; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.SpriteSheet; public class Player extends Entity { public Input input; public String playerPath = "/res/player.png"; public SpriteSheet sheet; public Animation upAnim; public Animation downAnim; public Animation rightAnim; public Animation leftAnim; public boolean up, down, right, left; public Player(float x, float y, int width, int height, float moveSpeed) { super(x, y, width, height, moveSpeed); try { sheet = new SpriteSheet(playerPath, width, height); upAnim = new Animation(sheet, 0, 3, 2, 3, false, 250, false); downAnim = new Animation(sheet, 0, 0, 2, 0, false, 250, false); rightAnim = new Animation(sheet, 0, 2, 2, 2, false, 250, false); leftAnim = new Animation(sheet, 0, 1, 2, 1, false, 250, false); } catch (SlickException e) { new RuntimeException("Could not load spritesheet..", e); } } public void update(GameContainer gc, int delta) throws SlickException { input = gc.getInput(); boolean upPressed = input.isKeyDown(Input.KEY_W); boolean downPressed = input.isKeyDown(Input.KEY_S); boolean rightPressed = input.isKeyDown(Input.KEY_D); boolean leftPressed = input.isKeyDown(Input.KEY_A); if(upPressed) { y -= delta * moveSpeed; up = true; upAnim.setAutoUpdate(true); } else { up = false; upAnim.setAutoUpdate(false); } if(downPressed) { y += delta * moveSpeed; down = true; downAnim.setAutoUpdate(true); } else { down = false; downAnim.setAutoUpdate(false); } if(rightPressed) { x += delta * moveSpeed; right = true; rightAnim.setAutoUpdate(true); } else { right = false; rightAnim.setAutoUpdate(false); } if(leftPressed) { x -= delta * moveSpeed; left = true; leftAnim.setAutoUpdate(true); } else { left = false; leftAnim.setAutoUpdate(false); } } public void render(GameContainer gc, Graphics g) throws SlickException { if(up) { upAnim.draw(x, y); } else if(down) { downAnim.draw(x, y); } else if(right) { rightAnim.draw(x, y); } else if(left) { leftAnim.draw(x, y); } else { downAnim.draw(x, y); } } }