使用 XNA 在游戏窗口中显示矩形

Displaying rectangles in game window with XNA(使用 XNA 在游戏窗口中显示矩形)

本文介绍了使用 XNA 在游戏窗口中显示矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的游戏网格划分为一个矩形数组.每个矩形为 40x40,每列有 14 个矩形,共 25 列.这涵盖了 560x1000 的游戏区域.

I want to divide my game grid into an array of rectangles. Each rectangle is 40x40 and there are 14 rectangles in every column, with a total of 25 columns. This covers a game area of 560x1000.

这是我设置的用于在游戏网格上制作第一列矩形的代码:

This is the code I have set up to make the first column of rectangles on the game grid:

Rectangle[] gameTiles = new Rectangle[15];

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

我很确定这可行,但我当然无法确认,因为矩形不会呈现在屏幕上,我无法亲眼看到它们.为了调试目的,我想做的是渲染一个边框,或者用颜色填充矩形,这样我就可以在游戏本身上看到它,只是为了确保它有效.

I'm pretty sure this works, but of course I cannot confirm it because rectangles do not render on the screen for me to physically see them. What I would like to do for debugging purposes is to render a border, or fill the rectangle with color so I can see it on the game itself, just to make sure this works.

有没有办法做到这一点?或者任何相对简单的方法我可以确保它有效?

Is there a way to make this happen? Or any relatively simple way I can just make sure that this works?

非常感谢.

推荐答案

首先,为矩形制作一个 1x1 像素的白色纹理:

First, make a 1x1 pixel texture of white for the rectangle:

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

现在,您需要渲染矩形 - 假设矩形被称为 rectangle.对于渲染填充块,它非常简单 - 确保将 tint Color 设置为您想要的颜色.只需使用此代码:

Now, you need to render the rectangle - assume the Rectangle is called rectangle. For a rendering a filled block, it is very simple - make sure to set the tint Color to be the colour you want. Just use this code:

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

对于边框,是否更复杂.你必须画出构成轮廓的 4 条线(这里的矩形是 r):

For a border, is it more complex. You have to draw the 4 lines that make up the outline (the rectangle here is 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

希望对你有帮助!

这篇关于使用 XNA 在游戏窗口中显示矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 XNA 在游戏窗口中显示矩形

基础教程推荐