mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 20:20:57 +01:00
fixed game speed, now using ticks per generation, not TPS
This commit is contained in:
81
main.go
81
main.go
@@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "v0.0.1"
|
VERSION = "v0.0.4"
|
||||||
Alive = 1
|
Alive = 1
|
||||||
Dead = 0
|
Dead = 0
|
||||||
)
|
)
|
||||||
@@ -33,17 +33,18 @@ type Images struct {
|
|||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
Grids []*Grid // 2 grids: one current, one next
|
Grids []*Grid // 2 grids: one current, one next
|
||||||
History *Grid
|
History *Grid // holds state of past dead cells for evolution tracks
|
||||||
Index int // points to current grid
|
Index int // points to current grid
|
||||||
Width, Height, Cellsize, Density int
|
Width, Height, Cellsize, Density int // measurements
|
||||||
ScreenWidth, ScreenHeight int
|
ScreenWidth, ScreenHeight int
|
||||||
Generations int
|
Generations int // Stats
|
||||||
Black, White, Grey, Beige color.RGBA
|
Black, White, Grey, Beige color.RGBA
|
||||||
Speed int
|
TPG int // ticks per generation/game speed, 1==max
|
||||||
Debug, Paused, Empty, Invert bool
|
TicksElapsed int // tick counter for game speed
|
||||||
ShowEvolution, NoGrid, RunOneStep bool
|
Debug, Paused, Empty, Invert bool // game modi
|
||||||
Rule *Rule
|
ShowEvolution, NoGrid, RunOneStep bool // flags
|
||||||
Tiles Images
|
Rule *Rule // which rule to use, default: B3/S23
|
||||||
|
Tiles Images // pre-computed tiles for dead and alife cells
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
@@ -81,10 +82,20 @@ func Contains[E comparable](s []E, v E) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update all cells according to the current rule
|
||||||
func (game *Game) UpdateCells() {
|
func (game *Game) UpdateCells() {
|
||||||
// compute cells
|
// count ticks so we know when to actually run
|
||||||
next := game.Index ^ 1 // next grid index, we just xor 0|1 to 1|0
|
game.TicksElapsed++
|
||||||
|
|
||||||
|
if game.TPG > game.TicksElapsed {
|
||||||
|
// need to sleep a little more
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// next grid index, we just xor 0|1 to 1|0
|
||||||
|
next := game.Index ^ 1
|
||||||
|
|
||||||
|
// compute life status of cells
|
||||||
for y := 0; y < game.Height; y++ {
|
for y := 0; y < game.Height; y++ {
|
||||||
for x := 0; x < game.Width; x++ {
|
for x := 0; x < game.Width; x++ {
|
||||||
state := game.Grids[game.Index].Data[y][x] // 0|1 == dead or alive
|
state := game.Grids[game.Index].Data[y][x] // 0|1 == dead or alive
|
||||||
@@ -105,12 +116,16 @@ func (game *Game) UpdateCells() {
|
|||||||
// switch grid for rendering
|
// switch grid for rendering
|
||||||
game.Index ^= 1
|
game.Index ^= 1
|
||||||
|
|
||||||
// global counter
|
// global stats counter
|
||||||
game.Generations++
|
game.Generations++
|
||||||
|
|
||||||
if game.RunOneStep {
|
if game.RunOneStep {
|
||||||
|
// setp-wise mode, halt the game
|
||||||
game.RunOneStep = false
|
game.RunOneStep = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset speed counter
|
||||||
|
game.TicksElapsed = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// a GOL rule
|
// a GOL rule
|
||||||
@@ -178,34 +193,29 @@ func (game *Game) CheckInput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
|
||||||
if game.Speed > 1 {
|
if game.TPG < 120 {
|
||||||
game.Speed--
|
game.TPG++
|
||||||
ebiten.SetTPS(game.Speed)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
|
||||||
if game.Speed < 120 {
|
if game.TPG > 1 {
|
||||||
game.Speed++
|
game.TPG--
|
||||||
ebiten.SetTPS(game.Speed)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageDown) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyPageDown) {
|
||||||
switch {
|
if game.TPG <= 115 {
|
||||||
case game.Speed > 5:
|
game.TPG += 5
|
||||||
game.Speed -= 5
|
|
||||||
case game.Speed <= 5:
|
|
||||||
game.Speed = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ebiten.SetTPS(game.Speed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageUp) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyPageUp) {
|
||||||
if game.Speed <= 115 {
|
switch {
|
||||||
game.Speed += 5
|
case game.TPG > 5:
|
||||||
ebiten.SetTPS(game.Speed)
|
game.TPG -= 5
|
||||||
|
case game.TPG <= 5:
|
||||||
|
game.TPG = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,8 +287,8 @@ func (game *Game) Draw(screen *ebiten.Image) {
|
|||||||
|
|
||||||
ebitenutil.DebugPrint(
|
ebitenutil.DebugPrint(
|
||||||
screen,
|
screen,
|
||||||
fmt.Sprintf("FPS: %d, Generations: %d %s",
|
fmt.Sprintf("FPS: %d, TPG: %d, Generations: %d %s",
|
||||||
game.Speed, game.Generations, paused),
|
game.Speed, game.TPG, game.Generations, paused),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -355,6 +365,7 @@ func (game *Game) Init() {
|
|||||||
game.InitTiles()
|
game.InitTiles()
|
||||||
|
|
||||||
game.Index = 0
|
game.Index = 0
|
||||||
|
game.TicksElapsed = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// count the living neighbors of a cell
|
// count the living neighbors of a cell
|
||||||
@@ -386,9 +397,11 @@ func main() {
|
|||||||
pflag.IntVarP(&game.Width, "width", "W", 40, "grid width in cells")
|
pflag.IntVarP(&game.Width, "width", "W", 40, "grid width in cells")
|
||||||
pflag.IntVarP(&game.Height, "height", "H", 40, "grid height in cells")
|
pflag.IntVarP(&game.Height, "height", "H", 40, "grid height in cells")
|
||||||
pflag.IntVarP(&game.Cellsize, "cellsize", "c", 8, "cell size in pixels")
|
pflag.IntVarP(&game.Cellsize, "cellsize", "c", 8, "cell size in pixels")
|
||||||
pflag.IntVarP(&game.Speed, "tps", "t", 60, "game speed in ticks per second")
|
|
||||||
pflag.IntVarP(&game.Density, "density", "D", 10, "density of random cells")
|
pflag.IntVarP(&game.Density, "density", "D", 10, "density of random cells")
|
||||||
|
pflag.IntVarP(&game.TPG, "ticks-per-generation", "t", 1, "game speed: the higher the slower (default: 1)")
|
||||||
|
|
||||||
pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule")
|
pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule")
|
||||||
|
|
||||||
pflag.BoolVarP(&showversion, "version", "v", false, "show version")
|
pflag.BoolVarP(&showversion, "version", "v", false, "show version")
|
||||||
pflag.BoolVarP(&game.Paused, "paused", "p", false, "do not start simulation (use space to start)")
|
pflag.BoolVarP(&game.Paused, "paused", "p", false, "do not start simulation (use space to start)")
|
||||||
pflag.BoolVarP(&game.Debug, "debug", "d", false, "show debug info")
|
pflag.BoolVarP(&game.Debug, "debug", "d", false, "show debug info")
|
||||||
@@ -406,14 +419,12 @@ func main() {
|
|||||||
|
|
||||||
game.Rule = ParseGameRule(rule)
|
game.Rule = ParseGameRule(rule)
|
||||||
|
|
||||||
repr.Print(game.Rule.Birth)
|
repr.Print(game.TPG)
|
||||||
repr.Print(game.Rule.Death)
|
|
||||||
game.Init()
|
game.Init()
|
||||||
|
|
||||||
ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight)
|
ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight)
|
||||||
ebiten.SetWindowTitle("Game of life")
|
ebiten.SetWindowTitle("Game of life")
|
||||||
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||||
ebiten.SetTPS(game.Speed)
|
|
||||||
|
|
||||||
if err := ebiten.RunGame(game); err != nil {
|
if err := ebiten.RunGame(game); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user