Analyze PyGame Example Project – – Part 1

In this post, I would like to analyze another Python sample project. Its function is to play the music file when dragging and dropping them to the project window. The program could also respond to different keys to change volume, play forward or rewind. Those functions have being listed at the header section of the file, as shown below.

From line 24 to 44, the program defines a function called “add_file”. It receives an input argument “filename”. At line 30, “filename.rpartition[“.”]” searches for the last occurrance of the char “.” and returns a tuple of three elements. Element at index 0 represents everything before the char “.”, Element at index 1 represents the char “.” and element at index 2 represents everything after the char “.”. Therefore, expression “filename.rpartition(“.”)[2].lower” gets the extension name of the file and checks if the file type belongs to any of music_file_types. Only the music file type is regarded as a valid one.

From line 34 to 39, the program considers several conditions under which the file is stored on the computer. If the input argument filename is offered as an absolute complete path, code in line 34 could find the file successfully. However, if the input filename is just a short file name, the program will search main_dir or data_dir to check if the file exists there.

After adding file to music_file_list, the next function “play_file” defines how to play the music. It first calls function “add_file” successfully, then calls “” method to load the music into memory. The program uses “try… except… ” structure to ensure the input music file is a valid one. If any exception occurs, the program removes the music file from music_file_list and then exits the function.

At line 65, the program begins playing the music. “fade_ms” is an optional integer argument, which is 0 by default, makes the music start playing at 0 volume and fade up to full volume over the given time 4000 ms (4 secs.).

From line 68 to line 73, the program checks if the music type belongs to any one in music_can_seek. If it does, it means the music type supports playing in the specified position, so set variable “starting_pos” to 0, otherwise, set variable starting_pos to -1.

From line 77 to line 102, the program defines a function called “play_next”. It means to play the next music file in music_file_list. It first checks if the music_file_list contains more than one music file. If it does, the program gets the first item in the list (.pop(0)) at line 84, and then the popped item is appended to the end of the music_file_list at line 91. It is because the files in music_file_list play in a loop mode. At line 95 and 96, the program plays the music and sets the volume.

At line 97, method “” creates a custom type event name called “MUSIC_DONE”. This new event will cause pygame to signal (by means of the event queue) when the music is done playing. This event will be checked and caught in the PyGame main loop.

The next function “draw_text_line” is defined to draw text on the main surface. Please note that the expression of x indicates that those strings are aligned in the center. The input argument y represents the y position of previous line text, through using “y += surf.get_height()”, the newly added text will be blit at the correct position.

From line 119 to line 134, the program defines a function called “change_music_position”. This function only works with OGG and MP3 file types. In previous functions “play_file” and “play_next”, we set the variable “starting_pos” to either value 0 or -1. If the variable value is equal to -1, the music file does not support playback position.

Here at line 129, the program checks if variable “starting_pos” bigger or equal to 0. If it does, the music file supports playback position. “” function gets the number of milliseconds that the music has been playing for. The returned time only represents how long the music has been playing; it does not take into account any starting position offsets. Through adding starting_pos and “”, we could get the current playback position. After adding an offset amount, the music would start playing at an updated position.

The above sections are all about the functions defined in this program file. From line 137 to line 145, the program defines some global variables which are used by different functions. After line 145, we will define main() function and enter PyGame main loop. Keep tuning on us and I will introduce the main loop in the next post. Enjoy the coding and have fun!

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

One thought on “Analyze PyGame Example Project – – Part 1

  1. Pingback: Analyze PyGame Example Project – – Part 2 – The Coding Fun

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.