mirror of
				https://github.com/hajimehoshi/ebiten.git
				synced 2025-10-31 03:36:21 +08:00 
			
		
		
		
	examples/spritse: Speed optimization
This commit is contained in:
		| @@ -38,7 +38,8 @@ var ( | ||||
| ) | ||||
|  | ||||
| type Sprite struct { | ||||
| 	image *ebiten.Image | ||||
| 	imageWidth  int | ||||
| 	imageHeight int | ||||
| 	x           int | ||||
| 	y           int | ||||
| 	vx          int | ||||
| @@ -51,18 +52,15 @@ func (s *Sprite) Update() { | ||||
| 	if s.x < 0 { | ||||
| 		s.x = -s.x | ||||
| 		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 { | ||||
| 		s.y = -s.y | ||||
| 		s.vy = -s.vy | ||||
| 	} | ||||
| 	w, h := s.image.Size() | ||||
| 	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 | ||||
| 	} else if screenHeight <= s.y+s.imageHeight { | ||||
| 		s.y = 2*(screenHeight-s.imageHeight) - s.y | ||||
| 		s.vy = -s.vy | ||||
| 	} | ||||
| } | ||||
| @@ -150,7 +148,8 @@ func main() { | ||||
| 		x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h) | ||||
| 		vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1 | ||||
| 		sprites.sprites[i] = &Sprite{ | ||||
| 			image: ebitenImage, | ||||
| 			imageWidth:  w, | ||||
| 			imageHeight: h, | ||||
| 			x:           x, | ||||
| 			y:           y, | ||||
| 			vx:          vx, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Hajime Hoshi
					Hajime Hoshi