2013-11-22 75 views
1

我目前正在做一個遊戲,我將爲我的軟件工程學科呈現在學校。說實話,我沒有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); 
     } 
    } 
} 

回答

1

更改每個tile的寬度和高度,使之更大:

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); 
+1

感謝您的快速反應主席先生,我做你告訴我,這是結果http://postimg.org/image/iwrigei5x/ – user3022332

+0

它只是增加了瓷磚之間的差距:( – user3022332

+0

看起來像瓷磚繪製,但你使用的紋理產生這個結果。 嘗試用簡單的邊框替換紋理,使用簡單的邊框 – Nick

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

所以問題是:在你的代碼紋理的矩形寬度高度設爲您的地圖寬度高度 - 這似乎是比你的紋理的規模要小得多圖片。

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第二....

相關問題