So I'm not an incredibly experienced programmer, but I know a thing or two, so I've been working on a little game that I don't really know what will happen with. It'll likely just end up being me messing around. One of the things I'm trying to implement is random terrain generation. I have it sort of implemented, but it's really...weird. Just so you know, it's written in C# with Monogame, and I'm using Visual Studio 2013 Express at the moment. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Fantasy_RPG
{
class Tile
{
List<Texture2D> textureList;
Texture2D tileTexture;
const int TileWidth = 64;
const int TileHeight = 64;
private Vector2 position;
public Tile(ContentManager content, Vector2 position)
{
this.position = position;
textureList = new List<Texture2D>();
LoadTextures(content);
Generate();
}
void LoadTextures(ContentManager content)
{
textureList.Add(content.Load<Texture2D>("My Sprites/Terrain/Grass1"));
textureList.Add(content.Load<Texture2D>("My Sprites/Terrain/Grass2"));
}
void Generate()
{
Random random = new Random();
int x = random.Next(0, textureList.Count);
Console.WriteLine(x);
for (int i = 0; i < textureList.Count; i++)
{
if (x == i)
{
tileTexture = textureList[i];
Console.WriteLine(i);
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(tileTexture, position, Color.White);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Fantasy_RPG
{
class TileGenerator
{
private List<Tile> tiles;
public TileGenerator(ContentManager content)
{
tiles = new List<Tile>();
Populate(content);
}
void Populate(ContentManager content)
{
for (int x = 0; x <= 1440; x += 64)
{
for (int y = 0; y <= 900; y += 64)
{
tiles.Add(new Tile(content, new Vector2(x, y)));
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Tile tile in tiles)
{
tile.Draw(spriteBatch);
}
}
}
}
This is then drawn properly in my Main class, but I don't think you'll want more code to read through .
So this sort of works, but it's more like chunks. The way I have it right now, there are two textures: a light green and a dark green texture. The background is usually dominated mostly by one, with chunks of the other mixed in. Oddly enough, if I comment out the
Console.WriteLine(x);
and
Console.WriteLine(i);
lines in the Tile class, it randomly makes the entire background one of the colors. If I comment out either of these lines, it is either entirely dominated by one color or just about completely dominated by one color more so than it normally is. I know enough about programming to know that random generation is really pseudo-random and based on time, so I've deduced that that must be why those lines affect it. However, my question now is, how can I fix this to make it work to be fully random, regardless of time impacted by console output lines I was using for debugging?
Thanks in advance!