Java Slick 2D绘图与键盘

时间:2015-03-17 19:25:36

标签: java slick2d

我已粘贴下面的主要状态。每次我移动我的键盘时,对象都会移动,但它会刷新页面。我如何使对象不刷新,它看起来像图像轨迹,它最终绘制。

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;
}
}

1 个答案:

答案 0 :(得分:0)

为了让它看起来“像图像轨迹并最终绘制”,你必须实际上将图像重新绘制在之前的位置,直到你希望它消失为止 - 否则你的渲染方法只会重绘当前状态并且会有没有'尾随'的影子。

一种选择是保持x-y坐标的有限历史,并在每次render()调用时重绘历史记录中的所有内容。要创建淡入淡出/尾随效果,请减少alpha。您可以通过在渲染之前在Graphics对象上设置字体来执行此操作,使用具有较低alpha值的Color的字体。