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] My Pygame Sprite not appearing... noodlespinbot 3 4,856 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  My Pygame Sprite not appearing... noodlespinbot 1 2,861 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Terrible Sprite controls, need help. michael1789 16 8,034 Dec-18-2019, 10:32 PM
Last Post: michael1789
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 5,703 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 3,841 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 3,412 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 4,269 Aug-11-2019, 09:12 PM
Last Post: metulburr
  creating sprite mask pfaber11 5 4,883 Jun-12-2019, 09:39 PM
Last Post: pfaber11
  [PyGame] assigning rect to sprite pfaber11 1 2,764 May-18-2019, 05:39 PM
Last Post: metulburr
  moving a sprite pfaber11 3 3,297 May-15-2019, 12:52 PM
Last Post: pfaber11

Forum Jump:

User Panel Messages

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