FC2ブログ

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

【c#】 ランダムで部屋を作らないマップ

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SD = System.Drawing;

namespace Project1
{
internal static class Program
{
[STAThread]
private static void Main()
{
var window = new Window();
var canvas = new Canvas();
var image = new MapViewer();
var map = new MapCreator();

canvas.Children.Add(image);
canvas.Width = 500;
canvas.Height = 500;

window.MouseDown += (sender, e) =>
{
//クリックするとマップ生成
map.MapInitStart();
map.MapDraw(image);
};

window.ResizeMode = ResizeMode.CanMinimize;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = canvas;
window.ShowDialog();
}
}
///
/// マップ生成用
///
internal sealed class MapCreator
{
///
/// 壁
///
private const int wall = -3;
///
/// 通路
///
private const int passage = -2;
///
/// 部屋の出口
///
private const int exit = -1;
///
/// 部屋
///
private const int room = 0;

internal readonly Random Random = new Random();

///
/// マップ配列
///
internal readonly int[,] Map = new int[50, 50];
///
/// エリア管理
///

internal SD.Rectangle[] AreaArray
{
get;
private set;
}
///
/// 部屋管理
///

internal SD.Rectangle[] RoomArray
{
get;
private set;
}

internal MapCreator()
{

}

///
/// マップ生成開始
///
///
internal void MapInitStart()
{
this.MapWallSet();
this.AreaAndRoomArrayInit();
this.MapAreaSet();
this.RoomPassegeSet();
}

///
/// 一旦、マップの全てを壁にする
///
private void MapWallSet()
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
map[i, j] = MapCreator.wall;
}
}
}

///
/// マップにエリアを指定
///
private void MapAreaSet()
{
var map = this.Map;
var random = this.Random;


//どこに部屋を作らないかを決める
var noRoom = this.Random.Next(0, this.RoomArray.Length);


for (var i = 0; i < this.AreaArray.Length; i++)
{
var areaRect = this.AreaArray[i];

//部屋の位置を決める
var roomX = areaRect.X + random.Next(3, 12);
var roomY = areaRect.Y + random.Next(3, 12);
//部屋の大きさを決める
var roomWidth = random.Next(6, 12);
var roomHeight = random.Next(6, 12);


//指定されたエリアに部屋を作らない
if (i == noRoom)
{
//部屋を造らない
this.RoomArray[i] = new SD.Rectangle(roomX, roomY, 0, 0);
}
//それ以外のエリアに部屋を作る
else
{
//部屋を作る
this.CreateRoom(i, roomX, roomY, roomWidth, roomHeight);
}

}
}
///
/// エリアと部屋配列の初期化
///
private void AreaAndRoomArrayInit()
{
//エリアの数
const int areaCount = 4;

var map = this.Map;
var random = this.Random;

this.AreaArray = new SD.Rectangle[areaCount];
this.RoomArray = new SD.Rectangle[areaCount];

//エリアの大きさを決める
this.AreaArray[0] = new SD.Rectangle(1, 1, 24, 24);
this.AreaArray[1] = new SD.Rectangle(25, 1, 24, 24);
this.AreaArray[2] = new SD.Rectangle(1, 25, 24, 24);
this.AreaArray[3] = new SD.Rectangle(25, 25, 24, 24);
}
///
/// 部屋を作る
///
private void CreateRoom(int roomNo, int x, int y, int width, int height)
{
//部屋サイズを決める
this.RoomArray[roomNo] = new SD.Rectangle(x, y, width, height);

//部屋を造る
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
//部屋を造る
this.Map[x + i, y + j] = MapCreator.room;
}
}
}

///
/// 通路の作成
///
private void RoomPassegeSet()
{
var room0 = 0;
var room1 = 1;
var room2 = 2;
var room3 = 3;

//部屋と部屋の間に通路を作る
this.RoomVerticalPassageSet(room0, room3);
this.RoomVerticalPassageSet(room1, room2);
}
///
/// 部屋2つを通路を縦線でつなぐ
///
private void RoomVerticalPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.X, room0.Right);
var exit1 = this.Random.Next(room1.X, room1.Right);

