mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 03:58:55 +01:00
examples/spritse: Speed optimization
This commit is contained in:
parent
2c4dc3681a
commit
26e290b2bc
@ -38,7 +38,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Sprite struct {
|
type Sprite struct {
|
||||||
image *ebiten.Image
|
imageWidth int
|
||||||
|
imageHeight int
|
||||||
x int
|
x int
|
||||||
y int
|
y int
|
||||||
vx int
|
vx int
|
||||||
@ -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,7 +148,8 @@ 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,
|
||||||
|
imageHeight: h,
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
vx: vx,
|
vx: vx,
|
||||||
|
Loading…
Reference in New Issue
Block a user