Looping Over Several Objects

It's possible to use the same loop to interact with different objects. In the following example, we'll superimpose a baseball over a lake. We do this by looping over the pixels in all the rows and columns of the baseball image and copying them one by one into the lake image. The top-left corner of both images is located at (0, 0). We can position the baseball away from the corner by adding offsets when we refer to indices in the lake image; the result is shown in Figure 7.3, on the following page.

Download loop/multiloop.py

import media baseball = media.load_picture( 'baseball.png') lake = media.load_picture( 'lake.png')

width, height = media.get_width(baseball), media.get_height(baseball) for y in range(0, height):

Figure 7.3: baseballjake.png

# Position the top-left of the baseball at (50, 25) from_p = media.get_pixe1(baseba11, x, y) to_p = media.get_pixe1(1ake, 50 + x, 25 + y) media.set_co1or(to_p, media.get_co1or(from_p))


Was this article helpful?

0 0

Post a comment