mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Refactoring
This commit is contained in:
parent
b84cc9a57b
commit
88eeb6d65d
@ -31,23 +31,27 @@ func NewSprite(screenWidth, screenHeight int,
|
|||||||
vx: rand.Intn(2)*2 - 1,
|
vx: rand.Intn(2)*2 - 1,
|
||||||
vy: rand.Intn(2)*2 - 1,
|
vy: rand.Intn(2)*2 - 1,
|
||||||
}
|
}
|
||||||
go func() {
|
go sprite.update(screenWidth, screenHeight)
|
||||||
for {
|
|
||||||
<-sprite.ch
|
|
||||||
sprite.x += sprite.vx
|
|
||||||
sprite.y += sprite.vy
|
|
||||||
if sprite.x < 0 || maxX <= sprite.x {
|
|
||||||
sprite.vx = -sprite.vx
|
|
||||||
}
|
|
||||||
if sprite.y < 0 || maxY <= sprite.y {
|
|
||||||
sprite.vy = -sprite.vy
|
|
||||||
}
|
|
||||||
sprite.ch <- true
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return sprite
|
return sprite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sprite *Sprite) update(screenWidth, screenHeight int) {
|
||||||
|
maxX := screenWidth - sprite.texture.Width
|
||||||
|
maxY := screenHeight - sprite.texture.Height
|
||||||
|
for {
|
||||||
|
<-sprite.ch
|
||||||
|
sprite.x += sprite.vx
|
||||||
|
sprite.y += sprite.vy
|
||||||
|
if sprite.x < 0 || maxX <= sprite.x {
|
||||||
|
sprite.vx = -sprite.vx
|
||||||
|
}
|
||||||
|
if sprite.y < 0 || maxY <= sprite.y {
|
||||||
|
sprite.vy = -sprite.vy
|
||||||
|
}
|
||||||
|
sprite.ch <- true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (sprite *Sprite) Update() {
|
func (sprite *Sprite) Update() {
|
||||||
sprite.ch <- true
|
sprite.ch <- true
|
||||||
<-sprite.ch
|
<-sprite.ch
|
||||||
|
Loading…
Reference in New Issue
Block a user