$$ \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} \renewcommand{\mod}{\,\mathrm{mod}\,} \renewcommand{\div}{\,\mathrm{div}\,} \newcommand{\metar}{\,\mathrm{m}} \newcommand{\cm}{\,\mathrm{cm}} \newcommand{\dm}{\,\mathrm{dm}} \newcommand{\litar}{\,\mathrm{l}} \newcommand{\km}{\,\mathrm{km}} \newcommand{\s}{\,\mathrm{s}} \newcommand{\h}{\,\mathrm{h}} \newcommand{\minut}{\,\mathrm{min}} \newcommand{\kmh}{\,\mathrm{\frac{km}{h}}} \newcommand{\ms}{\,\mathrm{\frac{m}{s}}} \newcommand{\mss}{\,\mathrm{\frac{m}{s^2}}} \newcommand{\mmin}{\,\mathrm{\frac{m}{min}}} \newcommand{\smin}{\,\mathrm{\frac{s}{min}}} $$

Prijavi problem

Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem

Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

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:

  • Function pg.image.load loads the image from the disk. We pass the name of the file containing the image to this function (path to the file may be included), and as a result it returns the image in a format suitable for program. We should save that result in some variable (in the example it is the variable tree_image);

  • Function blit displays an image in a given window. Arguments of this function are a variable containing the image (result of function pg.image.load) and position \((x, y)\) in the window where we want the image to appear. At the position we set, the upper left corner of the image will appear. In the example, we set the position \((0, 0)\), so the upper left corner of the image appears in the upper left corner of the window.

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.


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).

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.