Skip to main content

A library that improves pygame and makes it structured.

Project description

PyGameX

Description

An easy-to-learn library that makes Pygame more structured and user-friendly. The library has many built-in functions that simplify development at times.

Usage/Examples

Empty template

import PygameX as pygamex

class MainGame(Game):
    pass

MainGame(height=300, width=500, max_fps=60)

Simple game

import PygameX

from PygameX.game import *
from PygameX.all_objects import *

from random import randint


class MainGame(Game):
    background_color = color.BLUE
    object_render_mode = True

    def ready(self):
        player = Circle(
            position=(50, 50),
            radius=25,
            color=(0, 255, 0)
        )
        self.objects["player"] = player

    def on_event(self, event):
        super().on_event(event)

    def on_mouse_pressed(self, mouse):
        if mouse[1] == 1:
            if mouse[2]:
                player = self.objects["player"]
                if PygameX.math.is_point_inside_circle(mouse[0], player):
                    player.color = (randint(0, 255), randint(0, 255), randint(0, 255))

    def update(self):
        player = self.objects["player"]
        keys = PygameX.key.get_pressed() # List of pressed keys
        if keys[PygameX.key.LEFT]:
            player.position = (player.position[0] - 3, player.position[1])
        if keys[PygameX.key.RIGHT]:
            player.position = (player.position[0] + 3, player.position[1])
        if keys[PygameX.key.UP]:
            player.position = (player.position[0], player.position[1] - 3)
        if keys[PygameX.key.DOWN]:
            player.position = (player.position[0], player.position[1] + 3)


MainGame(width=500, height=300, max_fps=60)

API Reference

Game class init

class MainGame(Game):
    pass

MainGame(width=500, height=300, caption="My first PagameX game!", max_fps=60)
Parameters Type Description
width int Sets the width of the screen
height int Sets the height of the screen
caption string Sets the title of the game
max_fps int Sets the max frame rate of the game process

Game class methods

Ready

def ready(self):
    pass

Called when the game window has been created and started.

Init

def init(self):
    pass

Called before starting the game and initializing pygame.

On quit

def on_quit(self):
    pass

Called when the game is closed.

Quit

quit()

Completes the game cycle.

Update

def update(self):
    pass

Called every time, before rendering and processing the keys.

Render

def render(self):
    pass

Called every time to draw objects.

On event (event)

def on_event(self, event):
    pass

Called every time an interaction event occurs (Includes keystroke events).

Parameter Type Description
event pygame event Pygame event about a pressed key or a set of keys and its status

On mouse pressed (key)

def on_mouse_pressed(self, mouse):
    pass

Called every time an interaction event occurs.

Parameter Type Description
mouse tuple An immutable list containing the position: tuple, pressed key: pygame key, is pressed: boolean

Object render mode

PygameX adds the ability to use objects instead of manually drawing shapes.

To enable this mode, just set object_render_mode to True in your game class.

object_render_mode = True

All that remains to be done is to add objects to the objects dictionary.

Object

Object is the main class of all objects in PygameX, it is usually not used in games because it is a dummy that exists to create other objects such as line, circle, rect.

It has a render function for displaying an object on the game screen.

Rect

The object inheriting the Object having 3 settings: position size color.

Initializing template:

Rect(
    position=(50,50),
    size=(25,25),
    color=(0,255,0)
)

Circle

The object inheriting the Object having 3 settings: position radius color.

Initializing template:

Circle(
    position=(50,50),
    radius=25,
    color=(0,255,0)
)

Line

The object inheriting the Object having 3 settings: point1 point2 color.

Initializing template:

Line(
    point1=(50,50),
    point2=(25,25),
    color=(0,255,0)
)

Creating your own object

Any objects should inherit Object because it contains the render function and is simply more convenient than creating a new class manually. As a result, the dummy of the new class will look like this:

import pygame
from .Object import Object

class MyCustomObject(Object):

    def __init__(self):
        pass
    
    def render(self, screen):
        pass

