examples: Add comments

This commit is contained in:
Hajime Hoshi 2018-01-28 22:39:54 +09:00
parent 825c406128
commit bba20e8b8c
4 changed files with 41 additions and 27 deletions

View File

@ -33,7 +33,7 @@ var (
) )
func init() { func init() {
// licensed under Public Domain // Licensed under Public Domain
// https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg // https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg
const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg" const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg"

View File

@ -32,15 +32,17 @@ const (
) )
var ( var (
hueInt = 0 hue128 = 0
saturationInt = 128 saturation128 = 128
valueInt = 128 value128 = 128
inverted = false inverted = false
prevPressedI = false prevPressedI = false
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
// clamp clamps v to the range [min, max].
func clamp(v, min, max int) int { func clamp(v, min, max int) int {
if min > max { if min > max {
panic("min must <= max") panic("min must <= max")
@ -55,25 +57,30 @@ func clamp(v, min, max int) int {
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
// Adjust HSV values along with the user's input.
if ebiten.IsKeyPressed(ebiten.KeyQ) { if ebiten.IsKeyPressed(ebiten.KeyQ) {
hueInt-- hue128--
} }
if ebiten.IsKeyPressed(ebiten.KeyW) { if ebiten.IsKeyPressed(ebiten.KeyW) {
hueInt++ hue128++
} }
if ebiten.IsKeyPressed(ebiten.KeyA) { if ebiten.IsKeyPressed(ebiten.KeyA) {
saturationInt-- saturation128--
} }
if ebiten.IsKeyPressed(ebiten.KeyS) { if ebiten.IsKeyPressed(ebiten.KeyS) {
saturationInt++ saturation128++
} }
if ebiten.IsKeyPressed(ebiten.KeyZ) { if ebiten.IsKeyPressed(ebiten.KeyZ) {
valueInt-- value128--
} }
if ebiten.IsKeyPressed(ebiten.KeyX) { if ebiten.IsKeyPressed(ebiten.KeyX) {
valueInt++ value128++
} }
hue128 = clamp(hue128, -256, 256)
saturation128 = clamp(saturation128, 0, 256)
value128 = clamp(value128, 0, 256)
pressedI := ebiten.IsKeyPressed(ebiten.KeyI) pressedI := ebiten.IsKeyPressed(ebiten.KeyI)
if pressedI && !prevPressedI { if pressedI && !prevPressedI {
inverted = !inverted inverted = !inverted
@ -83,18 +90,19 @@ func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
hueInt = clamp(hueInt, -256, 256)
saturationInt = clamp(saturationInt, 0, 256)
valueInt = clamp(valueInt, 0, 256)
// Center the image on the screen.
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
hue := float64(hueInt) * 2 * math.Pi / 128 // Change HSV.
saturation := float64(saturationInt) / 128 hue := float64(hue128) * 2 * math.Pi / 128
value := float64(valueInt) / 128 saturation := float64(saturation128) / 128
value := float64(value128) / 128
op.ColorM.ChangeHSV(hue, saturation, value) op.ColorM.ChangeHSV(hue, saturation, value)
// Invert the color.
if inverted { if inverted {
op.ColorM.Scale(-1, -1, -1, 1) op.ColorM.Scale(-1, -1, -1, 1)
op.ColorM.Translate(1, 1, 1, 0) op.ColorM.Translate(1, 1, 1, 0)
@ -102,6 +110,7 @@ func update(screen *ebiten.Image) error {
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)
// Draw the text of the current status.
msgInverted := "false" msgInverted := "false"
if inverted { if inverted {
msgInverted = "true" msgInverted = "true"

View File

@ -37,13 +37,19 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
count++ count++
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
// Center the image on the screen.
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
// Rotate the hue.
op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360) op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360)
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)
return nil return nil
} }

View File

@ -33,13 +33,13 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
// World represents the game state // World represents the game state.
type World struct { type World struct {
area [][]bool area [][]bool
rnd *rand.Rand 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{
area: makeArea(width, height), area: makeArea(width, height),
@ -48,7 +48,7 @@ func NewWorld(width, height int) *World {
return &world return &world
} }
// RandomSeed inits world with a random state // RandomSeed inits world with a random state.
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])
@ -59,14 +59,13 @@ func (w *World) RandomSeed(limit int) {
} }
} }
// Progress game state by one tick // Update game state by one tick.
func (w *World) Progress() { func (w *World) Update() {
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++ {
pop := neighbourCount(w.area, x, y) pop := neighbourCount(w.area, x, y)
switch { switch {
case pop < 2: case pop < 2:
@ -95,7 +94,7 @@ func (w *World) Progress() {
} }
// DrawImage paints current game state // DrawImage paints current game state
func (w *World) DrawImage(pix []uint8) { func (w *World) DrawImage(pix []byte) {
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++ {
@ -116,7 +115,7 @@ func (w *World) DrawImage(pix []uint8) {
} }
} }
// neighbourCount calculates the Moore neighborhood of x, y // neighbourCount calculates the Moore neighborhood of (x, y)
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])
@ -163,11 +162,11 @@ const (
var ( var (
world = NewWorld(screenWidth, screenHeight) world = NewWorld(screenWidth, screenHeight)
pixels = make([]uint8, screenWidth*screenHeight*4) pixels = make([]byte, screenWidth*screenHeight*4)
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
world.Progress() world.Update()
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }