簡體   English   中英

如何在 libgdx 中顯示 map?

[英]How would I display a map in libgdx?

所以我正在用 libgdx 創建一個游戲,我將 pong 和 breakout 結合起來。 我在試圖弄清楚如何從游戲突破中顯示“磚塊”時遇到了麻煩。 我目前正在為積木使用 map。

class 顯示所有內容:

public class GameScreen extends ScreenAdapter {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private World world;
    private Box2DDebugRenderer box2DDebugRenderer;

    //Game Objects
    private Player player, player2;
    private AI playerAI, playerAI2;
    private Ball ball;
    private Arena arenaTop, arenaBottom;
    private Brick brick, brick2;

    private GameContactListener gameContactListener;
    private TextureRegion[] numbers;

    public GameScreen(OrthographicCamera camera) {
        this.camera = camera;
        this.camera.position.set(new Vector3(Main.INSTANCE.getScreenWidth() / 2, Main.INSTANCE.getScreenHeight() / 2, 0));
        this.batch = new SpriteBatch();
        this.world = new World(new Vector2(0, 0), false);
        this.box2DDebugRenderer = new Box2DDebugRenderer();
        this.gameContactListener = new GameContactListener(this);
        this.world.setContactListener(this.gameContactListener);

        this.player = new Player(316, Main.INSTANCE.getScreenHeight() / 2, this);
        this.player2 = new Player(1260, Main.INSTANCE.getScreenHeight() / 2, this);
        this.playerAI = new AI(1260, Main.INSTANCE.getScreenHeight() / 2, this);
        this.playerAI2 = new AI(316, Main.INSTANCE.getScreenHeight() / 2, this);
        this.ball = new Ball(this);
        this.arenaTop = new Arena(32, this);
        this.arenaBottom = new Arena(Main.INSTANCE.getScreenHeight() - 32, this);
        this.brick = new Brick(5, 96, 8, 5, this);
        this.brick2 = new Brick(5, 166, 8, 5, this);
        this.numbers = loadTextureSprite("core/src/com/pongbreaker/recources/numbers.png", 10);
    }



    public void update() {
        world.step(1 / 60f, 6, 2);

        this.camera.update();
        this.player.update();
        this.player2.update();
        this.playerAI.update();
        this.playerAI2.update();
        this.ball.update();
        this.brick.update();
        this.brick2.update();

        batch.setProjectionMatrix(camera.combined);

        if(Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            Gdx.app.exit();
        }

        if(Gdx.input.isKeyPressed(Input.Keys.R)) {
            this.ball.reset();
            this.player.setScore(0);
            this.player2.setScore(0);
            this.playerAI.setScore(0);
        }
    }

    @Override
    public void render (float delta) {
        update();

        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        //this.player.render(batch);
        //this.player2.render(batch);
        this.playerAI.render(batch);
        this.playerAI2.render(batch);
        this.ball.render(batch);
        this.arenaTop.render(batch);
        this.arenaBottom.render(batch);
        this.brick.render(batch);
        this.brick2.render(batch);

        drawNumbers(batch,player.getScore(), 64, Main.INSTANCE.getScreenHeight() - 55, 30, 42);
        drawNumbers(batch, player2.getScore(), Main.INSTANCE.getScreenWidth() - 96, Main.INSTANCE.getScreenHeight() -55, 30, 42);

        batch.end();

        //this.box2DDebugRenderer.render(world, camera.combined.scl(Constants.PPM));
    }

    private void drawNumbers(SpriteBatch batch, int number, float x, float y, float width, float height) {
        if(number < 10) {
            batch.draw(numbers[number], x, y, width, height);
        }else{
            batch.draw(numbers[Integer.parseInt(("" + number).substring(0,1))], x, y, width, height);
            batch.draw(numbers[Integer.parseInt(("" + number).substring(1,2))], x + 20, y, width, height);
        }
    }
    private TextureRegion[] loadTextureSprite(String filename, int columns) {
        Texture texture = new Texture(filename);
        return TextureRegion.split(texture, texture.getWidth() / columns, texture.getHeight())[0];
    }

    public World getWorld() {
        return world;
    }

    public Ball getBall() {
        return ball;
    }

    public Player getPlayer() {
        return player;
    }

    public Player getPlayer2() {
        return player2;
    }

    public Brick getBrick() {
        return brick;
    }

    @Override
    public void dispose () {

    }
}

和磚class:

public class Brick {
    private Body body;
    private float x, y;
    private int width, height, score, row, column;
    private Texture texture;
    private GameScreen gameScreen;
    private int map [][];

    public Brick(float x, float y, int row, int column, GameScreen gameScreen) {
        this.x = x;
        this.y = y;
        this.gameScreen = gameScreen;
        this.map = new int[row][column];
        this.width = 300 / column;
        this.height = 512 / row;

        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                map[i][j] = 1;
            }
        }

        this.texture = new Texture("core/src/com/pongbreaker/recources/redTexture.png");
        this.body = BodyHelper.createBody(x, y, width, height, true, 10000, gameScreen.getWorld(), ContactType.BRICKS);
    }

    public void update() {
        x = body.getPosition().x * Constants.PPM - (width / 2);
        y = body.getPosition().y * Constants.PPM - (height / 2);

    }

    public void render(SpriteBatch batch) {
        batch.draw(texture, x, y, width, height);
    }

    public void setBrick(int value, int row, int column) {
        map[row][column] = value;
    }

    public void draw(SpriteBatch batch) {
        for(int i = 0; i < map.length; i++) {
            for(int j = 0; j < map.length; j++) {
                if(map[i][j] > 0) {
                    //batch.draw();
                }
            }
        }
    }

    public void score() {
        this.score++;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

現在我正在分別繪制如下所示的磚塊:!( https://imgur.com/a/Fs4D8pg

第一件事是第一件事。 它與您的問題無關,但是您需要在磚塊 class 中公開一個dispose()方法來處理紋理,並從 Screen class 的dispose()方法以及所有的 dispose( dispose()方法中調用它您的其他一次性用品(Textures、SpriteBatch、Box2DDebugRenderer 等)。 否則,您將泄漏 memory。

我認為您通過制作一個Brick class 使自己感到困惑,它同時代表單個 Brick 並代表電路板一側的所有磚塊。

將 Brick class 視為代表單個 Brick 的字段:

body, x, y, width, height

將 Brick class 視為代表所有磚塊的字段:

score, row, column, map

您應該將此 class 分成兩個類,BrickGroup 和 Brick。 map中的 map 應該是單個磚塊的二維數組,而不是int s。 您可以添加一個int status = 1; 字段到您的 Brick class 以代替之前二維數組中的內容。

然后您的 BrickGroup 渲染方法可以簡單地迭代map來調用每個 Brick 的渲染方法。 Brick 的渲染方法可以使用它的狀態、position 和大小來繪制自己。

我還建議將 BrickGroup 中的rowcolumn重命名為rowCountcolumnCount以明確它們代表的內容。

確保不要將紋理加載移動到 Brick class 中,否則您將加載許多重復調用。 您可以在 BrickGroup 中加載一次並為 Brick 類的渲染方法創建一個texture參數,因此 BrickGroup 在繪制自身時傳遞每個 Brick 的引用以借用。

最后,我認為先水平然后垂直(x,然后 y)進行 2D 數組查找更自然,因此在初始化 2D 數組大小時應該交換行和列。

例子:

public class Brick {
    private Body body;
    private float x, y;
    private int width, height, status;

    public Brick(GameScreen gameScreen, float x, float y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.body = BodyHelper.createBody(x, y, width, height, true, 10000, gameScreen.getWorld(), ContactType.BRICKS);
        this.status = 1;
    }

    public void update() {
        x = body.getPosition().x * Constants.PPM - (width / 2f);
        y = body.getPosition().y * Constants.PPM - (height / 2f);
    }

    public void render(SpriteBatch batch, Texture texture) {
        if (status > 0) {
            batch.draw(texture, x, y, width, height);
        }
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}
public class BrickGroup implements Disposable {
    private int score;
    private Texture texture;
    private GameScreen gameScreen;
    private Brick[][] map;

    public BrickGroup(float x, float y, int rowCount, int columnCount, GameScreen gameScreen) {
        this.gameScreen = gameScreen;
        this.map = new Brick[columnCount][rowCount];

        int brickWidth = 300 / column;
        int brickHeight = 512 / row;

        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                map[i][j] = new Brick(gameScreen, x + i * brickWidth, y + j * brickHeight, brickWidth, brickHeight);
            }
        }

        this.texture = new Texture("core/src/com/pongbreaker/recources/redTexture.png");
    }

    public void update() {
        for(Brick[] col : map) {
            for(Brick brick : col) {
                brick.update();
            }
        }
    }

    public void render(SpriteBatch batch) {
        for(Brick[] col : map) {
            for(Brick brick : col) {
                brick.render(batch, texture);
            }
        }
    }

    public void setBrick(int value, int row, int column) {
        map[row][column].setStatus(value);
    }

    public void score() {
        this.score++;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public void dispose() {
        texture.dispose();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM