0

我很乐意承认这段代码很奇怪,而且是一种非常非正统的创建任何 2.5D 的方式,但是如果有人能看到发生了什么,我将不胜感激。

我的代码结果如图所示: 奇怪的曲面

我当然想要一个完全平坦的平面,但是我不确定它为什么会这样弯曲。如果有人有任何答案,代码如下所示:

公共静态int宽度= 640;公共静态int高度= 480;

private Image image;
private Graphics imageG;

private int mapWidth = 20, mapHeight = 15;

private int[] map;

private float x, y;

public Game(String title)
{
    super(title);
}

@Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
    imageG.setBackground(Color.black);
    imageG.clear();

    for (int y = 0; y < mapHeight; y++)
        for (int x = 0; x < mapWidth; x++)
        {
            switch (map[x + y * (mapWidth)])
            {
            case 0:
                imageG.setColor(Color.green);
                break;
            case 1:
                imageG.setColor(Color.yellow);
                break;
            }

            imageG.fillRect(x * 32, y * 32, 32, 32);
        }

    imageG.flush();

    Image frustrum;

    for (int i = 0; i < 240; i++)
    {
        frustrum = image.getSubImage((int) (x - i), (int) (y - i), 32 + 2 * i, 1);
        frustrum.draw(0, height - 1 - i, width, 1);
        //g.drawImage(frustrum, x - i, y - i);
    }

    //g.setColor(Color.blue);
    //g.fillRect(x, y, 32, 32);
}

@Override
public void init(GameContainer gc) throws SlickException
{
    try
    {
        image = new Image(width, height);
        imageG = image.getGraphics();
    } catch (SlickException e)
    {
        e.printStackTrace();
    }

    /*map = new int[]
        {
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
            1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
        };*/

    map = new int[mapWidth * mapHeight];
    Random random = new Random();
    for (int i = 0; i < map.length; i++)
    {
        map[i] = random.nextInt(2);
    }

    x = 10 * 32;
    y = 7 * 32;

}

@Override
public void update(GameContainer gc, int delta) throws SlickException
{
    if (gc.getInput().isKeyDown(Input.KEY_W)) y--;
    if (gc.getInput().isKeyDown(Input.KEY_S)) y++;
    if (gc.getInput().isKeyDown(Input.KEY_A)) x--;
    if (gc.getInput().isKeyDown(Input.KEY_D)) x++;
}

public static void main(String[] args)
{
    try
    {
        AppGameContainer appgc = new AppGameContainer(new Game("2.5D Game"));
        appgc.setDisplayMode(width, height, false);
        appgc.start();
    } catch (SlickException e)
    {
        e.printStackTrace();
    }
}

这是我的第一篇文章,如果事情看起来有点混乱,很抱歉。我相信我最终会习惯的...

4

0 回答 0