for (var i = 0; i < area0.Bottom - room0.Bottom; i++)
{
map[exit0, room0.Bottom + i] = MapCreator.passage;
}

for (var i = 0; i < room1.Top - area1.Top; i++)
{
map[exit1, room1.Top - 1 - i] = MapCreator.passage;
}
if (room0.Size != SD.Size.Empty) map[exit0, room0.Bottom] = MapCreator.exit;
if (room1.Size != SD.Size.Empty) map[exit1, room1.Top - 1] = MapCreator.exit;

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[exit1 + i, area0.Height] = MapCreator.passage;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[exit0 + i, area1.Height] = MapCreator.passage;
}
}
}
///
/// 部屋2つを通路を横線でつなぐ
///
private void RoomHorizonPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.Y, room0.Bottom);
var exit1 = this.Random.Next(room1.Y, room1.Bottom);

for (var i = 0; i < area0.Right - room0.Right; i++)
{
map[room0.Right + i, exit0] = MapCreator.passage;
}

for (var i = 0; i < room1.Left - area1.Left; i++)
{
map[room1.Left - 1 - i, exit1] = MapCreator.passage;
}
if (room0.Size != SD.Size.Empty) map[room0.Right, exit0] = MapCreator.exit;
if (room1.Size != SD.Size.Empty) map[room1.Left - 1, exit1] = MapCreator.exit;

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[area1.Width, exit1 + i] = MapCreator.passage;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[area0.Width, exit0 + i] = MapCreator.passage;
}
}
}

///
/// マップを描画
///
internal void MapDraw(MapViewer viewer)
{
using (DrawingContext dc = viewer.Drawing.Open())
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
const int size = 7;
const int offset = 10;

var brush = default(Brush);

switch (map[i, j])
{
case MapCreator.wall: brush = Brushes.Black; break;
case MapCreator.passage: brush = Brushes.Aqua; break;

case MapCreator.room: brush = Brushes.Green; break;
case MapCreator.exit: brush = Brushes.Yellow; break;
}
dc.DrawRectangle(brush, null, new Rect(i * size + offset, j * size + offset, size, size));
}
}
}
}
}

internal sealed class MapViewer : FrameworkElement
{
internal readonly DrawingGroup Drawing = new DrawingGroup();

internal MapViewer()
{

}

protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawDrawing(this.Drawing);
}
}
}
スポンサーサイト

【c#】 特定の部屋を作らないマップを作る

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SD = System.Drawing;

namespace Project1
{
internal static class Program
{
[STAThread]
private static void Main()
{
var window = new Window();
var canvas = new Canvas();
var image = new MapViewer();
var map = new MapCreator();

canvas.Children.Add(image);
canvas.Width = 500;
canvas.Height = 500;

window.MouseDown += (sender, e) =>
{
//クリックするとマップ生成
map.MapInitStart();
map.MapDraw(image);
};

window.ResizeMode = ResizeMode.CanMinimize;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = canvas;
window.ShowDialog();
}
}
///
/// マップ生成用
///
internal sealed class MapCreator
{
///
/// 壁
///
private const int wall = -1;
///
/// 部屋
///
private const int room = 1;

internal readonly Random Random = new Random();

///
/// マップ配列
///
internal readonly int[,] Map = new int[50, 50];
///
/// エリア管理
///

internal SD.Rectangle[] AreaArray
{
get;
private set;
}
///
/// 部屋管理
///

internal SD.Rectangle[] RoomArray
{
get;
private set;
}

internal MapCreator()
{

}

///
/// マップ生成開始
///
///
internal void MapInitStart()
{
this.MapWallSet();
this.MapAreaSet();
}
///
/// 一旦、マップの全てを壁にする
///
private void MapWallSet()
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
map[i, j] = MapCreator.wall;
}
}
}
///
/// マップにエリアを指定
///
private void MapAreaSet()
{
//エリアの数
const int areaCount = 4;

var map = this.Map;
var random = this.Random;

this.AreaArray = new SD.Rectangle[areaCount];
this.RoomArray = new SD.Rectangle[areaCount];

//エリアの大きさを決める
this.AreaArray[0] = new SD.Rectangle(1, 1, 24, 24);
this.AreaArray[1] = new SD.Rectangle(25, 1, 24, 24);
this.AreaArray[2] = new SD.Rectangle(1, 25, 24, 24);
this.AreaArray[3] = new SD.Rectangle(25, 25, 24, 24);

for (var i = 0; i < this.AreaArray.Length; i++)
{
var areaRect = this.AreaArray[i];


//部屋の位置を決める
var roomX = areaRect.X + random.Next(3, 12);
var roomY = areaRect.Y + random.Next(3, 12);
//部屋の大きさを決める
var roomWidth = random.Next(6, 12);
var roomHeight = random.Next(6, 12);

//とりあえず1番のエリアに部屋をつくらない
if (i == 1)
{
//1番のエリアに部屋を作らない
//
//(どこを行き止まりにするか、
// どこで曲がるか、は決める)
this.RoomArray[i] = new SD.Rectangle(roomX, roomY, 0, 0);
}
//1番以外のエリアに部屋を作る
else
{
//部屋サイズを決める
this.RoomArray[i] = new SD.Rectangle(roomX, roomY, roomWidth, roomHeight);
//部屋を作る
this.CreateRoom(i, roomX, roomY, roomWidth, roomHeight);
}

}

//通路の作成
this.RoomPassegeSet();
}


///
/// 部屋を作る
///
private void CreateRoom(int roomNo, int x, int y, int width, int height)
{
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
this.Map[x + i, y + j] = MapCreator.room;
}
}
}


private int passageCount = 0;
///
/// 通路の作成
///
private void RoomPassegeSet()
{
var room0 = 0;
var room1 = 1;
var room2 = 2;
var room3 = 3;

switch (this.passageCount)
{
case 0:
//部屋と部屋の間に通路を作る
this.RoomVerticalPassageSet(room0, room3);
this.RoomVerticalPassageSet(room1, room2);
break;

case 1:
//部屋と部屋の間に通路を作る
this.RoomHorizonPassageSet(room0, room1);
this.RoomHorizonPassageSet(room2, room3);

this.RoomVerticalPassageSet(room1, room2);
break;
}

this.passageCount++;
if (this.passageCount > 1) this.passageCount = 0;
}
///
/// 部屋2つを通路を縦線でつなぐ
///
private void RoomVerticalPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.X, room0.Right);
var exit1 = this.Random.Next(room1.X, room1.Right);

for (var i = 0; i < area0.Bottom - room0.Bottom; i++)
{
map[exit0, room0.Bottom + i] = 0;
}

for (var i = 0; i < room1.Top - area1.Top; i++)
{
map[exit1, room1.Top - 1 - i] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[exit1 + i, area0.Height] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[exit0 + i, area1.Height] = 0;
}
}
}
///
/// 部屋2つを通路を横線でつなぐ
///
private void RoomHorizonPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.Y, room0.Bottom);
var exit1 = this.Random.Next(room1.Y, room1.Bottom);

for (var i = 0; i < area0.Right - room0.Right; i++)
{
map[room0.Right + i, exit0] = 0;
}

for (var i = 0; i < room1.Left - area1.Left; i++)
{
map[room1.Left - 1 - i, exit1] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[area1.Width, exit1 + i] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[area0.Width, exit0 + i] = 0;
}
}
}

///
/// マップを描画
///
internal void MapDraw(MapViewer viewer)
{
using (DrawingContext dc = viewer.Drawing.Open())
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
const int size = 7;
const int offset = 10;

var brush = default(Brush);

switch (map[i, j])
{
case MapCreator.wall: brush = Brushes.Black; break;
case MapCreator.room: brush = Brushes.Green; break;
}
dc.DrawRectangle(brush, null, new Rect(i * size + offset, j * size + offset, size, size));
}
}
}
}
}

internal sealed class MapViewer : FrameworkElement
{
internal readonly DrawingGroup Drawing = new DrawingGroup();

internal MapViewer()
{

}

protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawDrawing(this.Drawing);
}
}
}

【c#】 ランダムな通路を作る

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SD = System.Drawing;

