1

我目前正在做一个游戏,我将在学校展示我的软件工程学科。说实话,我在 C# 方面没有经验,这就是我关注这个 TileEngine 教程的原因。但是当我在向游戏中添加图块时,我的图块被渲染得如此之小。我什至使用了 256 x 256 的图像。就像在教程视频中一样。

您可以在此处查看问题的屏幕截图。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TileEngine
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        List<Texture2D> tileTextures = new List<Texture2D>();

        int[,] tileMap = new int[,]
        {
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
        };

        int tileWidth = 64;
        int tileHeight = 64;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Texture2D texture;

            texture = Content.Load<Texture2D>("Tiles/se_free_dirt_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_grass_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_ground_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_mud_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_road_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_rock_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_wood_texture");
            tileTextures.Add(texture);
        }

        protected override void UnloadContent() { }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            int tileMapWidth = tileMap.GetLength(1);
            int tileMapHeight = tileMap.GetLength(0);

            for (int x = 0; x < tileMapWidth; x++)
            {
                for (int y = 0; y < tileMapHeight; y++)
                {
                    int textureIndex = tileMap[y, x];
                    Texture2D texture = tileTextures[textureIndex];

                    spriteBatch.Draw(
                        texture,
                        new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
                        Color.White);
                }
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
4

3 回答 3

2

您只需要替换以下块中的第 3 行:

spriteBatch.Draw(
    texture,
    new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
    Color.White);

所以它就像下一个块的第 3 行:

spriteBatch.Draw(
    texture,
    new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight),
    Color.White);

所以问题是:在你的代码中,纹理的矩形宽度高度被设置为你的地图宽度高度——这似乎比你的纹理图像的大小要小得多。

于 2013-11-22T17:04:12.607 回答
1

更改每个图块的宽度和高度以使其更大:

int tileWidth = 300;
int tileHeight = 300;

基本上添加任何数字产生你喜欢的大小。

此外,矩形是通过添加起点 + 大小形成的,因此大小也需要与点匹配,否则它们将被绘制在彼此之上。

例如,如果您想将其呈现为正方形,则 titleWidth 应与 tileMapWidth 相同。

目前看起来它是矩阵的长度,这肯定是错误的,因为这个数字很小(10)并且会呈现小方块(10x10)。

因此这样做:

int tileMapWidth = tileWidth;
int tileMapHeight = tileHeight;

代替

int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);
于 2013-11-22T16:03:18.503 回答
0

你可以试试这样

width = 64 ' or texture.width
height = 64 ' or texture.height
pos = new vector2d(x * width , y * height)
spriteBatch.Draw(texture, pos, Color.White);

如果 x 从 0 开始,则第一个纹理将在 0,0 秒处 64,0 ....

于 2013-11-22T18:30:37.797 回答