Displaying ready-made images

Drawing basic shapes can be fun and sometimes challenging. Still, it would be even more fun to be able to combine our drawing with ready-made pictures or photos. In PyGame environment this is very simple. Let’s look at the following example:

../_images/tree.png ../_images/apple_small.png ../_images/basket.png

We have two new PyGame functions in this program:

Try typing different coordinate values instead of \((0, 0)\) to get a better understanding of how the blit function works. We can observe that displaying ready-made images is similar to displaying movable drawings using an anchor.

You can also try displaying one image in multiple places on the screen, as we did with basic shapes. Just call the blit function multiple times, with different values for the display location.

When displaying an image in multiple places, the images may overlap. Therefore, care must be taken of the order of displaying. In our case, we should first show the tree we imagine as further, and then the tree we imagine as closer. Otherwise the final scene may seem wrong, as the following picture shows.

../_images/trees_and_apples_bad.png

The objects we perceive as further are usually in the upper part of the image, which means that they have a smaller \(y\) coordinate, though this may not always be true. In this and similar examples it will be enough to stick to this simple rule: it is better to first display objects with smaller \(y\) coordinate.

Tasks for exercise

Task - trees

In the following program, correct the order of tree positions in the list, and then add a statement to draw a tree at position (x, y) in the loop.

Task - Apples

Finish the program by drawing the tree with the apples (as in the example).

After you finish the program, you can also try modifying the program to display an apple tree 100 pixels to the right and 50 pixels lower.

Task - Checkmate

Write a program that draws a chess position, as in the example. The filenames for the empty chessboard, white king, white rook and black king image files are respectively “chess_table.png”, “white_king.png”, “white_rook.png”, “black_king.png”.

../_images/chess_table.png ../_images/white_king.png ../_images/white_rook.png ../_images/black_king.png

Task - Orchard

In the next task the drawing of the orchard has been started. If we run the program, we will notice some inconsistencies. One problem is that apples are only found on the first tree, and they should be located on each tree, arranged in the same way. In addition, the second tree on the left (correctly) overlaps the leftmost tree but does not overlap its apples. We need to display the tree we show earlier along with its apples before moving on to the next tree.

Correct the program so that it displays the image obtained by clicking the “Play task” button.