gpt4 book ai didi

java - 网格 libgdx 内的屏幕坐标

转载 作者:行者123 更新时间:2023-12-02 04:43:19 25 4
gpt4 key购买 nike

我使用 ShapeRenderer.line 创建了一个网格。它绘制一个 10x10 网格。

private void setupCamera() {
// TODO Auto-generated method stub
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);
camera.update();
}

@Override
public void draw() {
for(int x = 0; x < rows; x++){
for(int y = 0; y < columns; y++){

shapeRenderer.line(x+1, 12-y, x+2, 12-y);
shapeRenderer.line(x+1, 12-y, x+1, 11-y);
shapeRenderer.line(x+2, 12-y, x+2, 11-y);
shapeRenderer.line(x+1, 11-y, x+2, 11-y);
}
}
}

public class Cell
{
private static int gridWidth = 1;
private static int gridHeight = 1;

public int coordinatesX;
public int coordinatesY;

public Cell(){}

public Cell(int x, int y){

this.coordinatesX = x;
this.coordinatesY = y;
}

public void drawLine(ShapeRenderer shapeRenderer){

int left = coordinatesX * gridWidth;
int right = left + gridWidth;
int bottom = coordinatesY * gridHeight;
int top= bottom + gridHeight;

shapeRenderer.line(left , top, left, bottom);
shapeRenderer.line(right , top, right , bottom);
shapeRenderer.line(left , top, right , top);
shapeRenderer.line(left , bottom, right , bottom);
}

public void drawImageStartPosition(SpriteBatch spritebatch, int x, int y, Texture texture){

int left = x * texture.getWidth();
int bottom = y * texture.getHeight();

spritebatch.draw(texture, left, bottom, texture.getWidth(), texture.getHeight());
}

}

我已经创建了一个纹理(ball.png),我想要完成的是我想根据我创建的网格将该纹理放置在单元格内(使用该特定单元格的屏幕坐标)。

任何人都可以指导我如何解决这个问题吗?

最佳答案

尝试将每个网格单元变成一个对象。

public class GridSquare {

private static int gridWidth = 10;
private static int gridHeight = 10;

public final int xPos;
public final int yPos;

public GridSquare(int x, int y){
xPos = x;
yPos = y;
}

public void drawOutLine(ShapeRenderer shapeRenderer){

/*
If the gridWidth and gridHeight don't change over time then you can
move the calculation of the left, right, bottom and top positions
into the constructor for better performance.
*/
int left = xPos * gridWidth;
int right = left + gridWidth;
int bottom = yPos * gridHeight;
int top= bottom + gridHeight;

shapeRenderer.line(left , top, left, bottom);
shapeRenderer.line(right , top, right , bottom);
shapeRenderer.line(left , top, right , top);
shapeRenderer.line(left , bottom, right , bottom);
}

public void drawImage(Spritebatch spritebatch, Texture texture){

/*
If the gridWidth and gridHeight don't change over time then you can
move the calculation of the left and bottom positions
into the constructor for better performance.
*/

int left = xPos * gridWidth;
int bottom = yPos * gridHeight;

spritebatch.draw(
texture,
left,
bottom,
gridWidth,
gridHeight
);
}
}

然后你可以迭代它们并告诉它们渲染自己,他们就会知道如何去做。

 shapeRenderer.begin();
for(int x = 0; x < rows; x++){
for(int y = 0; y < columns; y++){
gridSquares[x][y].drawOutLine(shapeRenderer);
}
}
shapeRenderer.end();


spritebatch.begin();
for(int x = 0; x < rows; x++){
for(int y = 0; y < columns; y++){
// Assuming you have loaded the png image into a variable named balltexture
gridSquares[x][y].drawImage(spritebatch, balltexture);
}
}
spritebatch.end();

关于java - 网格 libgdx 内的屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29929229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com