简体   繁体   English

二维随机生成的平铺游戏Libgdx Java

[英]2d randomly generated tiled game Libgdx Java

So i am trying to create a game that is top down 2d and using a pixel tiled map style format. 因此,我试图创建一个自上而下2d并使用像素平铺地图样式格式的游戏。 They way i would like to do it is have say 4 different tiles, different shades of green and brown for grass. 他们要用的方式是说4种不同的瓷砖,草的绿色和棕色的阴影不同。 I would like to generate these 4 tiles randomly around the 1920*1080 pixel area to give a semi realistic effect. 我想在1920 * 1080像素区域周围随机生成这4个图块,以产生半逼真的效果。 So something like this: 所以像这样: 在此处输入图片说明

But with randomly generated different tiles in there. 但是在那里随机生成了不同的图块。 What would be the best way of doing this? 最好的方法是什么?

Just use a for loop and place a random tile at each location 只需使用for循环并在每个位置放置一个随机图块

for(int i = 0; i < numTilesY; i++)
{
    for(int j = 0; j < numTilesX; j++)
    {
        placeRandomTile(i, j);
    }
}

placeRandomTile would just use java's Random class to choose a random number, and depending on the number will place the corresponding tile. placeRandomTile只会使用Java的Random类来选择一个随机数,然后根据该数字放置相应的图块。 For example, if randomNumber == 2, place grass tile. 例如,如果randomNumber == 2,则放置草砖。

If you want the tiles to form a realistic looking world similiar to Minecraft, you should use some sort of a noise function like Perlin Noise . 如果您希望瓷砖形成类似于Minecraft的逼真的世界,则应使用诸如Perlin Noise的某种噪声函数。 It will be random in that every time you generate a new world, the world will be different. 这将是随机的,因为每当您产生一个新世界时,世界都会有所不同。

EDIT: 编辑:

int[][] map= new int[32][32]; //create 32x32 tile map

public void placeRandomTile(int xIndex, int yIndex)
{
    Random rand = new Random();
    int value = rand.nextInt(5);//get value between 0 and 5
    int tile = 0;

    if(value == 0)
    {
        tile = 1; //1 corresponds to GRASS tile
    }

    if(value == 1)
    {
        tile = 2; //WATER tile
    }

    this.map[xIndex][yIndex] = tile;
}

Now that you have your map data stored in a 2D array, you just need to loop through the data and draw each tile accordingly at the proper locations. 现在,您已将地图数据存储在2D数组中,您只需要遍历数据并在适当的位置相应地绘制每个图块。

In your drawing method, you would just draw each tile from the map like so: 在绘制方法中,您只需像这样从地图上绘制每个图块:

public void draw(float x, float y, Graphics g)
{
    for(int i = 0; i < map.length; i++)
    {
        for(int j = 0; j < map[i].length; j++)
        {
            int tileType = map[i][j]; //get tile type
            if(tileType == Tile.grass.id)
            {
                Tile.grass.draw(x + (j * Tile.WIDTH), y + (i * Tile.HEIGHT));//draw grass
            }

        }
    }
}

Since you are using libgdx, you will need to look into these classes: TiledMap, TiledMapTileLayer, Screen, Texture, and OrthogonalTiledMapRenderer. 由于您使用的是libgdx,因此需要研究以下类:TiledMap,TiledMapTileLayer,Screen,Texture和OrthogonalTiledMapRenderer。 You basically create a TiledMap and draw Textures using a TiledMapRenderer. 您基本上可以创建TiledMap并使用TiledMapRenderer绘制纹理。

You can view a sample game I have on Github from a while ago that does what you want. 您可以查看我在Github上拥有的示例游戏,该游戏可以满足您的需求。 It's here . 在这里 Look at Dungeon class and screens/GameScreen class 查看地下城类和屏幕/ GameScreen类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM