Analyze PyGame Example Project – – Part 2

In the previous post, I introduced the first part of the project It defines functions that would be called by main() function. In this post, I will introduce the main() function and wrap up the whole project. So let’s start now!

The main() function starts at line 148. It first defines several global variables because they will be used by both main() function and other functions. From line 159 to 162, the program sets font, window caption, window size and a clock, which are pretty common for all PyGame projects.

From line 164 to 167, the program uses “pg.scrap” libray to handle the operation related to clipboard. However, it seems the project could not recognize the text on the clipboard suceessfully, so I will just ignore this part of code.

From line 170 and 171, the program checks the command line arguments. Command line arguments are those values that are passed during calling of the program through command line shell application. Python provides various ways of dealing with these types of arguments. One common way is to use sys.argv, which is a simple list structure. The first element of the list sys.argv() is the name of the program itself. “sys.argv[1:]” contains all the music files passed in the command line and the program adds each of them at line 171.

At line 172, the program will play a default music file whenver the program runs. Function “play_file” has been introduced in the previous post. From line 175 to line 181, the program draws multiple lines of text and aligns them in the center of the window. Function “draw_line_text()” has also been explained in the previous post.

Starting from line 187, the program enters the main loop, captures and responds to different events. Besides those common event type such as QUIT and KEYDOWN, I would like to explain a few special event types which are not so commonly used. The event type of DROPFILE will be triggered when a file is dropped into a pygame application. The event’s attribute of file (ev.file) could get the file name being dropped.

There is a similar event type called “DROPTEXT” which could capture the text dropped into the Pygame application window. According to PyGame documentation, the DROPTEXT event is only supported on MAC X11. Since I am working on Windows 10 OS, I would ignore this part of code.

A third special event type is MUSIC_DONE. If you have read part 1 post for this project, you will notice that the program defines a custom event type “MUSIC_DONE” in line 137. It also calls “ (MUSIC_DONE)” in both “play_file” and “play_next” functions to hook the event type “MUSIC_DONE” with the music end event. Therefore, when a music file finishes playing, event type “MUSIC_DONE” is triggered.

From line 200 to 221, the program handles all the behaviours when Up, Down, Left or Right, Space, Return kyes are pressed. This part is not complicated, so I will not explain further.

From line 223 to line 228, the program adjusts the volume of the current music file. From line 230 to line 235, the program calls “pg.scrap.get()” method to get the text in the clipboard and checks if there is any change from the previous text. The purpose is to play the music file stored on the clipboard. However, I am using Windows 10 and could not verify this function, so just ignore this part.

At the end of the program, from line 236 to 239, the program does some clean up stuff. It updates all the display in the memory to the main surface by using “pg.display.flip()”. It sets “clock.tick(9)”. The frame rate of 9 is low enough so that CPU occupation of the application could be kept at a reasonable level.

That is the explanation for all this project. Although it is pretty light weighted, the project covers the topics of how to load and play music, adjust volume, forward and rewind the music. It also covers how to capture dragging and dropping behavior of mouse and how to define custom type events. Hopefully, you could learn useful stuff from this sample project. 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.

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 )

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.