Почему мои плитки стали маленькими?
В настоящее время я делаю игру, которую я представлю для моего предмета разработки программного обеспечения в школе. По правде говоря, я не опытен в C#, поэтому я следую этому уроку TileEngine. Но когда я добавляю плитки в игру, мои плитки оказываются такими маленькими. Я даже использовал 256 х 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);
}
}
}
3 ответа
Вам просто нужно заменить 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);
Итак, проблема заключалась в том, что в вашем коде ширина и высота прямоугольника текстуры были установлены на ширину и высоту вашей карты - что, по-видимому, намного меньше размера изображения вашей текстуры.
Измените ширину и высоту каждой плитки, чтобы сделать ее больше:
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);
Ты можешь попробовать вот так
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 ....