Module 7 - Section 2 - Bitmap Graphics
Module 7 - Section 2 - Bitmap Graphics
- use an image as a background image in Pygame basic template
- create python code to load sprites
- superimpose sprites on top of the background image
- rewrite Python code to move the sprites using the keyboard or the mouse
7.2.3 - Display the background
Loading an image is a simple process and involves only one line of code. The command will load a file called background.jpg, store it to a variable called background_image and convert it to a format Pygame can more
easily work with. The file background.jpgmust be located in the same directory that the python program is in, or the computer will not find it:
background_image = pygame.image.load("background.jpg").convert()
Loading the image should be done before the main program loop. While it would be possible to load it in the main program loop, this would cause the program to fetch the image from the disk all the time. This is completely unnecessary. It is only necessary to do it once at program start-up.
To display the image use the blit command.
screen.blit(background_image, [0, 0])
Download the pygame basic template from here and save it inside the Documents folder Module7.2 using the name game7.4.2
In line 19 copy the first line that loads and converts the background image and in line 47 replace the existing line with the above blit command.
The final program can be downloaded from here.
If the program creates an error make sure that it is in the same folder with the file background.jpg.
Otherwise press the button and fix the error.