我是 C# 和 XNA 的初学者,目前正在尝试制作棋盘游戏 LUDO。我以前做过一些 java 编程,在面向对象编程中很常见。所以我现在卡在的事情是绘制“板”的精灵。
我在 MS Paint 中制作了我自己使用的所有精灵,所有不同的精灵(26 个不同的精灵)都具有相同的大小 45px X 45px。我在想的是制作一个包含数字的 2Darray,这些数字将引用纹理数组中的特定精灵。示例:这是我正在设置的板类型的链接:http: //upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Klassisk_ludo-spill.JPG/220px-Klassisk_ludo-spill.JPG
我的 2Darray 设置如下:它从板的顶部开始,然后从左到右。所以这就是我制作 2Darray 的方式:
//Create the full grid of the board, where the numbers refer to the sprites that is supposed to be
// placed here. the array starts from the top of the bord from left to right and then goes downwards.
int[,] myArray = new int[,] {{3,3,3,3,3,3,4,4,4,0,0,0,0,0,0},
{3,24,25,23,25,3,4,0,0,0,12,13,12,13,0},
{3,22,23,22,23,3,4,0,4,0,10,11,10,11,0},
{3,24,25,23,25,3,4,0,4,0,12,13,12,13,0},
{3,22,23,22,23,3,4,0,4,0,10,11,10,11,0},
{3,3,3,3,3,3,4,0,4,0,0,0,0,0,0},
{4,3,4,4,4,4,6,0,5,4,4,4,4,4,4},
{4,3,3,3,3,3,3,9,1,1,1,1,1,1,4},
{4,4,4,4,4,4,7,2,8,4,4,4,4,1,4},
{2,2,2,2,2,2,4,2,4,1,1,1,1,1,1},
{2,20,21,20,21,2,4,2,4,1,16,17,16,17,1},
{2,18,19,18,19,2,4,2,4,1,14,15,14,15,1},
{2,20,21,20,21,2,4,2,4,1,16,17,16,17,1},
{2,18,19,18,19,2,2,2,4,1,14,15,14,15,1},
{2,2,2,2,2,2,4,4,4,1,1,1,1,1,1}};
就像我说的,这些数字应该对应于另一个纹理数组中的特定精灵。以下是什么数字对应什么精灵的概述:
textureArray = newTexture2D[26];
0 = blueblock;
1 = redblock;
2 = yellowBlock;
3 = greenBlock;
4 = whiteBlock;
5 = blueredBlock;
6 = greenblueBlock;
7 = greenyellowBlock;
8 = yellowredBlock;
9 = xcenterBlock;
10 = BlueBottomLeftBlock;
11 = BlueBottomRightBlock;
12 = BlueTopLeftBlock;
13 = BlueTopRightBlock;
14 = RedBottomLeftBlock;
15 = RedBottomRightBlock;
16 = RedTopLeftBlock;
17 = RedTopRightBlock;
18 = YellowBottomLeftBlock;
19 = YellowBottomRightBlock;
20 = YellowTopLeftBlock;
21 = YellowTopRightBlock;
22 = GreenBottomLeftBlock;
23 = GreenBottomRightBlock;
24 = GreenTopLeftBlock;
25 = GreenTopRightBlock;
目前我已经在我的后台类中创建了一个内容管理器,如下所示:
// ContentManager that is loadedes textures and position for the objects in to game1
public void LoadContent(ContentManager content)
{
this._BlueBlock = content.Load<Texture2D>("blue-block");
this._RedBlock = content.Load<Texture2D>("red-block");
this._YellowBlock = content.Load<Texture2D>("yellow-block");
this._GreenBlock = content.Load<Texture2D>("green-block");
this._WhiteBlock = content.Load<Texture2D>("white-block");
this._BlueRedBlock = content.Load<Texture2D>("blue-red-block");
this._GreenBlueBlock = content.Load<Texture2D>("green-blue-block");
this._GreenYellowBlock = content.Load<Texture2D>("green-yellow-block");
this._YellowRedBlock = content.Load<Texture2D>("yellow-red-block");
this._XCenterBlock = content.Load<Texture2D>("x-center-block");
this._BlueBottomLeftBlock = content.Load<Texture2D>("blue-bottomleft-block");
this._BlueBottomRightBlock = content.Load<Texture2D>("blue-bottomright-block");
this._BlueTopLeftBlock = content.Load<Texture2D>("blue-topleft-block");
this._BlueTopRightBlock = content.Load<Texture2D>("blue-topright-block");
this._RedBottomLeftBlock = content.Load<Texture2D>("red-bottomleft-block");
this._RedBottomRightBlock = content.Load<Texture2D>("red-bottomright-block");
this._RedTopLeftBlock = content.Load<Texture2D>("red-topleft-block");
this._RedTopRightBlock = content.Load<Texture2D>("red-topright-block");
this._YellowBottomLeftBlock = content.Load<Texture2D>("yellow-bottomleft-block");
this._YellowBottomRightBlock = content.Load<Texture2D>("yellow-bottomright-block");
this._YellowTopLeftBlock = content.Load<Texture2D>("yellow-topleft-block");
this._YellowTopRightBlock = content.Load<Texture2D>("yellow-topright-block");
this._GreenBottomLeftBlock = content.Load<Texture2D>("green-bottomleft-block");
this._GreenBottomRightBlock = content.Load<Texture2D>("green-bottomright-block");
this._GreenTopLeftBlock = content.Load<Texture2D>("green-topleft-block");
this._GreenTopRightBlock = content.Load<Texture2D>("green-topright-block");
}
这就是我偶然发现我的主要问题的地方。我应该像这样加载内容吗?如果是这样,我应该如何引用 textureArray 中的纹理,以便数字与它们应该在我的 2DArray 中具有的纹理相对应?
我正在考虑制作一个嵌套的 for 循环,将所有精灵放在正确的位置,因为板是 15 X 15 精灵(总共 225 个)。我还应该在我的“public void Draw(SpriteBatch spriteBatch)”方法中使用 foreach 语句吗?
我真的希望有人能给我一些指示,他们将不胜感激!
此致!