package main; import org.newdawn.slick.*; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; /** * User: Zsolt * Date: 2013.04.11. * Time: 19:14 */ public class GameState extends BasicGameState { float x = 100, y = 100; // player's location (starts from 100 by 100) float targetX, targetY; // The targets for the player float mouseX, mouseY; // The mouse locations Image player; // The IMG file (player) @Override public int getID() { return 0; // This is used to jump between states ( stateBasedGame.enterState(GameState.getID()); ) } @Override public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { player = new Image("res/bg.png"); // Init the image } @Override public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { player.draw(x - 50, y - 50, 100, 100); // Draw a 100 by 100 sized player } @Override public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { Input input = gc.getInput(); // Store all inputs in this input object mouseX = input.getMouseX(); // Get mouseX from the input mouseY = input.getMouseY(); // Get mouseY from the input // 0 = left; 1 = right mouse button if (input.isMouseButtonDown(0)) { targetX = mouseX; // Set the target x to the mouseX targetY = mouseY; // Set the target Y to the mouseY } // Calculate the disrance between the current and the target locations float length = (float) Math.sqrt((targetX - x) * (targetX - x) + (targetY - y) * (targetY - y)); float velocityX = (targetX - x) / length * delta * 1f; float velocityY = (targetY - y) / length * delta * 1f; x += velocityX; // Add distance_x to the current location y += velocityY; // Add distance_y to the current location } }