Skip to main content

A Unity-like 2D engine in Python using pygame

Project description

Dependencies

This package depends on the following libraries:

  • numpy (BSD)
  • moderngl (MIT)
  • pygame (LGPL)

Pygame is used for graphics, and is not modified in this package. Please see Pygame's license: LGPL v2.1.

To use gamengine2d, You must first use the Engine class

from gamengine2d import Engine, Color, Rectangle, vector2d # import the Engine Rectangle, vector2d, and Color classes

engine = Engine(width=500, height=500,           # Create the Engine object that opens a window of height 500, width 500,
                name="Unity2D", resizable=True,  # name Unity2D, and make the window resizable,
                background_color=Color.blue)   # set the background color to blue

# By default, the name is Unity2D, resizable is True, and the background color is Color.black()

rect = Rectangle(pos=vector2d(0, 0), size=vector2d(100, 400),  # Create a rectangle at 0, 0, with size 100, 400,
                 color=Color.red, rotation=10, visible=True, # and color red, and rotation 10 degrees, and make it visible,
                 name="MyRect")                                # and name it MyRect

# By default, the position is 0, 0, size is 40, 40, color is Color.white(), rotation is 0, visible is True, and name is rectangle

engine.add_object(rect) # add the rectangle object to the engine

engine.run(60, dynamic_view=True) # start the engine, and try to run at 60 fps, and dynamic view is True

# dynamic view allows the use to use the mouse wheel to zoom in or out, or left click and drag to pan around.
# by default, fps is 60, and dynamic view is True

To add a script to an object, use attach.

rect.attach("scripts/rectangle.py", engine.context) # attach the script rectangle.py to the rect object with the engine context

Then, in scripts/rectangle.py

class Rectangle: # The class name should be the same as the filename
    def __init__(self, obj, context): # get the object, and the engine context
        self.obj = obj # assign self.obj to obj
        self.context = context # assign self.context to context
    
    def update(self, dt): # the update method is called every frame, and takes dt which is delta time
        self.obj.rotation += 10 * dt # rotate the object at 10 degrees per second
        self.context.functions.draw_text(text=str(self.obj.rotation), # Draw the current rotation as text
                                         pos=vector2d(400, 300),      # at 400, 300
                                         color=Color.red,           # in red
                                         font_size=18)                # with font size 18

# By default, font_size is 18

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

gamengine2d-2.9.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gamengine2d-2.9.1-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file gamengine2d-2.9.1.tar.gz.

File metadata

  • Download URL: gamengine2d-2.9.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for gamengine2d-2.9.1.tar.gz
Algorithm Hash digest
SHA256 d702fc5983d251e0ff9b79c2396ed6217b6fbdc9a6e49e6997ad66eaf67650f3
MD5 b5c28ae5992d803a9c4ef7b8c37e74ac
BLAKE2b-256 1c0a73a039f36ee117dc45cade51bfef4cc67f65abb7466e7a3a793da89c0e39

See more details on using hashes here.

File details

Details for the file gamengine2d-2.9.1-py3-none-any.whl.

File metadata

  • Download URL: gamengine2d-2.9.1-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for gamengine2d-2.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 29208080f9de2357561d031d4816074c20a3bfa1059d868779983f54c9814438
MD5 fcc431e48c6aa493929e8fbab9c2955f
BLAKE2b-256 6d53a6f60048c1170da37aab84a1bbc4843dc22687b2021bdcbd8fdc2a6003e5

See more details on using hashes here.

Supported by

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