如何将ASCII矩阵转换为DND地牢瓦片地图?求工具或实现方法
Go语言DND地牢可视化方案
一、可用Go语言库推荐
1. 标准库image
Go自带的image和image/draw包无需额外依赖,适合快速实现基础可视化:
- 定义墙体、通路的颜色(比如墙体用深灰色
color.RGBA{40,40,40,255},通路用米白色color.RGBA{240,230,210,255}) - 根据网格尺寸创建对应大小的图像(比如每个单元格设为30×30像素,6×6网格就是180×180像素)
- 遍历ASCII网格,逐个绘制矩形填充对应颜色
示例代码:
package main import ( "image" "image/color" "image/png" "os" ) func main() { dungeon := []string{ "000001", "000111", "000111", "001101", "011101", "011111", } cellSize := 30 imgWidth := len(dungeon[0]) * cellSize imgHeight := len(dungeon) * cellSize img := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight)) wallColor := color.RGBA{40, 40, 40, 255} floorColor := color.RGBA{240, 230, 210, 255} for y, row := range dungeon { for x, c := range row { x1 := x * cellSize y1 := y * cellSize x2 := x1 + cellSize y2 := y1 + cellSize fillColor := wallColor if c == '1' { fillColor = floorColor } for dx := x1; dx < x2; dx++ { for dy := y1; dy < y2; dy++ { img.Set(dx, dy, fillColor) } } } } f, _ := os.Create("dungeon.png") defer f.Close() png.Encode(f, img) }
2. gg图形库
gg是轻量2D图形库,语法简洁,支持绘制边框、文字、纹理,适合给DND地牢添加细节(比如房间编号、标记点),安装命令:go get github.com/fogleman/gg
- 可轻松绘制带边框的网格单元,给通路添加简单纹理模拟地板
- 支持输出PNG/JPG或PDF(适合打印给玩家)
示例简化逻辑:
package main import "github.com/fogleman/gg" func main() { dungeon := []string{ "000001", "000111", "000111", "001101", "011101", "011111", } cellSize := 30 w := len(dungeon[0]) * cellSize h := len(dungeon) * cellSize dc := gg.NewContext(w, h) wallColor := gg.NewRGBA(0.15, 0.15, 0.15, 1) floorColor := gg.NewRGBA(0.94, 0.90, 0.82, 1) borderColor := gg.NewRGBA(0, 0, 0, 1) for y, row := range dungeon { for x, c := range row { xPos := float64(x * cellSize) yPos := float64(y * cellSize) dc.SetColor(wallColor) if c == '1' { dc.SetColor(floorColor) } dc.DrawRectangle(xPos, yPos, float64(cellSize), float64(cellSize)) dc.Fill() // 绘制网格边框 dc.SetColor(borderColor) dc.DrawRectangle(xPos, yPos, float64(cellSize), float64(cellSize)) dc.Stroke() } } dc.SavePNG("dungeon_gg.png") }
二、无第三方库手动实现
如果不想引入外部依赖,除了标准image包,还可以生成SVG格式文件:
- SVG是文本格式,直接拼接字符串即可生成
- 用矩形元素对应每个网格单元,设置填充颜色
- 生成的SVG可直接用浏览器打开,或导出为图片打印
示例代码:
package main import ( "os" "strings" ) func main() { dungeon := []string{ "000001", "000111", "000111", "001101", "011101", "011111", } cellSize := 30 width := len(dungeon[0]) * cellSize height := len(dungeon) * cellSize var svg strings.Builder svg.WriteString(`<svg width="` + string(rune(width)) + `" height="` + string(rune(height)) + `" xmlns="http://www.w3.org/2000/svg">`) for y, row := range dungeon { for x, c := range row { x1 := x * cellSize y1 := y * cellSize fill := "#282828" if c == '1' { fill = "#f0e6d2" } svg.WriteString(`<rect x="` + string(rune(x1)) + `" y="` + string(rune(y1)) + `" width="` + string(rune(cellSize)) + `" height="` + string(rune(cellSize)) + `" fill="` + fill + `" stroke="#000000"/>`) } } svg.WriteString(`</svg>`) os.WriteFile("dungeon.svg", []byte(svg.String()), 0644) }
三、DND场景优化建议
- 用不同颜色区分房间、走廊、宝藏点等功能区域
- 添加清晰的网格线或边框,方便玩家在纸质版上标记角色位置
- 生成PDF格式文件,直接打印供桌面游戏使用
内容的提问来源于stack exchange,提问作者Tallys Assis
相关产品推荐
相关产品推荐

