generated at
EbitenでLifeGame
Ebiten公式サンプルのGame of Life を参考にして、パラメータを少しいじって実験
GameのDraw関数で、screen.ReplacePixelsに新しい状態のbyte配列を渡して描画し直している
go
func (g *Game) Draw(screen *ebiten.Image) { if g.pixels == nil { g.pixels = make([]byte, screenWidth*screenHeight*4) } g.world.Draw(g.pixels) screen.ReplacePixels(g.pixels) }
Life の true/false のルールを一部変更。50%の確率で 近接4つがtrueの時に 自身も true で継続するようにしたら、時間経過でどんどん trueの白い点が増えていく
go
func (w *World) Update() { width := w.width height := w.height next := make([]bool, width*height) for y := 0; y < height; y++ { for x := 0; x < width; x++ { pop := neigborCount(w.area, width, height, x, y) switch { case pop < 2: next[y*width+x] = false case (pop == 2 || pop == 3) && w.area[y*width+x]: next[y*width+x] = true case pop > 3: if rand.Intn(50) == 0 { next[y*width+x] = true } else { next[y*width+x] = false } case pop == 3: next[y*width+x] = true } } } w.area = next }
実行結果