Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "errors"
- "log"
- "math/rand"
- "time"
- "github.com/hajimehoshi/ebiten"
- "github.com/hajimehoshi/ebiten/ebitenutil"
- )
- const (
- SCREEN_WIDTH = 320
- SCREEN_HEIGHT = 240
- SCALE_FACTOR = 2
- APP_TITLE = "Draw Items"
- )
- var image *ebiten.Image
- var sprites Sprites
- var random = rand.New(rand.NewSource(time.Now().UnixNano()))
- type Sprite struct {
- img *ebiten.Image
- op *ebiten.DrawImageOptions
- x float64
- y float64
- vx float64
- vy float64
- count int
- }
- func NewSprite(image *ebiten.Image) (*Sprite, error) {
- s := new(Sprite)
- // Set Image
- s.img = image
- // Set DrawImageOptions obj
- s.op = new(ebiten.DrawImageOptions)
- w, h := s.img.Size()
- // Set x
- x := float64(random.Intn(SCREEN_WIDTH - w))
- err := s.setX(x)
- if err != nil {
- return s, err
- }
- // Set y
- y := float64(random.Intn(SCREEN_HEIGHT - h))
- err = s.setY(y)
- if err != nil {
- return s, err
- }
- s.op.GeoM.Translate(s.x, s.y)
- // Set vx
- s.vx = s.getVelocity()
- // Set vy
- s.vy = s.getVelocity()
- return s, nil
- }
- func (s *Sprite) setX(x float64) error {
- w, _ := s.img.Size()
- if x < 0 || x > float64(SCREEN_WIDTH-w) {
- return errors.New("Invalid x value")
- }
- s.x = x
- return nil
- }
- func (s *Sprite) setY(y float64) error {
- _, h := s.img.Size()
- if y < 0 || y > float64(SCREEN_HEIGHT-h) {
- return errors.New("Invalid y value")
- }
- s.y = y
- return nil
- }
- // Get random velocity other than zero
- func (s *Sprite) getVelocity() float64 {
- m := 0
- v := 0.0
- for m == 0 {
- v = float64(random.Intn(9) - 4)
- m = int(v)
- }
- return v
- }
- func (s *Sprite) GetImg() *ebiten.Image {
- return s.img
- }
- func (s *Sprite) GetOp() *ebiten.DrawImageOptions {
- return s.op
- }
- func (s *Sprite) Update() {
- w, h := s.img.Size()
- // Rotate the image in place
- theta := 0.2
- if s.vx < 0 {
- theta = -theta
- }
- s.op.GeoM.Translate(-s.x-float64(w)/2, -s.y-float64(h)/2)
- s.op.GeoM.Rotate(theta)
- s.op.GeoM.Translate(s.x+float64(w)/2, s.y+float64(h)/2)
- // Move the image
- s.x += s.vx
- s.y += s.vy
- s.op.GeoM.Translate(s.vx, s.vy)
- // Basic collision detection
- // Revert the direction if hit the edge of screen
- if s.x <= 0.0 || s.x >= float64(SCREEN_WIDTH-w) {
- s.vx = -s.vx
- }
- // Basic collision detection
- // Revert the direction if hit the edge of screen
- if s.y <= 0.0 || s.y >= float64(SCREEN_HEIGHT-h) {
- s.vy = -s.vy
- }
- }
- type Sprites struct {
- sprites []*Sprite
- }
- func (s *Sprites) Append(ss *Sprite) {
- s.sprites = append(s.sprites, ss)
- }
- func (s *Sprites) Init(image *ebiten.Image) error {
- numOfBottles := random.Intn(31) + 20
- for i := 0; i < numOfBottles; i++ {
- ss, err := NewSprite(image)
- if err != nil {
- return err
- }
- s.Append(ss)
- }
- return nil
- }
- func (s *Sprites) Update(screen *ebiten.Image) error {
- for _, ss := range s.sprites {
- ss.Update()
- if err := screen.DrawImage(ss.GetImg(), ss.GetOp()); err != nil {
- return err
- }
- }
- return nil
- }
- func init() {
- var err error
- image, _, err = ebitenutil.NewImageFromFile("beerBottle.png",
- ebiten.FilterNearest)
- if err != nil {
- log.Fatal(err)
- }
- sprites = Sprites{}
- if err = sprites.Init(image); err != nil {
- log.Fatal(err)
- }
- }
- func update(screen *ebiten.Image) error {
- if err := sprites.Update(screen); err != nil {
- return err
- }
- return nil
- }
- func main() {
- ebiten.Run(update, SCREEN_WIDTH, SCREEN_HEIGHT, SCALE_FACTOR, APP_TITLE)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement