main.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2014 Hajime Hoshi
  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/color"
  19. _ "image/png"
  20. "log"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  23. )
  24. const (
  25. screenWidth = 320
  26. screenHeight = 240
  27. )
  28. var (
  29. ebitenImage *ebiten.Image
  30. )
  31. type Game struct {
  32. count int
  33. }
  34. func (g *Game) Update() error {
  35. g.count++
  36. g.count %= ebiten.TPS() * 10
  37. return nil
  38. }
  39. func (g *Game) offset() float64 {
  40. v := float64(g.count) * 0.2
  41. switch {
  42. case 480 < g.count:
  43. v = 0
  44. case 240 < g.count:
  45. v = float64(480-g.count) * 0.2
  46. }
  47. return v
  48. }
  49. func (g *Game) Draw(screen *ebiten.Image) {
  50. screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
  51. // Draw 100 Ebitens
  52. v := g.offset()
  53. op := &ebiten.DrawImageOptions{}
  54. op.ColorScale.ScaleAlpha(0.5)
  55. for i := 0; i < 10*10; i++ {
  56. op.GeoM.Reset()
  57. x := float64(i%10)*v + 15
  58. y := float64(i/10)*v + 20
  59. op.GeoM.Translate(x, y)
  60. screen.DrawImage(ebitenImage, op)
  61. }
  62. }
  63. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  64. return screenWidth, screenHeight
  65. }
  66. func main() {
  67. // Decode an image from the image file's byte slice.
  68. img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. ebitenImage = ebiten.NewImageFromImage(img)
  73. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  74. ebiten.SetWindowTitle("Alpha Blending (Ebitengine Demo)")
  75. if err := ebiten.RunGame(&Game{}); err != nil {
  76. log.Fatal(err)
  77. }
  78. }