为什么我的瓷砖变小了?

时间:2013-11-22 15:59:36

标签: c# visual-studio-2010 xna tile tile-engine

我正在做一个游戏,我将在学校为我的软件工程学科提出。说实话,我没有C#的经验,这就是我跟随this TileEngine tutorial的原因。但是,当我将游戏添加到游戏中时,我的拼贴会变得如此之小。我甚至使用256 x 256图像。就像在教程视频中一样。

您可以看到问题here的屏幕截图。这是我的代码:

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

3 个答案:

答案 0 :(得分: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);

问题是:在代码纹理的矩形 width 中, height 设置为地图 宽度高度 - 它看起来比纹理图像的尺寸小得多。

答案 1 :(得分: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);

答案 2 :(得分: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开始,那么第一个纹理将在64,0 ....处于0,0秒....