Advertisement
cwchen

Drawing Items in an Ebiten program

Oct 18th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.76 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "errors"
  5.     "log"
  6.     "math/rand"
  7.     "time"
  8.  
  9.     "github.com/hajimehoshi/ebiten"
  10.     "github.com/hajimehoshi/ebiten/ebitenutil"
  11. )
  12.  
  13. const (
  14.     SCREEN_WIDTH  = 320
  15.     SCREEN_HEIGHT = 240
  16.     SCALE_FACTOR  = 2
  17.     APP_TITLE     = "Draw Items"
  18. )
  19.  
  20. var image *ebiten.Image
  21. var sprites Sprites
  22. var random = rand.New(rand.NewSource(time.Now().UnixNano()))
  23.  
  24. type Sprite struct {
  25.     img *ebiten.Image
  26.     x   int
  27.     y   int
  28. }
  29.  
  30. func NewSprite(image *ebiten.Image, x int, y int) (*Sprite, error) {
  31.     s := new(Sprite)
  32.     s.img = image
  33.  
  34.     err := s.SetX(x)
  35.     if err != nil {
  36.         return s, err
  37.     }
  38.  
  39.     err = s.SetY(y)
  40.     if err != nil {
  41.         return s, err
  42.     }
  43.  
  44.     return s, nil
  45. }
  46.  
  47. func (s *Sprite) SetX(x int) error {
  48.     w, _ := s.img.Size()
  49.     if x < 0 || x > SCREEN_WIDTH-w {
  50.         return errors.New("Invalid x value")
  51.     }
  52.  
  53.     s.x = x
  54.     return nil
  55. }
  56.  
  57. func (s *Sprite) SetY(y int) error {
  58.     _, h := s.img.Size()
  59.     if y < 0 || y > SCREEN_HEIGHT-h {
  60.         return errors.New("Invalid y value")
  61.     }
  62.  
  63.     s.y = y
  64.     return nil
  65. }
  66.  
  67. type Sprites struct {
  68.     sprites []*Sprite
  69. }
  70.  
  71. // Mandatory method for ImageParts interface
  72. func (s *Sprites) Len() int {
  73.     return len(s.sprites)
  74. }
  75.  
  76. // Mandatory method for ImageParts interface
  77. func (s *Sprites) Dst(i int) (x0, y0, x1, y1 int) {
  78.     if s.Len() <= i {
  79.         return 0, 0, 0, 0
  80.     }
  81.  
  82.     ss := s.sprites[i]
  83.     w, h := ss.img.Size()
  84.     return ss.x, ss.y, ss.x + w, ss.y + h
  85. }
  86.  
  87. // Mandatory method for ImageParts interface
  88. func (s *Sprites) Src(i int) (x0, y0, x1, y1 int) {
  89.     if s.Len() <= i {
  90.         return 0, 0, 0, 0
  91.     }
  92.  
  93.     ss := s.sprites[i]
  94.     w, h := ss.img.Size()
  95.     return 0, 0, w, h
  96. }
  97.  
  98. func (s *Sprites) Append(ss *Sprite) {
  99.     s.sprites = append(s.sprites, ss)
  100. }
  101.  
  102. func (s *Sprites) Init() error {
  103.     numOfBottles := random.Intn(7) + 4
  104.  
  105.     w, h := image.Size()
  106.     for i := 0; i < numOfBottles; i++ {
  107.         x := random.Intn(SCREEN_WIDTH - w)
  108.         y := random.Intn(SCREEN_HEIGHT - h)
  109.         ss, err := NewSprite(image, x, y)
  110.         if err != nil {
  111.             return err
  112.         }
  113.         s.Append(ss)
  114.     }
  115.  
  116.     return nil
  117. }
  118.  
  119. func (s *Sprites) Update(screen *ebiten.Image) error {
  120.     // Shared DrawImageOptions obj for Sprites
  121.     op := &ebiten.DrawImageOptions{
  122.         ImageParts: s,
  123.     }
  124.     // In this program, all bottle images share the same image file
  125.     if err := screen.DrawImage(image, op); err != nil {
  126.         return err
  127.     }
  128.  
  129.     return nil
  130. }
  131.  
  132. func init() {
  133.     var err error
  134.  
  135.     image, _, err = ebitenutil.NewImageFromFile("beerBottle.png",
  136.         ebiten.FilterNearest)
  137.     if err != nil {
  138.         log.Fatal(err)
  139.     }
  140.  
  141.     sprites = Sprites{}
  142.     if err = sprites.Init(); err != nil {
  143.         log.Fatal(err)
  144.     }
  145. }
  146.  
  147. func update(screen *ebiten.Image) error {
  148.     if err := sprites.Update(screen); err != nil {
  149.         return err
  150.     }
  151.  
  152.     return nil
  153. }
  154.  
  155. func main() {
  156.     ebiten.Run(update, SCREEN_WIDTH, SCREEN_HEIGHT, SCALE_FACTOR, APP_TITLE)
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement