94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"image/color"
|
|
"time"
|
|
|
|
"github.com/acifani/vita/lib/game"
|
|
)
|
|
|
|
const (
|
|
population = 20
|
|
cellSize int16 = 6
|
|
gameWidth uint32 = uint32(displayWidth / cellSize)
|
|
gameHeight uint32 = uint32(displayHeight / cellSize)
|
|
)
|
|
|
|
var (
|
|
gamebuffer []byte
|
|
|
|
universe *game.Universe
|
|
|
|
cellBuf = []color.RGBA{
|
|
colorWhite, colorWhite, colorWhite, colorWhite, colorWhite, colorWhite,
|
|
colorWhite, colorWhite, colorBlack, colorBlack, colorWhite, colorWhite,
|
|
colorWhite, colorBlack, colorBlack, colorBlack, colorBlack, colorWhite,
|
|
colorWhite, colorBlack, colorBlack, colorBlack, colorBlack, colorWhite,
|
|
colorWhite, colorWhite, colorBlack, colorBlack, colorWhite, colorWhite,
|
|
colorWhite, colorWhite, colorWhite, colorWhite, colorWhite, colorWhite,
|
|
}
|
|
)
|
|
|
|
func GameOfLife() {
|
|
display.FillScreen(colorWhite)
|
|
|
|
gamebuffer = make([]byte, gameHeight*gameWidth)
|
|
universe = game.NewUniverse(gameHeight, gameWidth)
|
|
universe.Randomize(population)
|
|
universe.Read(gamebuffer)
|
|
|
|
x, y, z := accel.ReadRawAcceleration()
|
|
speed := 10
|
|
|
|
for {
|
|
drawGrid()
|
|
display.Display()
|
|
universe.Read(gamebuffer)
|
|
|
|
universe.Tick()
|
|
|
|
getInput()
|
|
|
|
x, y, z = accel.ReadRawAcceleration()
|
|
if x < (-31000) || x > 31000 || y < (-31000) || y > 31000 || z < (-31000) || z > 31000 ||
|
|
(!buttonsOldState[buttonA] && buttonsState[buttonA]) {
|
|
universe.Reset()
|
|
universe.Randomize(population)
|
|
}
|
|
|
|
if buttonsState[buttonUp] && speed > 5 {
|
|
speed -= 5
|
|
}
|
|
|
|
if buttonsState[buttonDown] && speed < 250 {
|
|
speed += 5
|
|
}
|
|
|
|
if goBack() {
|
|
break
|
|
}
|
|
|
|
time.Sleep(time.Duration(speed) * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func drawGrid() {
|
|
var rows, cols uint32
|
|
|
|
for rows = 0; rows < gameHeight; rows++ {
|
|
for cols = 0; cols < gameWidth; cols++ {
|
|
idx := universe.GetIndex(rows, cols)
|
|
|
|
switch {
|
|
case universe.Cell(idx) == gamebuffer[idx]:
|
|
// no change, so skip
|
|
continue
|
|
case universe.Cell(idx) == game.Alive:
|
|
display.FillRectangleWithBuffer(1+cellSize*int16(cols), cellSize*int16(rows), cellSize, cellSize, cellBuf)
|
|
default: // game.Dead
|
|
display.FillRectangle(1+cellSize*int16(cols), cellSize*int16(rows), cellSize, cellSize, colorWhite)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|