namespace Project1
{
internal static class Program
{
[STAThread]
private static void Main()
{
var window = new Window();
var canvas = new Canvas();
var image = new MapViewer();
var map = new MapCreator();

canvas.Children.Add(image);
canvas.Width = 500;
canvas.Height = 500;

window.MouseDown += (sender, e) =>
{
//クリックするとマップ生成
map.MapInitStart();
map.MapDraw(image);
};

window.ResizeMode = ResizeMode.CanMinimize;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = canvas;
window.ShowDialog();
}
}
///
/// マップ生成用
///
internal sealed class MapCreator
{
///
/// 壁
///
private const int wall = -1;
///
/// 部屋
///
private const int room = 1;

internal readonly Random Random = new Random();

///
/// マップ配列
///
internal readonly int[,] Map = new int[50, 50];
///
/// エリア管理
///

internal SD.Rectangle[] AreaArray
{
get;
private set;
}
///
/// 部屋管理
///

internal SD.Rectangle[] RoomArray;

internal MapCreator()
{

}

///
/// マップ生成開始
///
///
internal void MapInitStart()
{
this.MapWallSet();
this.MapAreaSet();
}
///
/// 一旦、マップの全てを壁にする
///
private void MapWallSet()
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
map[i, j] = MapCreator.wall;
}
}
}
///
/// マップにエリアを指定
///
private void MapAreaSet()
{
//エリアの数
const int areaCount = 4;

var map = this.Map;

var random = new Random();

this.AreaArray = new SD.Rectangle[areaCount];
this.RoomArray = new SD.Rectangle[areaCount];

//エリアの大きさを決める
this.AreaArray[0] = new SD.Rectangle(1, 1, 24, 24);
this.AreaArray[1] = new SD.Rectangle(25, 1, 24, 24);
this.AreaArray[2] = new SD.Rectangle(1, 25, 24, 24);
this.AreaArray[3] = new SD.Rectangle(25, 25, 24, 24);

for (var num = 0; num < this.AreaArray.Length; num++)
{
var areaRect = this.AreaArray[num];

//部屋の位置を決める
var roomX = areaRect.X + random.Next(3, 12);
var roomY = areaRect.Y + random.Next(3, 12);
//部屋の大きさを決める
var roomWidth = random.Next(6, 12);
var roomHeight = random.Next(6, 12);

this.RoomArray[num] = new SD.Rectangle(roomX, roomY, roomWidth, roomHeight);

for (var i = 0; i < roomWidth; i++)
{
for (var j = 0; j < roomHeight; j++)
{
map[roomX + i, roomY + j] = MapCreator.room;
}
}
}

//通路の作成
this.RoomPassegeSet();
}

///
/// 通路の作成
///
private void RoomPassegeSet()
{
var room0 = 0;
var room1 = 1;
var room2 = 2;
var room3 = 3;


//さまざまなパターンの通路を作る
switch (this.Random.Next(0, 2))
{
case 0:
//部屋と部屋の間に通路を作る
this.RoomVerticalPassageSet(room0, room3);
this.RoomVerticalPassageSet(room1, room2);
break;

case 1:
//部屋と部屋の間に通路を作る
this.RoomHorizonPassageSet(room0, room1);
this.RoomHorizonPassageSet(room2, room3);

this.RoomVerticalPassageSet(room1, room2);
break;
}

}
///
/// 部屋2つを通路を縦線でつなぐ
///
private void RoomVerticalPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.X, room0.Right);
var exit1 = this.Random.Next(room1.X, room1.Right);

for (var i = 0; i < area0.Bottom - room0.Bottom; i++)
{
map[exit0, room0.Bottom + i] = 0;
}

for (var i = 0; i < room1.Top - area1.Top; i++)
{
map[exit1, room1.Top - 1 - i] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[exit1 + i, area0.Height] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[exit0 + i, area1.Height] = 0;
}
}
}
///
/// 部屋2つを通路を横線でつなぐ
///
private void RoomHorizonPassageSet(int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = this.RoomArray[roomNum0];
var room1 = this.RoomArray[roomNum1];

var area0 = this.AreaArray[roomNum0];
var area1 = this.AreaArray[roomNum1];

var exit0 = this.Random.Next(room0.Y, room0.Bottom);
var exit1 = this.Random.Next(room1.Y, room1.Bottom);

for (var i = 0; i < area0.Right - room0.Right; i++)
{
map[room0.Right + i, exit0] = 0;
}

for (var i = 0; i < room1.Left - area1.Left; i++)
{
map[room1.Left - 1 - i, exit1] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[area1.Width, exit1 + i] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[area0.Width, exit0 + i] = 0;
}
}
}

