Cássio Evaldt
Cássio Evaldt

Reputation: 5

pygame - create fluid effect to make a surface smaller

I want create an effect in pygame where an surface get smaller slowly. I try this:

In main loop:

earth = pygame.transform.scale(earth, (int(earth.get_width()*0.9999), int(earth.get_height()*0.9999)))   

how begins http://s14.postimg.org/d4rblmdgt/Untitled2.png

how ends http://s14.postimg.org/ng3oea565/Untitled1.png

with which technique is possible I can do this?

Thanks!

`

Upvotes: 0

Views: 130

Answers (1)

cmd
cmd

Reputation: 5830

Well a couple of things can help

  1. Don't overwrite your original, this can cause an assortment of transformation problem to accumulate. Keep your original as is and perform the total transformation you want each time.
  2. Don't scale by a static number each time through the loop. Each loop will not have exactly the same amount of time between them. Use time since start of shrinkage * rate of shrinkage. This will keep your animation smooth and prevent accumulation errors.

code would look something like this:

# time_start is when you start the shrinkage
# time_end is when the shrinkage should be completed
now = time.time()
if now < time_end:
    shrinking = (time_end - now) / (time_end - time_start)
    new_size = (int(earth.get_width()*shrinking),
                int(earth.get_height()*shrinking))
    earth_scaled = pygame.transform.scale(earth, new_size)  
    # draw earth_scaled

Note: you will probably want to also pay attention to where you are drawing it as your earth_scaled gets smaller.


Response to Comment

Transformations are not loss-less. Each transform will make your image less perfect. You can get artifacts, cropping issues, etc. For instance in your case you were multiplying the width and height by just less then 1, then truncating it with int. This will result is reducing the size by 1 pixel each iteration (unless you have a crazy large image). Scaling an image to 1 less pixel width than it started with will probably just omit one of the pixel columns. If you keep doing this it will keep removing 1 column and 1 row each time. (not what you want). If instead you take the full image and scale it, the scale function can make better choices about what to omit or merge.

Upvotes: 1

Related Questions