You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.1 KiB

package main
import (
"image"
"image/color"
"image/gif"
"math/rand"
"os"
"time"
)
const (
gridX = 50
gridY = 25
cellSize = 10
)
var palette = []color.Color{
color.Black,
color.RGBA{0x00, 0xb3, 0x68, 0xff},
color.RGBA{0xff, 0x57, 0x92, 0xff},
color.RGBA{0x00, 0xbd, 0xd6, 0xff},
color.RGBA{0x00, 0x94, 0x56, 0xff},
color.RGBA{0x58, 0x42, 0xff, 0xff},
color.RGBA{0x00, 0x95, 0xa8, 0xff},
color.RGBA{0xe6, 0x41, 0x00, 0xff},
color.RGBA{0xb3, 0x69, 0x4d, 0xff},
}
func vert(img *image.Paletted, x int, y int, length int, colorIdx uint8) {
for i := 0; i < length; i++ {
img.SetColorIndex(x, y+i, colorIdx)
}
}
func horz(img *image.Paletted, x int, y int, length int, colorIdx uint8) {
for i := 0; i < length; i++ {
img.SetColorIndex(x+i, y, colorIdx)
}
}
func drawCell(img *image.Paletted, colorIdx uint8, x int, y int, top bool, right bool, bottom bool, left bool) {
imgX := x * cellSize
imgY := y * cellSize
if top {
horz(img, imgX, imgY, cellSize, colorIdx)
}
if bottom {
horz(img, imgX, imgY+cellSize-1, cellSize, colorIdx)
}
if left {
vert(img, imgX, imgY, cellSize, colorIdx)
}
if right {
vert(img, imgX+cellSize-1, imgY, cellSize, colorIdx)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
anim := gif.GIF{LoopCount: 16}
for frame := 0; frame < 16; frame++ {
img := image.NewPaletted(
image.Rect(0, 0, cellSize*gridX, cellSize*gridY),
palette,
)
for x := 0; x < gridX; x++ {
for y := 0; y < gridY; y++ {
left := false
bottom := false
//main selection algorithm
topOrRight := rand.Intn(2)
top := topOrRight == 0
right := topOrRight != 0
// handle edge cases
if y == 0 {
top = true
right = false
}
if x == gridX-1 {
top = y == 0 // if y is 0, we want a top, otherwise no top
right = true
}
// handle other borders
if x == 0 {
left = true
}
if y == gridY-1 {
bottom = true
}
drawCell(img, uint8(frame%8+1), x, y, top, right, bottom, left)
}
}
anim.Delay = append(anim.Delay, 100)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(os.Stdout, &anim)
}