mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
examples/spritse: Speed optimization
This commit is contained in:
parent
2c4dc3681a
commit
26e290b2bc
@ -38,11 +38,12 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Sprite struct {
|
type Sprite struct {
|
||||||
image *ebiten.Image
|
imageWidth int
|
||||||
x int
|
imageHeight int
|
||||||
y int
|
x int
|
||||||
vx int
|
y int
|
||||||
vy int
|
vx int
|
||||||
|
vy int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sprite) Update() {
|
func (s *Sprite) Update() {
|
||||||
@ -51,18 +52,15 @@ func (s *Sprite) Update() {
|
|||||||
if s.x < 0 {
|
if s.x < 0 {
|
||||||
s.x = -s.x
|
s.x = -s.x
|
||||||
s.vx = -s.vx
|
s.vx = -s.vx
|
||||||
|
} else if screenWidth <= s.x+s.imageWidth {
|
||||||
|
s.x = 2*(screenWidth-s.imageWidth) - s.x
|
||||||
|
s.vx = -s.vx
|
||||||
}
|
}
|
||||||
if s.y < 0 {
|
if s.y < 0 {
|
||||||
s.y = -s.y
|
s.y = -s.y
|
||||||
s.vy = -s.vy
|
s.vy = -s.vy
|
||||||
}
|
} else if screenHeight <= s.y+s.imageHeight {
|
||||||
w, h := s.image.Size()
|
s.y = 2*(screenHeight-s.imageHeight) - s.y
|
||||||
if screenWidth <= s.x+w {
|
|
||||||
s.x = 2*(screenWidth-w) - s.x
|
|
||||||
s.vx = -s.vx
|
|
||||||
}
|
|
||||||
if screenHeight <= s.y+h {
|
|
||||||
s.y = 2*(screenHeight-h) - s.y
|
|
||||||
s.vy = -s.vy
|
s.vy = -s.vy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -150,11 +148,12 @@ func main() {
|
|||||||
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
|
||||||
sprites.sprites[i] = &Sprite{
|
sprites.sprites[i] = &Sprite{
|
||||||
image: ebitenImage,
|
imageWidth: w,
|
||||||
x: x,
|
imageHeight: h,
|
||||||
y: y,
|
x: x,
|
||||||
vx: vx,
|
y: y,
|
||||||
vy: vy,
|
vx: vx,
|
||||||
|
vy: vy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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