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 .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
Built Distribution
File details
Details for the file pygamex-0.1.5.tar.gz
.
File metadata
- Download URL: pygamex-0.1.5.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b883a8593da84d7be2a2c3c9e041b2cc8b3d79915e8ad150cf91fa64b4644788 |
|
MD5 | 4781ba94de692655cc345ad6d9d859f0 |
|
BLAKE2b-256 | e14993f5bc9efc05b3ebf77f7ef8dff2bb153c7e188dfc0e6d1808dc1793ca2e |
File details
Details for the file PygameX-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: PygameX-0.1.5-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01235baa3bc369a21b0f3f4e8a8fb889fa74f5c1c897dfa87bbfd2fb49f73ee2 |
|
MD5 | 1451f78bdc3314d3c14ea5a3d612aa57 |
|
BLAKE2b-256 | 7247c55734f5c249bc547e81b91716c44e972c579c09c162c137093ee3d39eba |