1
0

main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "image"
  18. _ "image/png"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  22. )
  23. const (
  24. screenWidth = 320
  25. screenHeight = 240
  26. frameOX = 0
  27. frameOY = 32
  28. frameWidth = 32
  29. frameHeight = 32
  30. frameCount = 8
  31. )
  32. var (
  33. runnerImage *ebiten.Image
  34. )
  35. type Game struct {
  36. count int
  37. }
  38. func (g *Game) Update() error {
  39. g.count++
  40. return nil
  41. }
  42. func (g *Game) Draw(screen *ebiten.Image) {
  43. op := &ebiten.DrawImageOptions{}
  44. op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
  45. op.GeoM.Translate(screenWidth/2, screenHeight/2)
  46. i := (g.count / 5) % frameCount
  47. sx, sy := frameOX+i*frameWidth, frameOY
  48. screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
  49. }
  50. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  51. return screenWidth, screenHeight
  52. }
  53. func main() {
  54. // Decode an image from the image file's byte slice.
  55. img, _, err := image.Decode(bytes.NewReader(images.Runner_png))
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. runnerImage = ebiten.NewImageFromImage(img)
  60. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  61. ebiten.SetWindowTitle("Animation (Ebitengine Demo)")
  62. if err := ebiten.RunGame(&Game{}); err != nil {
  63. log.Fatal(err)
  64. }
  65. }