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)
}
var (
scoreIndex = 0
frames = 0
type Game struct {
scoreIndex int
frames int
currentNote rune
)
}
func update(screen *ebiten.Image) error {
func (g *Game) Update(screen *ebiten.Image) error {
// Play notes for each half second.
if frames%30 == 0 && audioContext.IsReady() {
currentNote = playNote(scoreIndex)
scoreIndex++
scoreIndex %= len(score)
}
frames++
if ebiten.IsDrawingSkipped() {
return nil
if g.frames%30 == 0 && audioContext.IsReady() {
g.currentNote = playNote(g.scoreIndex)
g.scoreIndex++
g.scoreIndex %= len(score)
}
g.frames++
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
msg := "Note: "
if currentNote == 'R' || currentNote == 0 {
if g.currentNote == 'R' || g.currentNote == 0 {
msg += "-"
} else {
msg += string(currentNote)
msg += string(g.currentNote)
}
if !audioContext.IsReady() {
msg += "\n\n(If the audio doesn't start,\n click the screen or press keys)"
}
ebitenutil.DebugPrint(screen, msg)
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
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)
}
}

View File

@ -35,11 +35,13 @@ var (
gophersImage *ebiten.Image
)
func update(screen *ebiten.Image) error {
if ebiten.IsDrawingSkipped() {
return nil
}
type Game struct{}
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.
op := &ebiten.DrawImageOptions{}
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)
}
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
@ -79,7 +84,9 @@ func main() {
}
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)
}
}

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.
// Get the progress if available.
if !pianoNoteSamplesInited {
@ -220,20 +223,24 @@ func update(screen *ebiten.Image) error {
playNote(baseFreq * math.Exp2(float64(i-1)/12.0))
}
}
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
screen.DrawImage(pianoImage, nil)
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() {
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)
}
}

View File

@ -86,7 +86,7 @@ func (c *char) tryJump() {
//
// if gopher.y == groundY * unit {
// ...
gopher.vy = -10 * unit
c.vy = -10 * unit
}
func (c *char) update() {
@ -120,41 +120,50 @@ func (c *char) draw(screen *ebiten.Image) {
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
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) {
gopher.vx = 4 * unit
g.gopher.vx = 4 * unit
}
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
gopher.tryJump()
}
gopher.update()
if ebiten.IsDrawingSkipped() {
return nil
g.gopher.tryJump()
}
g.gopher.update()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Draws Background Image
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(0.5, 0.5)
screen.DrawImage(backgroundImage, op)
// Draws the Gopher
gopher.draw(screen)
g.gopher.draw(screen)
// Show the message
msg := fmt.Sprintf("TPS: %0.2f\nPress the space key to jump.", ebiten.CurrentTPS())
ebitenutil.DebugPrint(screen, msg)
}
return nil
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
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)
}
}

View File

@ -40,13 +40,6 @@ func init() {
emptyImage.Fill(color.White)
}
var (
vertices []ebiten.Vertex
ngon = 10
prevNgon = 0
)
func genVertices(num int) []ebiten.Vertex {
const (
centerX = screenWidth / 2
@ -98,43 +91,54 @@ func genVertices(num int) []ebiten.Vertex {
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) {
ngon--
if ngon < 1 {
ngon = 1
g.ngon--
if g.ngon < 1 {
g.ngon = 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
ngon++
if ngon > 120 {
ngon = 120
g.ngon++
if g.ngon > 120 {
g.ngon = 120
}
}
if prevNgon != ngon || len(vertices) == 0 {
vertices = genVertices(ngon)
prevNgon = ngon
if g.prevNgon != g.ngon || len(g.vertices) == 0 {
g.vertices = genVertices(g.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
}
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() {
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)
}
}