我目前正在做一个游戏,我将在学校展示我的软件工程学科。说实话,我在 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);
}
}
}