0

I'm currently making a game in pygame, Python 3 and a part of the code that has been giving me issues is:

for counter in range(0, 30):
                particles = pygame.image.load('particles.png').convert()
                particles = pygame.transform.rotozoom(particles, 36*counter, 1.1**counter).convert()                
                particles.set_colorkey((0, 0, 0, 0))
                screen.blit(particles, particles.get_rect(centerx=480, centery=100))                
                pygame.display.flip()                
                time.sleep(0.05)

particles.png is just a few colored pixels on a transparent background. The problem is that when the image is rotated and scaled, some of those particles sort of blur out resulting in a mass of black squares around them.

How do I fix this problem? Thanks in advance!!

Vladimir Shevyakov
  • 2,511
  • 4
  • 19
  • 40
  • 1
    Is your question about the blur or the transparent pixels? If it's about keeping the transparent pixels, just do `convert_alpha()` instead of `convert` on line 3. If it's about the blurry pixels, I'm not sure what's causing it. – DJMcMayhem Apr 21 '16 at 06:41

1 Answers1

1

I've had a bad experience with pygame's rotozoom combined with transparency. Instead of loading the image and then rotozooming it, consider the following:

  • Using PIL library:

    1. Convert the loaded surface to a string using pygame.image.tostring
    2. Convert the string to an Image object
    3. Use PIL's functions to replace rotozoom (zooming, rotating)
    4. Convert the Image object back into a surface using the same method
  • Using math:

    1. Load data about the position and color of the particles, or infer it from the loaded surface
    2. Use simple mathematical functions to caculate their new position, according to the angle and zoom
    3. Paint them using pygame.gfxdraw, pygame.draw or pygame.Surface.set_at

Good luck!

kmaork
  • 5,722
  • 2
  • 23
  • 40