Created
January 21, 2016 08:52
-
-
Save zyzof/3bc1803b74160465e510 to your computer and use it in GitHub Desktop.
Implementation of basic "Drunkard's Walk" path/map generation. Outputs a bitmap image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
namespace MapGen | |
{ | |
class Program | |
{ | |
static readonly int MAP_WIDTH = 1000; | |
static readonly int MAP_HEIGHT = 1000; | |
static readonly int NUM_TILES = 100000; | |
static readonly Vector START = new Vector(255, 255); | |
static void Main(string[] args) | |
{ | |
Vector currentPos = START; | |
Console.WriteLine("Creating new bitmap"); | |
Bitmap bmp = new Bitmap(MAP_WIDTH, MAP_HEIGHT); | |
for (int i = 0; i < MAP_WIDTH; i++) | |
{ | |
for (int j = 0; j < MAP_HEIGHT; j++) | |
{ | |
bmp.SetPixel(i, j, Color.Black); | |
} | |
} | |
Random rand = new Random(); | |
for (int i = 0; i < NUM_TILES; i++) | |
{ | |
int direction = rand.Next(1, 5); | |
switch (direction) | |
{ | |
case (int)Direction.N: | |
currentPos.y++; | |
break; | |
case (int)Direction.E: | |
currentPos.x++; | |
break; | |
case (int)Direction.S: | |
currentPos.y--; | |
break; | |
case (int)Direction.W: | |
currentPos.x--; | |
break; | |
default: | |
break; | |
} | |
bmp.SetPixel(currentPos.x, currentPos.y, Color.White); | |
Console.WriteLine("Setting tile at " + currentPos.x + ", " + currentPos.y); | |
} | |
Graphics gBmp = Graphics.FromImage(bmp); | |
gBmp.Dispose(); | |
bmp.Save("image.png", ImageFormat.Png); | |
} | |
} | |
enum Direction | |
{ | |
N = 1, | |
E = 2, | |
S = 3, | |
W = 4 | |
} | |
internal struct Vector | |
{ | |
public int x; | |
public int y; | |
public int z; | |
public Vector(int x, int y, int z = 0) | |
{ | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment