Python Forum
[PyGame] Efficient sprite scaling with Vector3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Efficient sprite scaling with Vector3
#1
For anyone who knows, I'm intending to scale sprites based on the pygame.math.Vector3 z axis. MY question is how to do this in a computationally efficient fashion.

Its a space shooter and I want enemies to scale according to distance (the z axis), but I'm unsure as to the smartest way to do this.

The first option is to have the image scale in the sprites update section. Scaling images for many sprites at a time seems like it could consume a lot of resources and impact FPS.

The other option I'm looking at is to load all the various scaled images into a dictionary via a loop where the value of the z axes are the keys in the dictionary.
fighter_image = pygame.image.load('fighter.png')
fighter_scaled_images = {}
for z in range(0, 100, 5):
    img = pygame.trasform.scale(fighter_image, (z, z))
    fighter_scaled_images[z] = img
Then the sprite can retrieve the already scaled image from this dictionary. The same could be done with the rotations, but in the case of rotations and scales, I don't know if it is faster for the computer to do it this way that to scale and rotate the original image with the sprite updates.

Is this a sound approach or is it slower for python to get images from dictionaries like this?
Reply
#2
Yes. This will work. You have to watch your memory usage thou. Bigger image more memory.
Memory is the only thing that will prevent you from doing this.

python dict are very fast.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,551 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,095 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,184 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,546 May-15-2019, 12:52 PM
Last Post: pfaber11
  [PyGame] Need Help With Sprite ghost0fkarma 2 3,252 Jan-09-2018, 02:14 PM
Last Post: ghost0fkarma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020