Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在像素颜色上绘制精灵_C#_Colors_Xna_Sprite_Pixel - Fatal编程技术网

C# 在像素颜色上绘制精灵

C# 在像素颜色上绘制精灵,c#,colors,xna,sprite,pixel,C#,Colors,Xna,Sprite,Pixel,我用一个简单的平铺引擎做了一个小游戏,它绘制一个对应于.txt文件中数字的精灵。现在我想前进,我想让游戏读一个.png或者别的什么,每一个像素都画一个精灵。在图像中使用不同的颜色时,会绘制不同的精灵。有人能帮我吗?另外,我在C#xNA4.0中也这样做。首先,使用pipleine将图像加载到纹理2D中。然后在代码中使用texture2d.GetData获得像素的颜色 MSDN texture2d.GetData示例使用texture2d.GetData如果您使用它来制作平铺贴图(我假设贴图将是一个

我用一个简单的平铺引擎做了一个小游戏,它绘制一个对应于.txt文件中数字的精灵。现在我想前进,我想让游戏读一个.png或者别的什么,每一个像素都画一个精灵。在图像中使用不同的颜色时,会绘制不同的精灵。有人能帮我吗?另外,我在C#xNA4.0中也这样做。

首先,使用pipleine将图像加载到纹理2D中。然后在代码中使用texture2d.GetData获得像素的颜色


MSDN texture2d.GetData示例

使用
texture2d.GetData
如果您使用它来制作平铺贴图(我假设贴图将是一个二维平铺网格),可能会很棘手,因为texture2d。返回1D数组

首先,您将需要地图的数组,但是存储它时,它可能如下所示:

Color[,] tiles = new Color[LEVEL_WIDTH,LEVEL_HEIGHT];
我使用以下技术从文件中加载预制结构

//Load the texture from the content pipeline
Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory");

//Convert the 1D array, to a 2D array for accessing data easily (Much easier to do Colors[x,y] than Colors[i],because it specifies an easy to read pixel)
Color[,] Colors = TextureTo2DArray(texture);

你的问题不清楚。如何在.txt文件中绘制精灵?此外,不可能在像素上绘制精灵,因为像素可以表示为1x1精灵。我的意思是,将纹理加载到游戏中,然后在纹理上的每个像素上,游戏绘制精灵。因此,纹理变得像一张地图,或者类似的东西:对不起,这是一个很好的答案,但我对C#XNA有点陌生,我不知道我们应该把它放在我的代码中,以及我应该如何使用它。你愿意做个绅士,简化一下吗?如果需要,您可以看到其他方法,如Draw(GameTime GameTime)、Update()等。在相同的区域中添加TextureTo2DArray(Texture2D texture)方法。你在哪里加载地图?我现在觉得自己太笨了,我还是不知道该怎么做才能让它在像素上从我的精灵表中画出精灵。一个雪碧是32x32。如果我制作一张地图,我会有一个例如64x64.png的图片,它在每个像素上画一个精灵,所以如果你在图片的顶部画一行像素。所以在游戏中,我设置了64行精灵。So 64(32x32)。抱歉这么愚蠢。
for(intx=0;x
将循环遍历每个瓷砖,并在x,y*32处绘制。为什么是32倍?因为我们需要为每个平铺偏移32(精灵大小)。我的意思是平铺上的像素应该是32x32。
    Color[,] TextureTo2DArray(Texture2D texture)
    {
        Color[] colors1D = new Color[texture.Width * texture.Height]; //The hard to read,1D array
        texture.GetData(colors1D); //Get the colors and add them to the array

        Color[,] colors2D = new Color[texture.Width, texture.Height]; //The new, easy to read 2D array
        for (int x = 0; x < texture.Width; x++) //Convert!
            for (int y = 0; y < texture.Height; y++)
                colors2D[x, y] = colors1D[x + y * texture.Width];

        return colors2D; //Done!
    }
 using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace YourNameSpace
{
    class Game
    {
        //"Global" array for the map, this holds each tile
        Color[,] tiles = new Color[LEVEL_WIDTH, LEVEL_HEIGHT];
        protected override void Initialize() //OR wherever you load the map and stuff
        {
            //Load the texture from the content pipeline
            Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory");

            //Convert the 1D array, to a 2D array for accessing data easily (Much easier to do Colors[x,y] than Colors[i],because it specifies an easy to read pixel)
            Color[,] Colors = TextureTo2DArray(texture);
        }
        Color[,] TextureTo2DArray(Texture2D texture)
        { //ADD THE REST, I REMOVED TO SAVE SPACE
        }
    }
}