BaseCanvas is a small helper-class that handles common tasks in pygame.
Project description
BaseCanvas
Description
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.
Use
To get started simply:
-
Import BaseCanvas
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)
lifecycle
The lifecycle of BaseCanvas is centered around inherited and overwritten hooks. They are 4 in total, each one with there own "responsibilities".
Setup hook
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.
Init hook
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.
Loop hook
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:
- pygame.QUIT
- pygame.KEYDOWN
- pygame.K_ESCAPE
- pygame.K_F11
- pygame.VIDEORESIZE
Author
João Pedro Braz, São Paulo - Brazil.
- Email: brazjoaopedro@ymail.com
- Linkedin: João Pedro Braz
- Github: Repositories
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file base-canvas-1.0.6.tar.gz
.
File metadata
- Download URL: base-canvas-1.0.6.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ea5d4729ab11943dd2053030147aa9c42e77dbf50982bc3e940698e70305a50 |
|
MD5 | 6c529aec1dfa0f3b6ea2a10d1637fff6 |
|
BLAKE2b-256 | 8d58c39344c8f4d2d0b2843b0cb33887fa07b334c03787a18caa6adfedbf19ce |
File details
Details for the file base_canvas-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: base_canvas-1.0.6-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21d0edf675a37bf176d38cac9cad5369e1b9d70f05dc601bbf17bff90c92b69f |
|
MD5 | 0a7be4a1ea5a8dc39305c7e3605586c4 |
|
BLAKE2b-256 | 6535151d99db41c0c79f66611537734f516c68a6636394dade04785899c09200 |