///
/// マップを描画
///
internal void MapDraw(MapViewer viewer)
{
using (DrawingContext dc = viewer.Drawing.Open())
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
const int size = 7;
const int offset = 10;

var brush = default(Brush);

switch (map[i, j])
{
case MapCreator.wall: brush = Brushes.Black; break;
case MapCreator.room: brush = Brushes.Green; break;
}
dc.DrawRectangle(brush, null, new Rect(i * size + offset, j * size + offset, size, size));
}
}
}
}
}

internal sealed class MapViewer : FrameworkElement
{
internal readonly DrawingGroup Drawing = new DrawingGroup();

internal MapViewer()
{

}

protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawDrawing(this.Drawing);
}
}
}

「魔法少女は再び旅に出る」を更新しました

http://www.freem.ne.jp/win/game/8764

更新しました
現在レベル3まで遊べます

【c#】 部屋と部屋を通路でつなぐ

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SD = System.Drawing;

namespace Project1
{
internal static class Program
{
[STAThread]
private static void Main()
{
var window = new Window();
var canvas = new Canvas();
var image = new MapCreator.MapViewer();
var map = new MapCreator();

canvas.Children.Add(image);
canvas.Width = 500;
canvas.Height = 500;

window.MouseDown += (sender, e) =>
{
//クリックするとマップ生成
map.MapInitStart(image);
};

window.ResizeMode = ResizeMode.CanMinimize;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = canvas;
window.ShowDialog();
}
}
///
/// マップ生成用
///

internal sealed class MapCreator
{
///
/// 壁
///

private const int wall = -1;
///
/// 部屋
///

private const int room = 1;

///
/// マップ配列
///

internal readonly int[,] Map = new int[50, 50];

internal MapCreator()
{

}

///
/// マップ生成開始
///

///
internal void MapInitStart(MapViewer image)
{
var dg = new DrawingGroup();

this.MapWallSet();
this.MapAreaSet();
this.MapDraw(dg);

image.Drawing = new DrawingImage(dg);
}
///
/// 一旦、マップの全てを壁にする
///

private void MapWallSet()
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
map[i, j] = MapCreator.wall;
}
}
}
///
/// マップにエリアを指定
///

private void MapAreaSet()
{
//エリアの数
const int areaCount = 4;

var map = this.Map;

var random = new Random();

var areaArray = new SD.Rectangle[areaCount];
var roomArray = new SD.Rectangle[areaCount];

//エリアの大きさを決める
areaArray[0] = new SD.Rectangle(1, 1, 24, 24);
areaArray[1] = new SD.Rectangle(25, 1, 24, 24);
areaArray[2] = new SD.Rectangle(1, 25, 24, 24);
areaArray[3] = new SD.Rectangle(25, 25, 24, 24);

for (var num = 0; num < areaArray.Length; num++)
{
var areaRect = areaArray[num];

//部屋の位置を決める
var roomX = areaRect.X + random.Next(3, 12);
var roomY = areaRect.Y + random.Next(3, 12);
//部屋の大きさを決める
var roomWidth = random.Next(3, 12);
var roomHeight = random.Next(3, 12);

roomArray[num] = new SD.Rectangle(roomX, roomY, roomWidth, roomHeight);

for (var i = 0; i < roomWidth; i++)
{
for (var j = 0; j < roomHeight; j++)
{
map[roomX + i, roomY + j] = MapCreator.room;
}
}
}

//通路の作成
this.RoomPassegeSet(random, roomArray, areaArray);
}


///
/// 通路の作成
///

private void RoomPassegeSet(Random random, SD.Rectangle[] roomArray, SD.Rectangle[] areaArray)
{
var room0 = 0;
var room1 = 1;
var room2 = 2;
var room3 = 3;

//部屋と部屋の間に通路を作る
this.RoomVerticalPassageSet(random, roomArray, areaArray, room0, room2);
this.RoomVerticalPassageSet(random, roomArray, areaArray, room1, room3);
this.RoomHorizonPassageSet(random, roomArray, areaArray, room0, room3);
}
///
/// 部屋2つを通路を縦線でつなぐ
///

