diff --git a/community/game_of_life/README.md b/community/game_of_life/README.md new file mode 100644 index 0000000..92d1aad --- /dev/null +++ b/community/game_of_life/README.md @@ -0,0 +1,20 @@ +# Conway's Game of Lfe + +Created by [Nathan Leniz](https://github.com/terakilobyte). +Inspired by and heavily uses [the doc](https://golang.org/doc/play/life.go) + +> The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves, or, for advanced "players", by creating patterns with particular properties. The Game has been reprogrammed multiple times in various coding languages. + +For more information, please see the [wikipedia](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) article. + +## Use + + go run main.go -h + -frameRate duration + The framerate in milliseconds (default 33ms) + -size int + The size of each cell (default 5) + -windowSize float + The pixel size of one side of the grid (default 800) + +![life](life.png) diff --git a/community/game_of_life/life.png b/community/game_of_life/life.png new file mode 100644 index 0000000..5b8807a Binary files /dev/null and b/community/game_of_life/life.png differ diff --git a/community/game_of_life/life/grid.go b/community/game_of_life/life/grid.go index ea59cc8..e7b0a14 100644 --- a/community/game_of_life/life/grid.go +++ b/community/game_of_life/life/grid.go @@ -41,15 +41,15 @@ func (g *Grid) Set(x, y int, state bool) { func (g *Grid) Draw(imd *imdraw.IMDraw) { for i := 0; i < g.h; i++ { for j := 0; j < g.h; j++ { - imd.Push( - pixel.V(float64(i*g.cellSize), float64(j*g.cellSize)), - pixel.V(float64(i*g.cellSize+g.cellSize), float64(j*g.cellSize+g.cellSize)), - ) if g.Alive(i, j) { imd.Color = colornames.Black } else { imd.Color = colornames.White } + imd.Push( + pixel.V(float64(i*g.cellSize), float64(j*g.cellSize)), + pixel.V(float64(i*g.cellSize+g.cellSize), float64(j*g.cellSize+g.cellSize)), + ) imd.Rectangle(0) } } diff --git a/community/game_of_life/life/life.go b/community/game_of_life/life/life.go index 2ee7fca..26d2823 100644 --- a/community/game_of_life/life/life.go +++ b/community/game_of_life/life/life.go @@ -1,4 +1,4 @@ -// Package life is the manages the "game" state +// Package life manages the "game" state // Shamelessly taken from https://golang.org/doc/play/life.go package life diff --git a/community/game_of_life/main.go b/community/game_of_life/main.go index 11b02d6..26f5ea0 100644 --- a/community/game_of_life/main.go +++ b/community/game_of_life/main.go @@ -16,14 +16,14 @@ import ( var ( size *int windowSize *float64 - frameRate *int64 + frameRate *time.Duration ) func init() { rand.Seed(time.Now().UnixNano()) size = flag.Int("size", 5, "The size of each cell") windowSize = flag.Float64("windowSize", 800, "The pixel size of one side of the grid") - frameRate = flag.Int64("frameRate", 33, "The framerate in milliseconds") + frameRate = flag.Duration("frameRate", 33*time.Millisecond, "The framerate in milliseconds") flag.Parse() } @@ -49,15 +49,15 @@ func run() { gridDraw := imdraw.New(nil) game := life.NewLife(rows, *size) - last := time.Now() + tick := time.Tick(*frameRate) for !win.Closed() { // game loop - if time.Since(last).Nanoseconds() > *frameRate { + select { + case <-tick: gridDraw.Clear() game.A.Draw(gridDraw) gridDraw.Draw(win) game.Step() - last = time.Now() } win.Update() }