2010-05-08 4 views
7

내 게임 격자를 사각형 배열로 나누고 싶습니다. 각 직사각형은 40x40이고 모든 열에 14 개의 직사각형이 있으며 총 25 개의 열이 있습니다. 여기에는 560x1000의 게임 영역이 포함됩니다. 나는이 작품 확신 해요,하지만 사각형이 없기 때문에 물론 나는 그것을 확신 할 수 없습니다XNA를 사용하여 게임 창에 사각형 표시

Rectangle[] gameTiles = new Rectangle[15]; 

for (int i = 0; i <= 15; i++) 
{ 
    gameTiles[i] = new Rectangle(0, i * 40, 40, 40); 
} 

:

내가 게임 그리드에 사각형의 첫 번째 열을 만들기 위해 설정 한 코드입니다 화면에서 렌더링하여 실제로 볼 수있게 해줍니다. 디버깅 목적으로 경계선을 렌더링하거나 직사각형에 색상을 채워서 게임 자체에서 볼 수 있도록하는 것이 좋습니다.

이렇게하는 방법이 있습니까? 아니면 비교적 간단한 방법으로이 방법을 사용할 수 있습니까?

대단히 감사합니다.

답변

23

첫째, 구형 흰색의 1x1 픽셀 질감을 만들 :

var t = new Texture2D(GraphicsDevice, 1, 1); 
t.SetData(new[] { Color.White }); 

지금, 당신은 사각형을 렌더링 할 필요가 - 사각형이 rectangle라고 가정합니다. 채워진 블록을 렌더링하려면 매우 간단합니다. 색을 Color으로 설정하여 원하는 색상으로 설정해야합니다. 이 코드를 사용하십시오 :

spriteBatch.Draw(t, rectangle, Color.Black); 

테두리가 더 복잡합니다. 당신은 윤곽을 구성하는 4 개 라인을 그릴 수있다 (여기에 사각형 r입니다) :

int bw = 2; // Border width 

spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, bw, r.Height), Color.Black); // Left 
spriteBatch.Draw(t, new Rectangle(r.Right, r.Top, bw, r.Height), Color.Black); // Right 
spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, r.Width , bw), Color.Black); // Top 
spriteBatch.Draw(t, new Rectangle(r.Left, r.Bottom, r.Width, bw), Color.Black); // Bottom 

는 희망이 도움이!

+1

+1 멋진 대답하여 그리기() 메소드에서 다음

// At the top of your class: Texture2D pixel; // Somewhere in your LoadContent() method: pixel = new Texture2D(GameBase.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); pixel.SetData(new[] { Color.White }); // so that we can draw whatever color we want on top of it 

같은 뭔가. 정확히 내가 뭘 찾고 있었습니까! 감사합니다 –

+0

+1 짧고 정확하게 필요한. – FRoZeN

0

기존 질감 위에 직사각형을 그리려면 완벽하게 작동합니다. 테스트 할 때/가

도형 그리기에 대한 기본 트릭은 하나의 픽셀을 만드는 것입니다 ----- 사이트에서 충돌

http://bluelinegamestudios.com/blog/posts/drawing-a-hollow-rectangle-border-in-xna-4-0/

-----에 대한 참조 대 텍스처는 흰색이며 다른 색상과 혼합하여 솔리드 모양으로 표시 할 수 있습니다.

spriteBatch.Begin(); 

// Create any rectangle you want. Here we'll use the TitleSafeArea for fun. 
Rectangle titleSafeRectangle = GraphicsDevice.Viewport.TitleSafeArea; 

// Call our method (also defined in this blog-post) 
DrawBorder(titleSafeRectangle, 5, Color.Red); 

spriteBatch.End(); 

그리고 드로잉을 수행하는 실제 방법 :

private void DrawBorder(Rectangle rectangleToDraw, int thicknessOfBorder, Color borderColor) 
{ 
    // Draw top line 
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, rectangleToDraw.Width, thicknessOfBorder), borderColor); 

    // Draw left line 
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, thicknessOfBorder, rectangleToDraw.Height), borderColor); 

    // Draw right line 
    spriteBatch.Draw(pixel, new Rectangle((rectangleToDraw.X + rectangleToDraw.Width - thicknessOfBorder), 
            rectangleToDraw.Y, 
            thicknessOfBorder, 
            rectangleToDraw.Height), borderColor); 
    // Draw bottom line 
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, 
            rectangleToDraw.Y + rectangleToDraw.Height - thicknessOfBorder, 
            rectangleToDraw.Width, 
            thicknessOfBorder), borderColor); 
} 
관련 문제