Part 11 - Bitmapped Graphics and Sounds

This section will show you how to use pre-existing image files of various formats in your program, as either a background, or an animated item. It will also show you how to add text and sounds/music to your programs.

What to do/hand in?

1. Read and work through "Part 11 - Bitmapped Graphics and Sound". You may also use Program Arcade Games Chapter 11 to help.

2. Complete the Part F assignment.

Submit:

1. Part G: Imported Graphics and Sound program.

Try to complete the challenges. See the Hints and FAQ section for some help if you are stuck.

Part 11 - Bitmapped Graphics and Sound.pdf

Hints and FAQ's

Q: I have a jpg with a white background and the image.set_colorkey(WHITE) isn't making the entire white background transparent. Why?

A: Often it will appear that a background color in a jpg is all one uniform color, but it is not actually the case. This is a result of the algorithm that is used to reduce the size of jpg image files.

Open your file up in an image editing program and make sure the background is all one uniform color. Or make the background transparent and save it in a suitable format (i recommend png or bmp).

Q: I want to re-size my image. How do I do that?

A: You can use the following line of code to re-size an image:

player_image = pygame.transform.scale(player_image, (width, height))

Q: I tried to re-size my image several times in a row. The image is getting distorted and I now have a mutant character. Why is this happening and how can I fix it?

A: The scale function isn't perfect. You just don't notice with one or two re-scales. However, the more you perform a scaling, the more the imperfections are apparent. In order to fix this, each time you use the pygame.transform.scale() function, you must first re-load the image so you are starting with a perfect image before each scale.

Here is an example:

         player_image = pygame.image.load("filename").convert()
         playerImage = pygame.transform.scale(player_image, (width, height))

This works, but can also have a bit of a negative impact on performance. Make sure that you only load the image when absolutely necessary, and not all of the time.

Q: In part D we played sounds using pygame.mixer.sound and pygame.mixer.music. What is the difference? How do I know which one to use?

A: In short, you should be using the pygame.mixer.sound to play sound effects, and smaller audio files, and the pygame.mixer.music to play larger sound files, like background music. Here is some more information about each:

pygame.mixer.sound                                   

1. Only loads sounds from OGG files and uncompressed WAV files . Uncompressed WAV files can get really large for even short soundtracks. Use Audacity or an online converter to turn your audio files to OGG files for smaller file sizes.

2. Can play multiple sounds at the same time.

3. Loads all audio files into memory when program is first run. This can cause performance issues if you are loading many large sound files.

4. Use this for short sound effects.

5. MP3 files do not work.

pygame.mixer.music

1. Streams audio files as program is running. If you have a large audio file as a background music, use this method. This will help with performance and filesize (a short mp3 file can become hundreds of MB when turned into an uncompressed WAV file).

2. Can only play one sound at a time. (In addition to sounds being played with pygame.mixer.sound)

3. Use this for longer background music.

4. There is very limited support for MP3 files.