2013-03-28 3 views
0

저는 C#과 XNA의 초보자입니다. 현재 보드 게임 LUDO를 만들려고합니다. 나는 전에 자바 프로그래밍을 해왔고 객체 지향 프로그래밍과 공통점이있다. 그래서 제가 붙어있는 것은,이 순간에 "보드"의 스프라이트를 그리는 것입니다.2Darrays를 사용하여 XNA에서 보드 게임 Ludo 그리기

나는 MS 페인트에서 나 자신을 사용하고있는 모든 스프라이트를 만들었고 모든 다른 스프라이트 (26 가지 스프라이트)는 모두 동일한 크기 45px 45px를가집니다. 내가 생각한 것은 숫자가 포함 된 2D 배열을 만드는 것이 었습니다.이 숫자는 텍스처 배열의 특정 스프라이트를 참조합니다. 예 : 설정중인 보드 유형에 대한 링크가 있습니다. http://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Klassisk_ludo-spill.JPG/220px-Klassisk_ludo-spill.JPG

내 2D 배열은 다음과 같습니다. 그래서 내가 내 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}}; 

내가이 숫자가 다른 텍스처 배열의 spesific 스프라이트에 해당하는 가정된다 말했듯이. 여기에 어떤 수의 개요 무엇 스프라이트에 해당됩니다

// 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 = 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; 

내가 만든 다음과 같습니다 내 배경 클래스의 콘텐츠 관리자 내 주요 문제에. 이런 내용을로드해야합니까? 그렇다면 텍스쳐 배열의 텍스쳐를 참조하여 번호가 2DArray에있는 텍스쳐와 일치하도록하려면 어떻게해야합니까?

보드가 15 x 15 스프라이트 (총 225 개)이므로 올바른 위치에 모든 스프라이트를 배치하는 중첩 for 루프를 만들려고했습니다. 또한 "public void Draw (SpriteBatch spriteBatch)"metod에서 foreach 문을 사용해야합니까?

나는 누군가가 나에게 그들이 매우 감사하게 될 어떤 조언을 줄 수 있기를 정말로 바란다.

감사합니다! 내가 비트 맵없이 재미로 코딩 한

+0

이 스프라이트의 색상을 변경하기보다는 모든 26 개 가지 블록 유형을 가지고 쉬울 것 ? 또한 스프라이트를 회전하면 숫자가 줄어들 수도 있습니다. – TheKingDave

답변

0

는 ... 멋진 https://dl.dropbox.com/u/59983887/ludo.rar

protected override void Draw(GameTime gameTime) { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     var ss = 3 * BLOCK_TOTAL_SIZE + BLOCK_GAP; 
     var bounds = new Rectangle(S, S, ss, ss);       // Triangle background 
     spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque); 
     spriteBatch.Draw(white, bounds, BackColor); 
     spriteBatch.End(); 

     Matrix transform = Matrix.Identity; 

     for (int i = 0; i < 4; i++) { 
      // Player area by color 
      spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null, transform); 
      DrawPlayerAreaByColor(colors[i]); 
      spriteBatch.End(); 

      transform = Matrix.CreateRotationZ(-MathHelper.PiOver2) * Matrix.CreateTranslation(0, BLOCK_TOTAL_SIZE * 15 - BLOCK_GAP, 0) * transform; 

      // Triangle 
      for (int j = 0; j < vertex.Length; j++) vertex[j].Color = colors[i]; 
      basicEffect.World = Matrix.CreateRotationZ(-i * MathHelper.PiOver2) * Matrix.CreateTranslation(bounds.Center.X, bounds.Center.Y, 0); 
      basicEffect.CurrentTechnique.Passes[0].Apply(); 
      GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertex, 0, 1); 
     } 


     base.Draw(gameTime); 
    } 

    void DrawPlayerAreaByColor(Color color) { 
     Rectangle bounds = new Rectangle(0, 0, S, S); 
     spriteBatch.Draw(white, bounds, color);      // Big area 
     bounds.Inflate(-BLOCK_SIZE, -BLOCK_SIZE); 
     spriteBatch.Draw(white, bounds, BackColor);     // Background inside big area 
     bounds.Inflate(-BLOCK_GAP, -BLOCK_GAP); 
     spriteBatch.Draw(white, bounds, Color.White);     // White area inside big area 

     for (int x = 0; x < 2; x++) 
      for (int y = 0; y < 2; y++) { 
       var mw = bounds.Width - 2 * BLOCK_SIZE - 4 * BLOCK_GAP; 
       var mh = bounds.Height - 2 * BLOCK_SIZE - 4 * BLOCK_GAP; 
       mw /= 3; 
       mh /= 3; 
       var mb = new Rectangle(bounds.X + mw, bounds.Y + mh, BLOCK_TOTAL_SIZE, BLOCK_TOTAL_SIZE); 
       mb.X += x * (mw + BLOCK_SIZE + 2 * BLOCK_GAP); 
       mb.Y += y * (mh + BLOCK_SIZE + 2 * BLOCK_GAP); 

       spriteBatch.Draw(white, mb, BackColor); 
       mb.Inflate(-BLOCK_GAP, -BLOCK_GAP); 
       spriteBatch.Draw(white, mb, color); 
      } 

     bounds = new Rectangle(0, S, S, 3 * BLOCK_TOTAL_SIZE + BLOCK_GAP); 
     spriteBatch.Draw(white, bounds, BackColor); 
     for (int x = 0; x < 6; x++) 
      for (int y = 0; y < 3; y++) { 
       bounds = new Rectangle(x * BLOCK_TOTAL_SIZE, S + BLOCK_GAP + y * BLOCK_TOTAL_SIZE, BLOCK_SIZE, BLOCK_SIZE); 
       spriteBatch.Draw(white, bounds, (x == 1 && y == 0 || x > 0 && y == 1) ? color : Color.White); 
      } 
    } 
