doc: Update examples

This commit is contained in:
Hajime Hoshi 2017-08-17 23:13:15 +09:00
parent a225f666ac
commit 179b3134e3
2 changed files with 11 additions and 15 deletions

View File

@ -27,6 +27,9 @@
<iframe src="audio.content.html" width="640" height="480"></iframe> <iframe src="audio.content.html" width="640" height="480"></iframe>
<pre><code class="language-go">// &#43;build example <pre><code class="language-go">// &#43;build example
// This is an example to implement an audio player.
// See examples/wav for a simpler example to play a sound file.
package main package main
import ( import (
@ -71,7 +74,7 @@ type Input struct {
} }
func (i *Input) update() { func (i *Input) update() {
for _, key := range []ebiten.Key{ebiten.KeyP, ebiten.KeyS, ebiten.KeyX, ebiten.KeyZ} { for _, key := range []ebiten.Key{ebiten.KeyP, ebiten.KeyS, ebiten.KeyX, ebiten.KeyZ, ebiten.KeyB} {
if !ebiten.IsKeyPressed(key) { if !ebiten.IsKeyPressed(key) {
i.keyStates[key] = 0 i.keyStates[key] = 0
} else { } else {
@ -183,6 +186,10 @@ func (p *Player) update() error {
p.updatePlayPause() p.updatePlayPause()
p.updateSE() p.updateSE()
p.updateVolume() p.updateVolume()
if p.input.isKeyTriggered(ebiten.KeyB) {
b := ebiten.IsRunnableInBackground()
ebiten.SetRunnableInBackground(!b)
}
if err := p.audioContext.Update(); err != nil { if err := p.audioContext.Update(); err != nil {
return err return err
} }
@ -275,6 +282,7 @@ func (p *Player) draw(screen *ebiten.Image) {
Press S to toggle Play/Pause Press S to toggle Play/Pause
Press P to play SE Press P to play SE
Press Z or X to change volume of the music Press Z or X to change volume of the music
Press B to switch the run-in-background state
%s`, ebiten.CurrentFPS(), currentTimeStr) %s`, ebiten.CurrentFPS(), currentTimeStr)
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
} }

View File

@ -165,8 +165,6 @@ var (
) )
func init() { func init() {
imageEmpty, _ := ebiten.NewImage(16, 16, ebiten.FilterNearest)
imageEmpty.Fill(color.White)
imagePiano, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) imagePiano, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
whiteKeys := []string{&#34;A&#34;, &#34;S&#34;, &#34;D&#34;, &#34;F&#34;, &#34;G&#34;, &#34;H&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;} whiteKeys := []string{&#34;A&#34;, &#34;S&#34;, &#34;D&#34;, &#34;F&#34;, &#34;G&#34;, &#34;H&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;}
width := 24 width := 24
@ -174,12 +172,7 @@ func init() {
for i, k := range whiteKeys { for i, k := range whiteKeys {
x := i*width &#43; 36 x := i*width &#43; 36
height := 112 height := 112
op := &amp;ebiten.DrawImageOptions{} ebitenutil.DrawRect(imagePiano, float64(x), float64(y), float64(width-1), float64(height), color.White)
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1)
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black) common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black)
} }
@ -190,12 +183,7 @@ func init() {
} }
x := i*width &#43; 24 x := i*width &#43; 24
height := 64 height := 64
op := &amp;ebiten.DrawImageOptions{} ebitenutil.DrawRect(imagePiano, float64(x), float64(y), float64(width-1), float64(height), color.Black)
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1)
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White) common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White)
} }
} }