addressing PR feedback

This commit is contained in:
Nathan 2018-01-18 20:44:54 -05:00
parent 17c9db7140
commit 6178d3ef7c
5 changed files with 30 additions and 10 deletions

View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

View File

@ -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)
}
}

View File

@ -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

View File

@ -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()
}