BaseCanvas is a small helper-class that handles common tasks in pygame.
BaseCanvas is a small helper-class that handles common tasks in pygame. I've built it because I'm constantly making side-projects, often related to simulations, data structures, little games and stuff and it's not pleasant to always have to paste boilerplate code just to get started. Feel free to contact me, for suggestions, complaints or anything, really.
To get started simply:
from base_canvas import BaseCanvas
Create a "Main Class" that inherits BaseCanvas
class Main(BaseCanvas): pass
Append the following at the end of the file in which your "Main Class" is:
if __name__ == "__main__": main = Main() main.loop()
When running your program from the command-line a few parameters can be passed:
- --width (an integer, if 0 fullscreen mode will be selected. Default to 800)
- --height (an integer, if 0 fullscreen mode will be selected. Default to 800)
- --fps (an integer. Default to 60)
The lifecycle of BaseCanvas is centered around inherited and overwritten hooks. They are 4 in total, each one with there own "responsibilities".
The setup hook (setup_hook) is the first one to be called, right after pygame is initiated. It's intended to do all sort of "variable initialization". It's called only once.
The init hook (init_hook) is the second one to be called. It's intended to do all sort of pre-loop logic, such as, initializing certain classes, doing a flow-check (should the program run like this or like that), etc. As with the setup hook, it's called only once.
The loop hook (loop_hook) is the third and main hook. It's where all of your main program logic should happen, things like: drawing to the canvas, collision check, etc. It's called at every frame, right after filling the main canvas with the "BACKGROUND_COLOR" and before calling "pygame.display.update()".
Handle events hook
The handle events hook (handle_events_hook) is the fourth and last hook. It's also the only hook that receives an argument, the event itself. It's called at every frame. The event (argument) is one element of the list returned by "pygame.event.get()". Keep in mind that some events are "handled" by default. They are:
João Pedro Braz, São Paulo - Brazil.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size base_canvas-0.0.4-py3-none-any.whl (6.4 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size base-canvas-0.0.4.tar.gz (3.5 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for base_canvas-0.0.4-py3-none-any.whl