mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
examples/life: Refactoring
This commit is contained in:
parent
035e4d321a
commit
665e7d5eea
@ -26,7 +26,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@ -34,20 +33,18 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
randSource = rand.NewSource(time.Now().UnixNano())
|
|
||||||
rnd = rand.New(randSource)
|
|
||||||
)
|
|
||||||
|
|
||||||
// World represents the game state
|
// World represents the game state
|
||||||
type World struct {
|
type World struct {
|
||||||
area [][]bool
|
area [][]bool
|
||||||
|
rnd *rand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWorld creates a new world
|
// NewWorld creates a new world
|
||||||
func NewWorld(width, height int) *World {
|
func NewWorld(width, height int) *World {
|
||||||
world := World{}
|
world := World{
|
||||||
world.area = makeArea(width, height)
|
area: makeArea(width, height),
|
||||||
|
rnd: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||||
|
}
|
||||||
return &world
|
return &world
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,10 +52,9 @@ func NewWorld(width, height int) *World {
|
|||||||
func (w *World) RandomSeed(limit int) {
|
func (w *World) RandomSeed(limit int) {
|
||||||
height := len(w.area)
|
height := len(w.area)
|
||||||
width := len(w.area[0])
|
width := len(w.area[0])
|
||||||
|
|
||||||
for i := 0; i < limit; i++ {
|
for i := 0; i < limit; i++ {
|
||||||
x := rnd.Intn(width)
|
x := w.rnd.Intn(width)
|
||||||
y := rnd.Intn(height)
|
y := w.rnd.Intn(height)
|
||||||
w.area[y][x] = true
|
w.area[y][x] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,9 +63,7 @@ func (w *World) RandomSeed(limit int) {
|
|||||||
func (w *World) Progress() {
|
func (w *World) Progress() {
|
||||||
height := len(w.area)
|
height := len(w.area)
|
||||||
width := len(w.area[0])
|
width := len(w.area[0])
|
||||||
|
|
||||||
next := makeArea(width, height)
|
next := makeArea(width, height)
|
||||||
|
|
||||||
for y := 0; y < height; y++ {
|
for y := 0; y < height; y++ {
|
||||||
for x := 0; x < width; x++ {
|
for x := 0; x < width; x++ {
|
||||||
|
|
||||||
@ -101,23 +95,22 @@ func (w *World) Progress() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DrawImage paints current game state
|
// DrawImage paints current game state
|
||||||
func (w *World) DrawImage(img *image.RGBA) {
|
func (w *World) DrawImage(pix []uint8) {
|
||||||
height := len(w.area)
|
height := len(w.area)
|
||||||
width := len(w.area[0])
|
width := len(w.area[0])
|
||||||
|
|
||||||
for y := 0; y < height; y++ {
|
for y := 0; y < height; y++ {
|
||||||
for x := 0; x < width; x++ {
|
for x := 0; x < width; x++ {
|
||||||
pos := 4*y*width + 4*x
|
pos := 4*y*width + 4*x
|
||||||
if w.area[y][x] {
|
if w.area[y][x] {
|
||||||
img.Pix[pos] = 0xff
|
pix[pos] = 0xff
|
||||||
img.Pix[pos+1] = 0xff
|
pix[pos+1] = 0xff
|
||||||
img.Pix[pos+2] = 0xff
|
pix[pos+2] = 0xff
|
||||||
img.Pix[pos+3] = 0xff
|
pix[pos+3] = 0xff
|
||||||
} else {
|
} else {
|
||||||
img.Pix[pos] = 0
|
pix[pos] = 0
|
||||||
img.Pix[pos+1] = 0
|
pix[pos+1] = 0
|
||||||
img.Pix[pos+2] = 0
|
pix[pos+2] = 0
|
||||||
img.Pix[pos+3] = 0
|
pix[pos+3] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,27 +120,22 @@ func (w *World) DrawImage(img *image.RGBA) {
|
|||||||
func neighbourCount(a [][]bool, x, y int) int {
|
func neighbourCount(a [][]bool, x, y int) int {
|
||||||
height := len(a)
|
height := len(a)
|
||||||
width := len(a[0])
|
width := len(a[0])
|
||||||
|
|
||||||
lowX := 0
|
lowX := 0
|
||||||
if x > 0 {
|
if x > 0 {
|
||||||
lowX = x - 1
|
lowX = x - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
lowY := 0
|
lowY := 0
|
||||||
if y > 0 {
|
if y > 0 {
|
||||||
lowY = y - 1
|
lowY = y - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
highX := width - 1
|
highX := width - 1
|
||||||
if x < width-1 {
|
if x < width-1 {
|
||||||
highX = x + 1
|
highX = x + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
highY := height - 1
|
highY := height - 1
|
||||||
if y < height-1 {
|
if y < height-1 {
|
||||||
highY = y + 1
|
highY = y + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
near := 0
|
near := 0
|
||||||
for pY := lowY; pY <= highY; pY++ {
|
for pY := lowY; pY <= highY; pY++ {
|
||||||
for pX := lowX; pX <= highX; pX++ {
|
for pX := lowX; pX <= highX; pX++ {
|
||||||
@ -174,8 +162,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
world *World
|
world = NewWorld(screenWidth, screenHeight)
|
||||||
noiseImage *image.RGBA
|
pixels = make([]uint8, screenWidth*screenHeight*4)
|
||||||
)
|
)
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
@ -183,20 +171,14 @@ func update(screen *ebiten.Image) error {
|
|||||||
if ebiten.IsRunningSlowly() {
|
if ebiten.IsRunningSlowly() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
world.DrawImage(noiseImage)
|
world.DrawImage(pixels)
|
||||||
screen.ReplacePixels(noiseImage.Pix)
|
screen.ReplacePixels(pixels)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
population := int((screenWidth * screenHeight) / 10)
|
world.RandomSeed(int((screenWidth * screenHeight) / 10))
|
||||||
scale := 2.0
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2.0, "Game of Life (Ebiten Demo)"); err != nil {
|
||||||
|
|
||||||
world = NewWorld(screenWidth, screenHeight)
|
|
||||||
world.RandomSeed(population)
|
|
||||||
|
|
||||||
noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight))
|
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, scale, "Game of Life (Ebiten Demo)"); err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user