我的主要问题是我不明白如何获取生成的图块的位置或如何判断鼠标的位置。我应该使用碰撞来检测鼠标还是其他东西?我可以做些什么来优化我的代码并使其更容易获得诸如位置之类的东西
我从我的代码中取出了一些东西,比如加载纹理只是为了让你们更短,因为这不是问题的一部分。
我的瓷砖生成代码
public Block[] tiles = new Block[3];
public int width, height;
public int[,] index;
public Rectangle tileRect;
public void Load(ContentManager content)
{
tiles[0] = new Block { Type = BlockType.Grass, Position = Vector2.Zero, texture = grass};
tiles[1] = new Block { Type = BlockType.Dirt, Position = Vector2.Zero, texture = dirt};
width = 50;
height = 50;
index = new int[width, height];
Random rand = new Random();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
index[x,y] = rand.Next(0,2);
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64),
Color.White);
}
}
}
块属性代码
public enum BlockType
{
Dirt,
Grass,
Selection
}
public class Block
{
public BlockType Type { get; set; }
public Vector2 Position { get; set; }
public Texture2D texture { get; set; }
}