从字符串中正确加载? [英] Loading properly out of a string ?

查看:79
本文介绍了从字符串中正确加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code Project,



我过去三天一直遇到问题。我一直试图解决它自己没有运气。基本上,我只创造一个平坦的草地图,除非(Y cooardinate)低于视图大小的一半(ChunkSize)。



这是代码:



// - 生成景观。

Hello Code Project,

I have been facing a problem for the last three days. I have been trying to fix it myself no luck. Basically, I am creating a flat grass map by only generating air unless the (Y cooardinate) is below half of the size of the view(ChunkSize).

Here is the code:

// -- Generating the Landscape.

// -- Create the Chunk.
/// <summary>
/// Creates the Chunk at the Cooardinate and returns the string..
/// </summary>
/// <param name="?"></param>
private string CreateChunk(int ChunkX, int ChunkY)
{

    // -- Return String.
    string _ReturnString = "";

    // -- Create a stream writer.
    StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");

    // -- Initialize.
    for (int i = 0; i < World.ChunkSize; i++)
        for (int j = 0; j < World.ChunkSize; j++)
            _ReturnString += (char)BlockID.BLOCK_GRASS ;

    // -- Write the world bytes.
    for (int i = 0; i < ChunkSize; i++)
    {
        for (int j = 0; j < ChunkSize; j++)
        {
            // -- Block.
            char Block;
            if (ChunkY > 0 || (ChunkY == 0 && j > ChunkSize / 2))
            {
                Block = (char)BlockID.BLOCK_GRASS;
            }
            else
                Block = (char)BlockID.BLOCK_AIR;

            // -- Add Return.
            _ReturnString = ReplaceAtIndex(i * j, Block, _ReturnString);


        }
    }

    // -- Write.
    _Writer.Write(_ReturnString);

    // -- Close the Writer.
    _Writer.Flush();
    _Writer.Close();

    // -- Return.
    return _ReturnString;
}





// - 获取一个块并加入字符串。





// -- Get a block and also join a string.

// -- Change a character.
/// <summary>
/// Changes a specific character in a string.
/// </summary>
/// <param name="i"></param>
/// <param name="value"></param>
/// <param name="word"></param>
/// <returns></returns>
static string ReplaceAtIndex(int i, char value, string word)
{
    // -- Create an array of characters out of the string.
    char[] _Characters = word.ToCharArray();

    // -- Change the character
    _Characters[i] = value;

    // -- Return.
    return string.Join("", _Characters);
}


// -- Get a block.
/// <summary>
/// Returns the block given from the arguments. Note: Also returns air if out of bounds.
/// </summary>
/// <param name="BlockX">X for the block.</param>
/// <param name="BlockY">Y for the block.</param>
/// <param name="Chunk">The chunk to check in.</param>
/// <returns></returns>
public static char GetBlock(int BlockX, int BlockY, string Chunk)
{
    // -- Block.
    char _Block = (char)BlockID.BLOCK_AIR;


    // -- Get the block.
    _Block = Chunk[BlockX * BlockY];

    // -- Return.
    return _Block;
}







// - 最重要的是绘图。




// -- And most importantly the drawing.

// -- Draw World.
        /// <summary>
        /// Draws the World on the Graphics given as parameter.
        /// </summary>
        /// <param name="g"></param>
        public void DrawWorld()
        {
            // -- Loop through the chunkX.
            for (int i = 0; i < World.ChunkSize; i++)
            {
                // -- Loop through the chunkY.
                for (int j = 0; j < World.ChunkSize; j++)
                {
                    // -- Get the Block.
                    char Block = (char)World.GetBlock(i, j, ThePlayer.LoadedChunks[5]);

                    // -- Color.
                    Color toDraw = Color.Transparent;

                    // -- Get the Right Color.
                    switch (Block)
                    {
                        case (char)BlockID.BLOCK_AIR:
                            break;

                        case (char)BlockID.BLOCK_DIRT:
                            toDraw = Color.Brown;
                            break;
                        case (char)BlockID.BLOCK_GRASS:
                            toDraw = Color.Green;
                            break;
                    }
                    

                    // -- Draw the Block.
                    int Size = BlockID.BlockSize;
                    int X = i == 0 ? 0 : i * Size;
                    int Y = j == 0 ? 0 : j * Size;
                    Rectangle _Rec = new Rectangle(X, Y, BlockID.BlockSize, BlockID.BlockSize);
                    spriteBatch.Draw(ImageBlock, _Rec, toDraw);

                }
            }
        }





如果你想知道结果是什么是:

http://i46.tinypic.com/de0daq.jpg [ ^ ]



编辑:对于不了解问题的人。问题是所有绿色块(ID.GrasS)应该在房间的高度/ 2下,现在它们到处都是,但是我不明白我怎么读错了?。



请帮忙! :(



And if you are wondering what the result is:
http://i46.tinypic.com/de0daq.jpg[^]

For people who do not understand the problem. The problem is that all the green blocks(ID.GrasS) should be under the Height / 2 of the room and now they are all over the place, but I do not understand how I am reading this out wrong ?.

Please help ! :(

推荐答案

我认为你的代码可以简化一点。我猜想我是x轴而J是y轴



I think your code could be simplified a little. I''m guessting that I was the x-axis and J was the y-axis

for (int i = 0; i < World.ChunkSize * World.ChunkSize; i++)
     _ReturnString += (char)BlockID.BLOCK_AIR;
 
// -- Write the world bytes.
for (int y = 0; y < ChunkSize; y++)
{
     for (int x = 0; x < ChunkSize; x++)
     {
          // -- Block.
         char Block = (char)BlockID.BLOCK_AIR;
         if (y < ChunkSize / 2)
              Block = (char)BlockID.BLOCK_GRASS;
         // -- Add Return.
         _ReturnString = ReplaceAtIndex(x * y, Block, _ReturnString);
     }
}





试试吧。



Try that.


这篇关于从字符串中正确加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