Skip to content

Instantly share code, notes, and snippets.

@arxae
Created March 28, 2017 21:33
Show Gist options
  • Save arxae/8baa5e4d49416f643703bbb195591210 to your computer and use it in GitHub Desktop.
Save arxae/8baa5e4d49416f643703bbb195591210 to your computer and use it in GitHub Desktop.
Simple roguelike map drawing in c# console
using System;
using System.Collections.Generic;
namespace rlike
{
class Program
{
static Random Rng;
static void Main(string[] args)
{
// Settings
const int MINROOMS = 3;
const int MAXROOMS = 7;
const int MINROOMSIZE = 3;
const int MAXROOMSIZE = 10;
// Init
Console.CursorVisible = false;
Console.SetWindowSize(50, 50);
Console.SetBufferSize(50, 50);
Rng = new Random();
FillScreen('█');
// Random rooms
var rooms = new List<Rect>();
var nrrooms = Rng.Next(MINROOMS, MAXROOMS);
for (int i = 0; i < nrrooms; i++)
{
int width = Rng.Next(MINROOMSIZE, MAXROOMSIZE);
int height = Rng.Next(MINROOMSIZE, MAXROOMSIZE);
int x = Rng.Next(0, Console.WindowWidth - width - 1);
int y = Rng.Next(0, Console.WindowHeight - height - 1);
var rect = new Rect(x, y, width, height);
rooms.Add(rect);
}
for (int r = 0; r < rooms.Count; r++)
{
FillRect(rooms[r], ' ');
if (r == 0) continue;
int previousRoomCenterX = rooms[r - 1].CenterX;
int previousRoomCenterY = rooms[r - 1].CenterY;
int currentRoomCenterX = rooms[r].CenterX;
int currentRoomCenterY = rooms[r].CenterY;
if (Rng.Next(0, 2) == 0)
{
HorizontalTunnel(previousRoomCenterX, currentRoomCenterX, previousRoomCenterY);
VerticalTunnel(previousRoomCenterY, currentRoomCenterY, currentRoomCenterX);
}
else
{
VerticalTunnel(previousRoomCenterY, currentRoomCenterY, previousRoomCenterX);
HorizontalTunnel(previousRoomCenterX, currentRoomCenterX, currentRoomCenterY);
}
}
Console.ReadKey();
}
static void FillScreen(char c)
{
for (int top = 0; top < Console.WindowHeight; top++)
{
string line = string.Empty;
for (int left = 0; left < Console.WindowWidth; left++)
{
line += c;
}
Console.SetCursorPosition(0, top);
Console.Write(line);
}
}
static void FillRect(Rect r, char c)
{
Console.SetCursorPosition(r.Left, r.Top);
for (int top = r.Top; top < r.Top + r.Height; top++)
{
for (int left = r.Left; left < r.Left + r.Width; left++)
{
Console.SetCursorPosition(left, top);
Console.Write(c);
}
}
}
static void HorizontalTunnel(int xStart, int xEnd, int y)
{
for (int x = Math.Min(xStart, xEnd); x <= Math.Max(xStart, xEnd); x++)
{
Console.SetCursorPosition(x, y);
Console.Write(' ');
}
}
static void VerticalTunnel(int yStart, int yEnd, int x)
{
for (int y = Math.Min(yStart, yEnd); y <= Math.Max(yStart, yEnd); y++)
{
Console.SetCursorPosition(x, y);
Console.Write(' ');
}
}
}
public class Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int CenterX { get { return (Left + (Width / 2)); } }
public int CenterY { get { return (Top + (Height / 2)); } }
public Rect(int l, int t, int w, int h)
{
Left = l;
Top = t;
Width = w;
Height = h;
}
}
}
@tvance929
Copy link

this is so helpful! Now I need to figure out how to check for collision before drawing a room... thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment