Draw textures(tiles) from txt-file (Monogame) HELP

  • Thread starter Thread starter Draw textures from txt-file (monogame)
  • Start date Start date
D

Draw textures from txt-file (monogame)

Guest
I'm currently working on a PacMan game in Monogame and I'm having trouble with drawing my different textures i need for the map(tileGrid)

I've wrote a txt-file with four different letters in it, together painting the "walls" "corridors" "corridors with food-dots" and "Power-Ups"

When I run it, is says that my



public void Draw(SpriteBatch spriteBatch)
{
foreach (Tile tile in tileGrid)
{
tile.Draw(spriteBatch);
}

}

is null. when i set a breakpoint, I can see that it's my "tile" which is null. This part in from a class called Maze.

This is what I have so far:

------------------------------------------------------------------------------------------------------------

CLASS MAZE:


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PM
{
class Maze
{
Texture2D wallTex;
Texture2D corridorTex;
Texture2D foodwayTex;
Texture2D powerUpTex;

Rectangle tiles;

Tile[,] tileGrid = new Tile[30, 30];
string tileText;
Char[] splitt = { ' ' };

public Maze()
{

}
public void LoadTexture(ContentManager contentMan)
{
powerUpTex = contentMan.Load<Texture2D>("TileTextures/powerUp");
foodwayTex = contentMan.Load<Texture2D>("TileTextures/food");
corridorTex = contentMan.Load<Texture2D>("TileTextures/corridor");
wallTex = contentMan.Load<Texture2D>("TileTextures/textureWall");
char[] splittChar = { ' ' };
for (int i = 0; i < tileGrid.GetLength(0); i++)
{
for (int j = 0; j < tileGrid.GetLength(1); j++)
{
tiles = new Rectangle(0, 0, 32, 32);
tileGrid[i, j] = new Tile(wallTex, tiles);
}
}
StreamReader sr = new StreamReader("PacMan_MatDots.txt");
while (!sr.EndOfStream)
{
tileText = sr.ReadLine();
string[] splittedTextFile = tileText.Split(splitt);
for (int i = 0; i < tileGrid.GetLength(0); i++)
{
for (int j = 0; j < tileGrid.GetLength(1); j++)
{
if (tileText == "w")
{
tileGrid[i, j] = new Tile(wallTex, tiles);
}
else if (tileText == "c")
{
tileGrid[i, j] = new Tile(corridorTex, tiles);
}
else if (tileText == "f")
{
tileGrid[i, j] = new Tile(foodwayTex, tiles);
}
else if (tileText == "p")
{
tileGrid[i, j] = new Tile(powerUpTex, tiles);
}
}
}
}
sr.Close();
}

public void Draw(SpriteBatch spriteBatch)
{
foreach (Tile tile in tileGrid)
{
tile.Draw(spriteBatch);
}

}
}
}


-----------------------------------------------------------------------------------------------------

CLASS TILE



using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Remoting.Contexts;
using Microsoft.Xna.Framework.Content;

namespace PM
{
class Tile
{
ContentManager contentManager;

Texture2D texture;
Rectangle tileRect = new Rectangle(0, 0, 32, 32);

public Tile(Texture2D texture, Rectangle rectangle)
{
this.texture = texture;
this.tileRect = rectangle;
}

public void LoadTexture(ContentManager contentManager)
{

}

public void Update(GameTime gametime)
{

}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, tileRect, Color.White);
}
}
}


------------------------------------------------------------------------------------------------------

CLASS GAMESTATE


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace PM
{
public enum GameState
{
titleScreen,
play,
pause,
gameOverScreen
}


public class GameStates
{
public GameState currentGameState = new GameState();
public GraphicsDeviceManager graphics;
Maze maze = new Maze();


Texture2D titleScreenBackground;

public GameStates()
{

}

public void LoadTextures(ContentManager contentManager)
{
titleScreenBackground = contentManager.Load<Texture2D>("Enter");
maze.LoadTexture(contentManager);
}
public void Update(GameTime gameTime)
{
switch (currentGameState)
{
case GameState.titleScreen:
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
currentGameState = GameState.play;
}
break;
case GameState.play:
//code
break;
case GameState.pause:
//code
break;
case GameState.gameOverScreen:
//code
break;
}
}
public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
{
switch (currentGameState)
{
case GameState.titleScreen:
spriteBatch.Draw(titleScreenBackground, new Rectangle(0, 0, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height), Color.White);
//if (Keyboard.GetState().IsKeyDown(Keys.Enter))
//{
// currentGameState = GameState.play;
//}
break;
case GameState.play:
maze.Draw(spriteBatch);
break;
case GameState.pause:
//code
break;
case GameState.gameOverScreen:
//code
break;
}
}
}

}



-------------------------------------------------------------------------------------------------------

GAME1


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PM
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Maze maze = new Maze();




GameStates gameStat = new GameStates();

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = 700;
graphics.PreferredBackBufferHeight = 800;


}

protected override void Initialize()
{

base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
gameStat.LoadTextures(Content);
}

protected override void UnloadContent()
{
}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
gameStat.Update(gameTime);


base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
gameStat.Draw(spriteBatch, graphics.GraphicsDevice);
maze.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}


Can you see where the problem is?
Have no red-lines in the code fiy.

All the spritetextures are uploaded in Content.

The txtfile looks like this:


wwwwwwwwwwwwwwwwwwwwwwwwwwwwww
ww111111111111ww111111111111ww
wwcwwww1wwwww1ww1wwwww1wwwwcww
wwpwwww1wwwww1ww1wwwww1wwwwpww
wwcwwww1wwwww1ww1wwwww1wwwwcww
ww11111111111111111111111111ww
ww1wwww1ww1wwwwwwww1ww1wwww1ww
ww1wwww1ww1wwwwwwww1ww1wwww1ww
ww111111ww1111ww1111ww111111ww
wwwwwww1wwwww1ww1wwwww1wwwwwww
wwwwwww1wwwww1ww1wwwww1wwwwwww
wwwwwww1ww111cccc111ww1wwwwwww
wwwwwww1ww1wwwccwww1ww1wwwwwww
wwwwwww1ww1wccccccw1ww1wwwwwww
ww111111111wccccccw1111ccccccw
wwwwwww1ww1wccccccw1ww1wwwwwww
wwwwwww1ww1wwwwwwww1ww1wwwwwww
wwwwwww1ww1111111111ww1wwwwwww
wwwwwww1ww1wwwwwwww1ww1wwwwwww
wwwwwww1ww1wwwwwwww1ww1wwwwwww
ww111111111111ww111111111111ww
ww1wwww1wwwww1ww1wwwww1wwww1ww
wwpwwww1wwwww1ww1wwwww1wwwwpww
ww1wwww1111111cc1111111wwww1ww
ww1wwww1ww1wwwwwwww1ww1wwww1ww
ww1wwww1ww1wwwwwwww1ww1wwww1ww
ww111111ww1111ww1111ww111111ww
ww1wwwwwwwwww1ww1wwwwwwwwww1ww
ww111111111111cc111111111111ww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Continue reading...
 
Back
Top