Reputation: 5
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
how ends
with which technique is possible I can do this?
Thanks!
`
Upvotes: 0
Views: 130
Reputation: 5830
Well a couple of things can help
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