2

I have pasted the main state underneath. Everytime i move my keyboard the object moves but it is refreshing the page. How do i make it so that the object does not refresh and it looks like the image trails and it eventually draws.

package javagame;

import org.lwjgl.input.*;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState{
    public static String mouse = "MOUSE IS NOT ON THE SCREEN";
    public static String keyBoard = "O";
    public static int keyBoardX = 50;
    public static int keyBoardY = 50;
    public static int xPos;
    public static int yPos;

public Menu (int state) {

}

// This method is just to initiate objects
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame)       
throws SlickException {
}

public void render(GameContainer gameContainer, StateBasedGame     
stateBasedGame, Graphics g) throws SlickException {
    g.drawString(mouse, xPos, 500 - yPos);
    g.drawString(keyBoard, keyBoardX, keyBoardY);
}

public void update(GameContainer gameContainer, StateBasedGame 
stateBasedGame, int delta) throws SlickException {
    // keyboard
    Input input = gameContainer.getInput();

    if (input.isKeyDown(input.KEY_UP)) {
        keyBoardY -= 1;
    } else if (input.isKeyDown(input.KEY_DOWN)) {
        keyBoardY += 1;
    } else if (input.isKeyDown(input.KEY_LEFT)) {
        keyBoardX -= 1;
    } else if (input.isKeyDown(input.KEY_RIGHT)) {
        keyBoardX += 1;
    }
}

public int getID() {
    return 0;
}
}
Tom
  • 16,842
  • 17
  • 45
  • 54

1 Answers1

0

To make it look "like the image trails and eventually draws", you have to actually keep redrawing the image in a previous position until you want it to disappear - otherwise your render method will only redraw the current state and there will be no 'trailing' shadow.

One option would be to keep a limited history of x-y co-ordinates, and to redraw everything in the history at every render() call. To create the effect of fading/trailing, reduce the alpha. You can do this by setting the font on the Graphics object before rendering, using a font with a Color with a lower alpha value.

deyur
  • 557
  • 2
  • 14