The idea of this manual is to introduce the reader to the kelboy-launcher and its internal workings. All the code is posted on the Lemoncrest github and you can feel free to fork and pull requests. In fact we are looking forward to your help in developing this little program.
This small article is the continuation of the previous article, so if you have not taken a look, we recommend reading it.
Everything that is programmed in python is a script, so when we talk about our program we will talk about scripts. In this case we will talk about how the parent script of the user interface works, the main.py
Every main indicates the start and the script we are talking about has a class called Main() and an initial function called run()
Within run we can find the parent thread, the initial idea is that it was the parent of everything that was created, but to close the stable version its operation has been modified to perform asynchronous actions.
These functions are responsible for:
- Capture kelboy controller inputs (events)
- Perform repetitive actions, such as holding down the up and down keys (moves)
- Draw an interface and run the relevant chord commands (async_run)
Each of them has a separate thread with a separate function.
Thread events (also moves)
This function is in charge of receiving the input events (arduino controller, joysticks, mouse ...) and transforming them into orders.
Initially there were only events but due to a request it was complemented with the moves method, so events is a moves handler. This means that it extends from another thread the functionalities that have been exposed (this way the programming of keeping a button pressed is done in a way with fewer lines of code, but it could be perfectly left within the events method).
This function runs for the duration of the program and waits until an event occurs. The events handled are the following:
- pygame.QUIT - It happens when you close the program for example with the X of the window
- pygame.KEYDOWN - It occurs at the moment of pressing a key down. This key must belong to a keyboard.
- pygame.KEYUP - The opposite of the previous one, it only happens when you release a keyboard key.
- pygame.JOYBUTTONDOWN - Same as pygame.KEYDOWN but only with game controllers (the kelboy controller)
- pygame.JOYBUTTONUP - Same as pygame.KEYUP but with game controllers.
- pygame.JOYAXISMOTION - Joystick movement exclusively type events.
Thanks to these controllers, basic actions can be launched that will be defined according to the programmed logic. This logic tends to use menus that are built with the menu.py script. This script contains six basic methods launched from the capture of these events (in addition to containing the way to work and interpret the orders at all times), with those corresponding to the calls:
With these 6 functions, the launcher has the basic inputs so that the program knows how to interpret how and what menus to draw.
The async_run thread
This is the thread that takes care of all the logic management in the launcher.
As you can see, it has a loop in charge of drawing pygame style, which also counts the time of the screensaver and draws the scenes. If you have been attentive in other manuals of the learn part, you will understand how a sprite works. If not, you can take a look at the two manuals on howto program videogames currently published in the Learn section.
The kelboy-launcher is programmed with a main component, menu.py. This component is in charge of managing the menus that are stipulated in configuration files easily editable by each user (it does not require knowing how to program to edit a menu).
But we will discuss that in other article.