Let's add the variables position and color inside.

import pygame
from .Object import Object

class MyCustomObject(Object):

    position = (0,0)
    color = (0,0,0)
    
    def __init__(self, position: tuple = (0,0), color: tuple = (0,0,0)):
        self.position = position
        self.color = color
    
    def render(self, screen):
        pass

Now in the render method we will draw our object. In the example, a 50x50 square is simply drawn using our values.

import pygame
from .Object import Object

class MyCustomObject(Object):

    position = (0,0)
    color = (0,0,0)
    
    def __init__(self, position: tuple = (0,0), color: tuple = (0,0,0)):
        self.position = position
        self.color = color
    
    def render(self, screen):
        pygame.draw.rect(screen, self.color, (self.position, (50, 50)))

Now let's try out our object in the game. Let's replace the circle from the example above with our object First, let's save our script in MyCustomObject, and then import it into the game.

import PygameX

from PygameX.game import *
from PygameX.all_objects import *
from .MyCustomObject import MyCustomObject # I immediately import the object

from random import randint


class MainGame(Game):
    background_color = color.BLUE
    object_render_mode = True

    def ready(self):
        player = MyCustomObject(
            position=(50, 50),
            radius=25,
            color=(0, 255, 0)
        )
        self.objects["player"] = player

    def on_event(self, event):
        super().on_event(event)

    def on_mouse_pressed(self, mouse):
        if mouse[1] == 1:
            if mouse[2]:
                player = self.objects["player"]
                if PygameX.math.is_point_inside_circle(mouse[0], player):
                    player.color = (randint(0, 255), randint(0, 255), randint(0, 255))

    def update(self):
        player = self.objects["player"]
        keys = PygameX.key.get_pressed()
        if keys[PygameX.key.LEFT]:
            player.position = (player.position[0] - 3, player.position[1])
        if keys[PygameX.key.RIGHT]:
            player.position = (player.position[0] + 3, player.position[1])
        if keys[PygameX.key.UP]:
            player.position = (player.position[0], player.position[1] - 3)
        if keys[PygameX.key.DOWN]:
            player.position = (player.position[0], player.position[1] + 3)


MainGame(width=500, height=300, max_fps=60)

Scripts

The library provides a quick import of all objects from the library.

from PygameX.all_objects import *

If you don't need to import all the objects, you can import them individually.

from .objects.Rect import Rect # Rect
from .objects.Circle import Circle # Circle
from .objects.Line import Line # Line
from .objects.TextDisplay import TextDisplay # Text

Convenient functions

PygameX provides functions for working with objects quickly.

List of additional functions:

is_point_inside_circle(point: tuple, circle: Circle)

PygameX.math.is_point_inside_circle(mouse_position, my_circle)

is_point_inside_rect(point: tuple, rect: Rect)

PygameX.math.is_point_inside_rect(mouse_position, my_rect)

Installation

Install my library using pip in CMD or PyCharm Console:

pip install PygameX

Authors

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

pygamex-0.1.3.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

PygameX-0.1.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file pygamex-0.1.3.tar.gz.

File metadata

  • Download URL: pygamex-0.1.3.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for pygamex-0.1.3.tar.gz
Algorithm Hash digest
SHA256 392f7e48e2119ae998a72d95d2104f9acc792e6ea7d1910881e20e9813302480
MD5 001ce05c878d67f6af538aac2a5df5f5
BLAKE2b-256 723ff95a867fb08e79b0b672006b23dc1a040a86e462c05bfa25364957ff73fb

See more details on using hashes here.

File details

Details for the file PygameX-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: PygameX-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for PygameX-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e1ccb882504f3f83b1d8034919571c2b9a663e89886be6b50a44374a188f9dd
MD5 5ad129f6306394dffb5cb0084492f5a3
BLAKE2b-256 97a2a18488092b444e5858611b5d5246aea88052f17e81be755cf56cc07bb012

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