man I love netpbm what a good file format. so easy to just output pixel data.
https://en.wikipedia.org/wiki/Netpbm
convert -delay 0 -loop 0 map_*.ppm -filter point -resize 150% animation.gif
func write(grid [][]byte, count *int) {
*count++
m, n := len(grid), len(grid[0])
data := []byte{}
height, width := strconv.Itoa(m), strconv.Itoa(n)
data = append(data, "P3\n"...)
data = append(data, width+" "+height+"\n"...)
data = append(data, "255\n"...)
for y := 0; y < m; y++ {
for x := 0; x < n; x++ {
color := ""
switch grid[y][x] {
case '0': // water
color = "25 166 188"
break
case '1': // water
color = "21 144 162"
break
case '#': // land
color = "21 162 31"
break
case '=': // sand
color = "188 167 34"
break
case '@': // tree
color = "85 162 29"
break
}
data = append(data, color+"\n"...)
}
}
num := strconv.Itoa(*count)
if len(num) == 1 {
num = "00" + num
}
if len(num) == 2 {
num = "0" + num
}
err := os.WriteFile("map_"+num+".ppm", data, 0644)
if err != nil {
panic("couldn't write file")
}
}