private void RoomVerticalPassageSet(Random random, SD.Rectangle[] roomArray, SD.Rectangle[] areaArray, int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = roomArray[roomNum0];
var room1 = roomArray[roomNum1];

var area0 = areaArray[roomNum0];
var area1 = areaArray[roomNum1];

var exit0 = random.Next(room0.X, room0.Right);
var exit1 = random.Next(room1.X, room1.Right);

for (var i = 0; i < areaArray[roomNum0].Bottom - room0.Bottom; i++)
{
map[exit0, room0.Bottom + i] = 0;
}

for (var i = 0; i < room1.Top - areaArray[roomNum1].Top; i++)
{
map[exit1, room1.Top - 1 - i] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[exit1 + i, area0.Height] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[exit0 + i, area1.Height] = 0;
}
}
}
///
/// 部屋2つを通路を横線でつなぐ
///

private void RoomHorizonPassageSet(Random random, SD.Rectangle[] roomArray, SD.Rectangle[] areaArray, int roomNum0, int roomNum1)
{
var map = this.Map;

var room0 = roomArray[roomNum0];
var room1 = roomArray[roomNum1];

var area0 = areaArray[roomNum0];
var area1 = areaArray[roomNum1];

var exit0 = random.Next(room0.Y, room0.Bottom);
var exit1 = random.Next(room1.Y, room1.Bottom);

for (var i = 0; i < area0.Right - room0.Right; i++)
{
map[room0.Right + i, exit0] = 0;
}

for (var i = 0; i < room1.Left - area1.Left; i++)
{
map[room1.Left - 1 - i, exit1] = 0;
}

if (exit0 > exit1)
{
for (var i = 0; i < exit0 - exit1 + 1; i++)
{
map[area1.Width, exit1 + i] = 0;
}
}
else
{
for (var i = 0; i < exit1 - exit0 + 1; i++)
{
map[area0.Width, exit0 + i] = 0;
}
}
}


///
/// マップを描画
///

///
private void MapDraw(DrawingGroup dg)
{
using (DrawingContext dc = dg.Open())
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
const int size = 7;
const int offset = 10;

var brush = default(Brush);

switch (map[i, j])
{
case MapCreator.wall: brush = Brushes.Black; break;
case MapCreator.room: brush = Brushes.Green; break;
}
dc.DrawRectangle(brush, null, new Rect(i * size + offset, j * size + offset, size, size));
}
}
}
}

internal sealed class MapViewer : FrameworkElement
{
private DrawingImage drawing;
internal DrawingImage Drawing
{
private get { return this.drawing; }
set
{
this.drawing = value;
this.InvalidateVisual();
}
}

internal MapViewer()
{

}

protected override void OnRender(DrawingContext drawingContext)
{
if (this.Drawing != null)
{
drawingContext.DrawImage(this.Drawing, this.Drawing.Drawing.Bounds);
}
}
}
}
}

【c#】 ランダムで部屋を生成

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SD = System.Drawing;

namespace Project1
{
internal static class Program
{
[STAThread]
private static void Main()
{
var window = new Window();
var canvas = new Canvas();
var image = new MapCreator.MapViewer();
var map = new MapCreator();

canvas.Children.Add(image);
canvas.Width = 500;
canvas.Height = 500;

window.MouseDown += (sender, e) =>
{
//クリックするとマップ生成
map.MapInitStart(image);
};

window.ResizeMode = ResizeMode.CanMinimize;
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = canvas;
window.ShowDialog();
}
}
///
/// マップ生成用
///

internal sealed class MapCreator
{
///
/// 壁
///

private const int wall = -1;
///
/// 部屋
///

private const int room = 1;

///
/// マップ配列
///

internal readonly int[,] Map = new int[50, 50];

internal MapCreator()
{

}

///
/// マップ生成開始
///

///
internal void MapInitStart(MapViewer image)
{
var dg = new DrawingGroup();

this.MapWallSet();
this.MapAreaSet();
this.MapDraw(dg);

image.Drawing = new DrawingImage(dg);
}

///
/// 一旦、マップの全てを壁にする
///

private void MapWallSet()
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
map[i, j] = MapCreator.wall;
}
}
}
///
/// マップにエリアを指定
///

private void MapAreaSet()
{
//エリアの数
const int areaCount = 4;

var map = this.Map;

var random = new Random();

var areaArray = new SD.Rectangle[areaCount];
var roomArray = new SD.Rectangle[areaCount];

//エリアの大きさを決める
areaArray[0] = new SD.Rectangle(1, 1, 24, 24);
areaArray[1] = new SD.Rectangle(25, 1, 24, 24);
areaArray[2] = new SD.Rectangle(1, 25, 24, 24);
areaArray[3] = new SD.Rectangle(25, 25, 24, 24);

for (var num = 0; num < areaArray.Length; num++)
{
var areaRect = areaArray[num];

//部屋の位置を決める
var roomX = areaRect.X + random.Next(1, 12);
var roomY = areaRect.Y + random.Next(1, 12);
//部屋の大きさを決める
var roomWidth = random.Next(3, 12);
var roomHeight = random.Next(3, 12);

roomArray[num] = new SD.Rectangle(roomX, roomY, roomWidth, roomHeight);

for (var i = 0; i < roomWidth; i++)
{
for (var j = 0; j < roomHeight; j++)
{
map[roomX + i, roomY + j] = MapCreator.room;
}
}
}
}
///
/// マップを描画
///

///
private void MapDraw(DrawingGroup dg)
{
using (DrawingContext dc = dg.Open())
{
var map = this.Map;

for (var i = 0; i < map.GetLength(0); i++)
{
for (var j = 0; j < map.GetLength(1); j++)
{
const int size = 7;
const int offset = 10;

var brush = default(Brush);

switch (map[i, j])
{
case MapCreator.wall: brush = Brushes.Black; break;
case MapCreator.room: brush = Brushes.Green; break;
}
dc.DrawRectangle(brush, null, new Rect(i * size + offset, j * size + offset, size, size));
}
}
}
}


internal sealed class MapViewer : FrameworkElement
{
private DrawingImage drawing;
internal DrawingImage Drawing
{
private get { return this.drawing; }
set
{
this.drawing = value;
this.InvalidateVisual();
}
}

internal MapViewer()
{

}

protected override void OnRender(DrawingContext drawingContext)
{
if (this.Drawing != null)
{
drawingContext.DrawImage(this.Drawing, this.Drawing.Drawing.Bounds);
}
}
}
}
}

魔法少女は再び旅に出る 制作状況20

ステージを作るのは大変ですが、
頑張って全部作ります

次回作を作るなら、もっと簡単に作れるステージエディタを作りたいな

「魔法少女は再び旅に出る」を更新しました

http://www.freem.ne.jp/win/game/8764
「魔法少女は再び旅に出る」を更新しました

更新内容
・ステージを少し追加
・バグ修正

WEB小説「最後の時の夢」の第6話「エイミー、教師になる」を公開しました

タイトル一覧を見る
第6話を見る

WEB小説「最後の時の夢」を更新しました
第6話「エイミー、教師になる」を公開しました

「魔法少女は再び旅に出る」をフリーゲーム夢幻に公開しました

http://freegame.on.arena.ne.jp/action/game_3461.html
「魔法少女は再び旅に出る」をフリーゲーム夢幻に公開しました
プロフィール

みにおん

Author:みにおん
c#でゲームを作る人です
web小説なども書いています

低学歴で馬鹿なのパート従業員
ですが、よろしく

相互リンク・ブロとも募集中です
コメントなどご自由にどうぞ

ホームページを見る
Twitter(個人ページ)
PIXIV(個人ページ)

○自作ゲーム一覧
ふりーむ
フリーゲーム夢幻
100%ふりげストア

・web小説
最後の時の夢

最新記事
最新コメント
月別アーカイブ
カテゴリ
ブロとも一覧
検索フォーム
ブロとも申請フォーム

この人とブロともになる

RSSリンクの表示
リンク
アクセスランキング
QRコード
QR
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。