mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
examples/sprites: Rotate images (#277)
This commit is contained in:
parent
b66ead56c8
commit
3a58bd7f26
@ -20,6 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
@ -29,6 +30,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
screenWidth = 320
|
screenWidth = 320
|
||||||
screenHeight = 240
|
screenHeight = 240
|
||||||
|
maxAngle = 256
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -42,6 +44,7 @@ type Sprite struct {
|
|||||||
y int
|
y int
|
||||||
vx int
|
vx int
|
||||||
vy int
|
vy int
|
||||||
|
angle int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sprite) Update() {
|
func (s *Sprite) Update() {
|
||||||
@ -61,6 +64,8 @@ func (s *Sprite) Update() {
|
|||||||
s.y = 2*(screenHeight-s.imageHeight) - s.y
|
s.y = 2*(screenHeight-s.imageHeight) - s.y
|
||||||
s.vy = -s.vy
|
s.vy = -s.vy
|
||||||
}
|
}
|
||||||
|
s.angle++
|
||||||
|
s.angle %= maxAngle
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sprites struct {
|
type Sprites struct {
|
||||||
@ -102,9 +107,13 @@ func update(screen *ebiten.Image) error {
|
|||||||
if ebiten.IsRunningSlowly() {
|
if ebiten.IsRunningSlowly() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
w, h := ebitenImage.Size()
|
||||||
for i := 0; i < sprites.num; i++ {
|
for i := 0; i < sprites.num; i++ {
|
||||||
s := sprites.sprites[i]
|
s := sprites.sprites[i]
|
||||||
op.GeoM.Reset()
|
op.GeoM.Reset()
|
||||||
|
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
|
||||||
|
op.GeoM.Rotate(2 * math.Pi * float64(s.angle) / maxAngle)
|
||||||
|
op.GeoM.Translate(float64(w)/2, float64(h)/2)
|
||||||
op.GeoM.Translate(float64(s.x), float64(s.y))
|
op.GeoM.Translate(float64(s.x), float64(s.y))
|
||||||
screen.DrawImage(ebitenImage, op)
|
screen.DrawImage(ebitenImage, op)
|
||||||
}
|
}
|
||||||
@ -132,6 +141,7 @@ func main() {
|
|||||||
w, h := ebitenImage.Size()
|
w, h := ebitenImage.Size()
|
||||||
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)
|
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)
|
||||||
vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1
|
vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1
|
||||||
|
a := rand.Intn(maxAngle)
|
||||||
sprites.sprites[i] = &Sprite{
|
sprites.sprites[i] = &Sprite{
|
||||||
imageWidth: w,
|
imageWidth: w,
|
||||||
imageHeight: h,
|
imageHeight: h,
|
||||||
@ -139,6 +149,7 @@ func main() {
|
|||||||
y: y,
|
y: y,
|
||||||
vx: vx,
|
vx: vx,
|
||||||
vy: vy,
|
vy: vy,
|
||||||
|
angle: a,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user