examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-05-23 23:18:21 +09:00
parent 3306a957ba
commit 0bbf0d5291
5 changed files with 108 additions and 77 deletions

View File

@ -131,40 +131,44 @@ func playNote(scoreIndex int) rune {
return rune(note) return rune(note)
} }
var ( type Game struct {
scoreIndex = 0 scoreIndex int
frames = 0 frames int
currentNote rune currentNote rune
) }
func update(screen *ebiten.Image) error { func (g *Game) Update(screen *ebiten.Image) error {
// Play notes for each half second. // Play notes for each half second.
if frames%30 == 0 && audioContext.IsReady() { if g.frames%30 == 0 && audioContext.IsReady() {
currentNote = playNote(scoreIndex) g.currentNote = playNote(g.scoreIndex)
scoreIndex++ g.scoreIndex++
scoreIndex %= len(score) g.scoreIndex %= len(score)
}
frames++
if ebiten.IsDrawingSkipped() {
return nil
} }
g.frames++
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
msg := "Note: " msg := "Note: "
if currentNote == 'R' || currentNote == 0 { if g.currentNote == 'R' || g.currentNote == 0 {
msg += "-" msg += "-"
} else { } else {
msg += string(currentNote) msg += string(g.currentNote)
} }
if !audioContext.IsReady() { if !audioContext.IsReady() {
msg += "\n\n(If the audio doesn't start,\n click the screen or press keys)" msg += "\n\n(If the audio doesn't start,\n click the screen or press keys)"
} }
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "PCM (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("PCM (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -35,11 +35,13 @@ var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { type Game struct{}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Split the image into horizontal lines and draw them with different scales. // Split the image into horizontal lines and draw them with different scales.
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
w, h := gophersImage.Size() w, h := gophersImage.Size()
@ -60,7 +62,10 @@ func update(screen *ebiten.Image) error {
screen.DrawImage(gophersImage.SubImage(image.Rect(0, i, w, i+1)).(*ebiten.Image), op) screen.DrawImage(gophersImage.SubImage(image.Rect(0, i, w, i+1)).(*ebiten.Image), op)
} }
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
@ -79,7 +84,9 @@ func main() {
} }
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Perspective (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -201,7 +201,10 @@ var (
} }
) )
func update(screen *ebiten.Image) error { type Game struct {
}
func (g *Game) Update(screen *ebiten.Image) error {
// The piano data is still being initialized. // The piano data is still being initialized.
// Get the progress if available. // Get the progress if available.
if !pianoNoteSamplesInited { if !pianoNoteSamplesInited {
@ -220,20 +223,24 @@ func update(screen *ebiten.Image) error {
playNote(baseFreq * math.Exp2(float64(i-1)/12.0)) playNote(baseFreq * math.Exp2(float64(i-1)/12.0))
} }
} }
return nil
}
if ebiten.IsDrawingSkipped() { func (g *Game) Draw(screen *ebiten.Image) {
return nil
}
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}) screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
screen.DrawImage(pianoImage, nil) screen.DrawImage(pianoImage, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Piano (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -86,7 +86,7 @@ func (c *char) tryJump() {
// //
// if gopher.y == groundY * unit { // if gopher.y == groundY * unit {
// ... // ...
gopher.vy = -10 * unit c.vy = -10 * unit
} }
func (c *char) update() { func (c *char) update() {
@ -120,41 +120,50 @@ func (c *char) draw(screen *ebiten.Image) {
screen.DrawImage(s, op) screen.DrawImage(s, op)
} }
var gopher = &char{x: 50 * unit, y: groundY * unit} type Game struct {
gopher *char
}
func (g *Game) Update(screen *ebiten.Image) error {
if g.gopher == nil {
g.gopher = &char{x: 50 * unit, y: groundY * unit}
}
func update(screen *ebiten.Image) error {
// Controls // Controls
if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft) { if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft) {
gopher.vx = -4 * unit g.gopher.vx = -4 * unit
} else if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight) { } else if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight) {
gopher.vx = 4 * unit g.gopher.vx = 4 * unit
} }
if inpututil.IsKeyJustPressed(ebiten.KeySpace) { if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
gopher.tryJump() g.gopher.tryJump()
}
gopher.update()
if ebiten.IsDrawingSkipped() {
return nil
} }
g.gopher.update()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Draws Background Image // Draws Background Image
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(0.5, 0.5) op.GeoM.Scale(0.5, 0.5)
screen.DrawImage(backgroundImage, op) screen.DrawImage(backgroundImage, op)
// Draws the Gopher // Draws the Gopher
gopher.draw(screen) g.gopher.draw(screen)
// Show the message // Show the message
msg := fmt.Sprintf("TPS: %0.2f\nPress the space key to jump.", ebiten.CurrentTPS()) msg := fmt.Sprintf("TPS: %0.2f\nPress the space key to jump.", ebiten.CurrentTPS())
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
}
return nil func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Platformer (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Platformer (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -40,13 +40,6 @@ func init() {
emptyImage.Fill(color.White) emptyImage.Fill(color.White)
} }
var (
vertices []ebiten.Vertex
ngon = 10
prevNgon = 0
)
func genVertices(num int) []ebiten.Vertex { func genVertices(num int) []ebiten.Vertex {
const ( const (
centerX = screenWidth / 2 centerX = screenWidth / 2
@ -98,43 +91,54 @@ func genVertices(num int) []ebiten.Vertex {
return vs return vs
} }
func update(screen *ebiten.Image) error { type Game struct {
vertices []ebiten.Vertex
ngon int
prevNgon int
}
func (g *Game) Update(screen *ebiten.Image) error {
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) { if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
ngon-- g.ngon--
if ngon < 1 { if g.ngon < 1 {
ngon = 1 g.ngon = 1
} }
} }
if inpututil.IsKeyJustPressed(ebiten.KeyRight) { if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
ngon++ g.ngon++
if ngon > 120 { if g.ngon > 120 {
ngon = 120 g.ngon = 120
} }
} }
if prevNgon != ngon || len(vertices) == 0 { if g.prevNgon != g.ngon || len(g.vertices) == 0 {
vertices = genVertices(ngon) g.vertices = genVertices(g.ngon)
prevNgon = ngon g.prevNgon = g.ngon
} }
if ebiten.IsDrawingSkipped() {
return nil
}
op := &ebiten.DrawTrianglesOptions{}
indices := []uint16{}
for i := 0; i < ngon; i++ {
indices = append(indices, uint16(i), uint16(i+1)%uint16(ngon), uint16(ngon))
}
screen.DrawTriangles(vertices, indices, emptyImage, op)
msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.CurrentTPS(), ngon)
ebitenutil.DebugPrint(screen, msg)
return nil return nil
} }
func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawTrianglesOptions{}
indices := []uint16{}
for i := 0; i < g.ngon; i++ {
indices = append(indices, uint16(i), uint16(i+1)%uint16(g.ngon), uint16(g.ngon))
}
screen.DrawTriangles(g.vertices, indices, emptyImage, op)
msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.CurrentTPS(), g.ngon)
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Polygons (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Polygons (Ebiten Demo)")
if err := ebiten.RunGame(&Game{ngon: 10}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }