Skip to main content

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:

  1. Import BaseCanvas

     from base_canvas import BaseCanvas
    
  2. Create a "Main Class" that inherits BaseCanvas

     class Main(BaseCanvas):
         pass
    
  3. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

base-canvas-1.0.2.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

base_canvas-1.0.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file base-canvas-1.0.2.tar.gz.

File metadata

  • Download URL: base-canvas-1.0.2.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for base-canvas-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8222e67b945d413941f99cb4d5d44a07e263ca5036952900a551628e0c3440d5
MD5 4b828c5f0689ce15a831c59b7d3c2b12
BLAKE2b-256 eb95f12b02e8a8fb35249fdf731e64543a17a2909e5fd0b7e63c8af87b9de5a4

See more details on using hashes here.

File details

Details for the file base_canvas-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: base_canvas-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for base_canvas-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3203d73b4af66cf8abf38495550ade8f241258dab593747b0ee3098b7beab047
MD5 52d7021315cc5f5f0a066ed3d2dfd45e
BLAKE2b-256 9a13dfe5f00493faeb8f6c26b380821b6ff9b7220dbd21911e374e48c4b6f66e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page