0

:) 내가 나중에 문제를 내가 질문을 하루 자신을 해결 끝났다. 이것은 내 코드는 모습입니다 같은 :

namespace Ludo 

{ 클래스 배경 {

//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}}; 

    //Create array that will gives the numbers in myArray the right sprites. 
    Texture2D[] textureArray = new Texture2D[26]; 

    // ContentManager that is loadedes textures and position for the objects in to game1 
    public void LoadContent(ContentManager content) 
    { 

     // Binds the places in textureArray with the right sprites 
     textureArray[0] = content.Load<Texture2D>("blue-block"); 
     textureArray[1] = content.Load<Texture2D>("red-block"); 
     textureArray[2] = content.Load<Texture2D>("yellow-block"); 
     textureArray[3] = content.Load<Texture2D>("green-block"); 
     textureArray[4] = content.Load<Texture2D>("white-block"); 
     textureArray[5] = content.Load<Texture2D>("blue-red-block"); 
     textureArray[6] = content.Load<Texture2D>("green-blue-block"); 
     textureArray[7] = content.Load<Texture2D>("green-yellow-block"); 
     textureArray[8] = content.Load<Texture2D>("yellow-red-block"); 
     textureArray[9] = content.Load<Texture2D>("x-center-block"); 
     textureArray[10] = content.Load<Texture2D>("blue-bottomleft-block"); 
     textureArray[11] = content.Load<Texture2D>("blue-bottomright-block"); 
     textureArray[12] = content.Load<Texture2D>("blue-topleft-block"); 
     textureArray[13] = content.Load<Texture2D>("blue-topright-block"); 
     textureArray[14] = content.Load<Texture2D>("red-bottomleft-block"); 
     textureArray[15] = content.Load<Texture2D>("red-bottomright-block"); 
     textureArray[16] = content.Load<Texture2D>("red-topleft-block"); 
     textureArray[17] = content.Load<Texture2D>("red-topright-block"); 
     textureArray[18] = content.Load<Texture2D>("yellow-bottomleft-block"); 
     textureArray[19] = content.Load<Texture2D>("yellow-bottomright-block"); 
     textureArray[20] = content.Load<Texture2D>("yellow-topleft-block"); 
     textureArray[21] = content.Load<Texture2D>("yellow-topright-block"); 
     textureArray[22] = content.Load<Texture2D>("green-bottomleft-block"); 
     textureArray[23] = content.Load<Texture2D>("green-bottomright-block"); 
     textureArray[24] = content.Load<Texture2D>("green-topleft-block"); 
     textureArray[25] = content.Load<Texture2D>("green-topright-block"); 



    } 
    // Method that draws the sprites to the screen. 
    public void Draw(SpriteBatch spriteBatch) 
    { 
     for (int y = 0; y < 15; y++) { 
      for (int x = 0; x < 15; x++) { 
       spriteBatch.Draw(textureArray[myArray[x, y]], new Rectangle(x * 45, y * 45, 45, 45), Color.White); 
      } 
     } 

    } 
} 

}