mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
examples: Add comments
This commit is contained in:
parent
825c406128
commit
bba20e8b8c
@ -33,7 +33,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
// licensed under Public Domain
|
||||
// Licensed under Public Domain
|
||||
// https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg
|
||||
const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg"
|
||||
|
||||
|
@ -32,15 +32,17 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
hueInt = 0
|
||||
saturationInt = 128
|
||||
valueInt = 128
|
||||
hue128 = 0
|
||||
saturation128 = 128
|
||||
value128 = 128
|
||||
|
||||
inverted = false
|
||||
|
||||
prevPressedI = false
|
||||
gophersImage *ebiten.Image
|
||||
)
|
||||
|
||||
// clamp clamps v to the range [min, max].
|
||||
func clamp(v, min, max int) int {
|
||||
if min > max {
|
||||
panic("min must <= max")
|
||||
@ -55,25 +57,30 @@ func clamp(v, min, max int) int {
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
// Adjust HSV values along with the user's input.
|
||||
if ebiten.IsKeyPressed(ebiten.KeyQ) {
|
||||
hueInt--
|
||||
hue128--
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyW) {
|
||||
hueInt++
|
||||
hue128++
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyA) {
|
||||
saturationInt--
|
||||
saturation128--
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyS) {
|
||||
saturationInt++
|
||||
saturation128++
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
||||
valueInt--
|
||||
value128--
|
||||
}
|
||||
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)
|
||||
if pressedI && !prevPressedI {
|
||||
inverted = !inverted
|
||||
@ -83,18 +90,19 @@ func update(screen *ebiten.Image) error {
|
||||
if ebiten.IsRunningSlowly() {
|
||||
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()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
|
||||
|
||||
hue := float64(hueInt) * 2 * math.Pi / 128
|
||||
saturation := float64(saturationInt) / 128
|
||||
value := float64(valueInt) / 128
|
||||
// Change HSV.
|
||||
hue := float64(hue128) * 2 * math.Pi / 128
|
||||
saturation := float64(saturation128) / 128
|
||||
value := float64(value128) / 128
|
||||
op.ColorM.ChangeHSV(hue, saturation, value)
|
||||
|
||||
// Invert the color.
|
||||
if inverted {
|
||||
op.ColorM.Scale(-1, -1, -1, 1)
|
||||
op.ColorM.Translate(1, 1, 1, 0)
|
||||
@ -102,6 +110,7 @@ func update(screen *ebiten.Image) error {
|
||||
|
||||
screen.DrawImage(gophersImage, op)
|
||||
|
||||
// Draw the text of the current status.
|
||||
msgInverted := "false"
|
||||
if inverted {
|
||||
msgInverted = "true"
|
||||
|
@ -37,13 +37,19 @@ var (
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
count++
|
||||
|
||||
if ebiten.IsRunningSlowly() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Center the image on the screen.
|
||||
w, h := gophersImage.Size()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
|
||||
|
||||
// Rotate the hue.
|
||||
op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360)
|
||||
|
||||
screen.DrawImage(gophersImage, op)
|
||||
return nil
|
||||
}
|
||||
|
@ -33,13 +33,13 @@ import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
// World represents the game state
|
||||
// World represents the game state.
|
||||
type World struct {
|
||||
area [][]bool
|
||||
rnd *rand.Rand
|
||||
}
|
||||
|
||||
// NewWorld creates a new world
|
||||
// NewWorld creates a new world.
|
||||
func NewWorld(width, height int) *World {
|
||||
world := World{
|
||||
area: makeArea(width, height),
|
||||
@ -48,7 +48,7 @@ func NewWorld(width, height int) *World {
|
||||
return &world
|
||||
}
|
||||
|
||||
// RandomSeed inits world with a random state
|
||||
// RandomSeed inits world with a random state.
|
||||
func (w *World) RandomSeed(limit int) {
|
||||
height := len(w.area)
|
||||
width := len(w.area[0])
|
||||
@ -59,14 +59,13 @@ func (w *World) RandomSeed(limit int) {
|
||||
}
|
||||
}
|
||||
|
||||
// Progress game state by one tick
|
||||
func (w *World) Progress() {
|
||||
// Update game state by one tick.
|
||||
func (w *World) Update() {
|
||||
height := len(w.area)
|
||||
width := len(w.area[0])
|
||||
next := makeArea(width, height)
|
||||
for y := 0; y < height; y++ {
|
||||
for x := 0; x < width; x++ {
|
||||
|
||||
pop := neighbourCount(w.area, x, y)
|
||||
switch {
|
||||
case pop < 2:
|
||||
@ -95,7 +94,7 @@ func (w *World) Progress() {
|
||||
}
|
||||
|
||||
// DrawImage paints current game state
|
||||
func (w *World) DrawImage(pix []uint8) {
|
||||
func (w *World) DrawImage(pix []byte) {
|
||||
height := len(w.area)
|
||||
width := len(w.area[0])
|
||||
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 {
|
||||
height := len(a)
|
||||
width := len(a[0])
|
||||
@ -163,11 +162,11 @@ const (
|
||||
|
||||
var (
|
||||
world = NewWorld(screenWidth, screenHeight)
|
||||
pixels = make([]uint8, screenWidth*screenHeight*4)
|
||||
pixels = make([]byte, screenWidth*screenHeight*4)
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
world.Progress()
|
||||
world.Update()
|
||||
if ebiten.IsRunningSlowly() {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user