|
|
@ -77,23 +77,36 @@ type Animation struct {
|
|
|
|
height int
|
|
|
|
height int
|
|
|
|
width int
|
|
|
|
width int
|
|
|
|
stroke int
|
|
|
|
stroke int
|
|
|
|
image image.Image
|
|
|
|
image []image.Image
|
|
|
|
|
|
|
|
updown int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewAnimation(sz image.Point) *Animation {
|
|
|
|
func NewAnimation(sz image.Point) *Animation {
|
|
|
|
reader, err := os.Open("mario.jpg")
|
|
|
|
reader, err := os.Open("marioUp.png")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rawMario, _, err := image.Decode(reader)
|
|
|
|
rawMario, _, err := image.Decode(reader)
|
|
|
|
mario := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
|
|
|
|
|
|
|
|
|
|
|
|
//marioUp := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
|
|
|
|
|
|
|
|
marioUp := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reader, err = os.Open("marioDown.png")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMario, _, err = image.Decode(reader)
|
|
|
|
|
|
|
|
//marioDown := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
|
|
|
|
|
|
|
|
marioDown := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
|
|
|
|
|
|
|
images := []image.Image{marioUp, marioDown}
|
|
|
|
return &Animation{
|
|
|
|
return &Animation{
|
|
|
|
ctx: gg.NewContext(sz.X, sz.Y),
|
|
|
|
ctx: gg.NewContext(sz.X, sz.Y),
|
|
|
|
dir: image.Point{1, 1},
|
|
|
|
dir: image.Point{1, 1},
|
|
|
|
height: 8,
|
|
|
|
height: 8,
|
|
|
|
width: 8,
|
|
|
|
width: 8,
|
|
|
|
stroke: 16,
|
|
|
|
stroke: 16,
|
|
|
|
image: mario,
|
|
|
|
image: images,
|
|
|
|
|
|
|
|
updown: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -101,10 +114,14 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
|
|
|
defer a.updatePosition()
|
|
|
|
defer a.updatePosition()
|
|
|
|
a.ctx.SetColor(color.Black)
|
|
|
|
a.ctx.SetColor(color.Black)
|
|
|
|
a.ctx.Clear()
|
|
|
|
a.ctx.Clear()
|
|
|
|
a.ctx.DrawImageAnchored(a.image, a.position.X, a.position.Y, 0.5, 0.5)
|
|
|
|
a.ctx.DrawCircle(float64(a.position.X), float64(a.position.Y), float64(a.stroke))
|
|
|
|
|
|
|
|
a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
|
|
|
|
|
|
|
|
if a.dir.X == 1 {
|
|
|
|
|
|
|
|
a.ctx.DrawImageAnchored(a.image[a.updown], a.position.X, a.position.Y, 0.5, 0.5)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
a.ctx.DrawImageAnchored(imaging.FlipH(a.image[a.updown]), a.position.X, a.position.Y, 0.5, 0.5)
|
|
|
|
|
|
|
|
}
|
|
|
|
//a.image = imaging.FlipH(a.image)
|
|
|
|
//a.image = imaging.FlipH(a.image)
|
|
|
|
//a.ctx.DrawCircle(float64(a.position.X), float64(a.position.Y), float64(a.stroke))
|
|
|
|
|
|
|
|
//a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
|
|
|
|
|
|
|
|
a.ctx.Fill()
|
|
|
|
a.ctx.Fill()
|
|
|
|
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
|
|
|
|
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -115,15 +132,15 @@ func (a *Animation) updatePosition() {
|
|
|
|
|
|
|
|
|
|
|
|
if a.position.Y+a.height > a.ctx.Height() {
|
|
|
|
if a.position.Y+a.height > a.ctx.Height() {
|
|
|
|
a.dir.Y = -1
|
|
|
|
a.dir.Y = -1
|
|
|
|
|
|
|
|
a.updown = 0
|
|
|
|
} else if a.position.Y-a.height < 0 {
|
|
|
|
} else if a.position.Y-a.height < 0 {
|
|
|
|
|
|
|
|
a.updown = 1
|
|
|
|
a.dir.Y = 1
|
|
|
|
a.dir.Y = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if a.position.X+a.width > a.ctx.Width() {
|
|
|
|
if a.position.X+a.width > a.ctx.Width() {
|
|
|
|
a.dir.X = -1
|
|
|
|
a.dir.X = -1
|
|
|
|
a.image = imaging.FlipH(a.image)
|
|
|
|
|
|
|
|
} else if a.position.X-a.width < 0 {
|
|
|
|
} else if a.position.X-a.width < 0 {
|
|
|
|
a.dir.X = 1
|
|
|
|
a.dir.X = 1
|
|
|
|
a.image = imaging.FlipH(a.image)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|