A PyGame wrapper for pygame-ce
Project description
PyGameKit
Created as an Abstraction of Pygame to make development a bit easier for begineer Python Programmers.
Uses Object-Oriented Concepts to create an Abstraction.
Installation
Currently working on implementing it in PyPI. But for now, you can download the repo and copy over the PyGameKit to your Desired Location and just import it that way.
Documentation
Before we overview the functionality of PyGameKit. Here are some basic codes:
Simple Program to draw a Rect
import PyGameKit as pg
class Game(pg.Game):
def start(self):
self.x, self.y = 100, 100
self.color = (100, 200, 255)
def update(self):
pg.rect((self.x, self.y, 50, 50), self.color)
pg.update_screen()
Game((800, 600)).run()
Here, we are Inheriting from the PyGameKit Library's Game Class, Overriding the start and update functions, and then PyGameKit creates a game loop by itself through the run method down below. The Developer now doesn't have to remember as much boiler-plate code as before in pygame. If they have some knowledge in OOP, they can grasp this pretty easily.
Declaration of variables is simple. Declare all the variables you need in the start function using:
self.[variable_name] = value
This ensures that the variable is transferred over to the update function too.
The PyGameKit Game Class
This is basically the main class of PyGameKit. And this can take these arguments while being called.
screen_size: Specifies the screen size of the window.
vsync: Specifies if the window should have vsync or not. It's a boolean. By default it's False.
fullscreen: Specifies if the window should be fullscreen or not. It's a boolean. By default, it's False.
fps: Specifies the fps of the window. By default it's 60.
You can call it using:
import PyGameKit
class MyGame(PyGameKit.Game):
def update(self):
# [...] your code.
pass
MyGame( (800, 600), vsync=True, fullscreen=False, fps=120 ).run()
The Methods
start(): To be overriden. This will be called for one frame once the program has been run. Can be used to declare variables.
update(): To be overriden. This is the main game loop. All of the main code shall be written here.
run(): This function should be called to actually run the program.
Key Features
delayed_keypress
This function checks if a specified key is pressed after a certain delay. This can be useful in many scenarios where the normal keypress function is outputting too fast. You can specify your own delay here.
from PyGameKit import delayed_keypress
if delayed_keypress('SPACE', 500): # Check if SPACE key is pressed after a delay of 500 milliseconds
# Your code here
delayed_mouse_press
Same thing as the delayed_keypress but for mouse presses.
from PyGameKit import delayed_mouse_pressed
if delayed_mouse_pressed(500)[0]: # Check if the left mouse button is pressed after a delay of 500 milliseconds
# Your code here
Displaying Images is easier now
Now, all the images loaded in PyGameKit will normally follow the rect behaviour. It uses pygame.transform.smoothscale to fit the image into the given rect.
# This is the same as example.py
import PyGameKit as pg
class MyGame(pg.Game):
def start(self):
pg.title("Tests")
self.catx = 0
self.caty = 0
def update(self): # Game Loop
pg.background((0, 0, 0))
cat = pg.load_image("cat.png")
mx, my = pg.mouse_getpos()
mb = pg.mouse_pressed()
if mb[0]:
self.catx, self.caty = mx - (200 / 2), my - (200 / 2)
pg.display_image(cat, (self.catx, self.caty, 200, 200))
pg.update_screen()
MyGame((800, 600), True).run()
Compatibility with PyGame
You can write the same kind of PyGame Programs with PyGameKit with no problems.
import PyGameKit as pg
screen = pg.display.set_mode((800, 600))
while True:
for event in pg.events.get():
if event.type == pg.QUIT:
pg.quit()
exit()
pg.draw.rect((100, 100, 200, 200), (100, 200, 255))
And this works as intented.
PyGameKit Functions, their Functionalities and their Examples.
Screen Functions
title
Changes the title of the Game Window.By Default it is: PyGameKit Window.
# [...]
PyGameKit.title("Example")
# [...]
update_screen
Equivalent to pygame.display.flip()Makes the screen visible.
# [...]
PyGameKit.update_screen()
# [...]
background
Argument list: color, screen(optional)
Changes the background of the screen to a certain color. The color is the same as pygame.PyGameKit.background( (0, 0, 0) ) # Black Background
Drawing Functions
line
Argument list: start, end, color, width, screen(optional)
Draws a line from start to end with width.
PyGameKit.line( (100, 100), (200, 200), (100, 200, 255), 5, surface ) # Draws a blue line from 100, 100 to 200, 200 on the screen called 'surface'
rect
Argument list: rect, color, corner_radius, width, screen(optional)
Draws a rect with the given color, corner_radius and width
Note: If width is 0, then the width argument is ignored.
PyGameKit.rect( (100, 100, 200, 200), (100, 200, 255), 5, 10)
circle
Argument list: center, radius, color, screen(optional)
Draws a circle in the center co-ordinate with the radius and color.
PyGameKit.circle( (100, 100), 50, (100, 200, 255) )
ellipse
Argument list: rect, color, screeen(optional)
Draws an ellipse around the rect with the given color.
PyGameKit.ellipse(( 100, 100, 200, 300 ), (100, 200, 255))
arc
Argument List: rect, start, stop, color, screen(optional)
PyGameKit.arc( (100, 100, 200, 200), 0, 180 )
Imaging Functions
load_image
Argument list: filename, colorkey
Loads the image and stores it into the given variable. If a color key is given, returns a transparent image.
img = PyGameKit.load_image("cat.png")
display_image
Argument list: surface, rect, screen(optional)
Displays the before-mentioned image on a rect. It can also display any surface as required.
img = PyGameKit.load_image("cat.png")
PyGameKit.display_image(img, (100, 100, 200, 200)) # <- The rect can also have two arguments, in which case it won't be scaled.
Transform Functions
scale
Argument list: surface, size
Scales the surface into the required size. This is the non-smooth variant.img = PyGameKit.load_image("cat.png")
img = PyGameKit.scale(img, (200, 200))
PyGameKit.display_image(img, (100, 100))
smoothscale
Argument list: surface, size
Scales the surface into the required size. This is the smooth variant.img = PyGameKit.load_image("cat.png")
img = PyGameKit.smoothscale(img, (200, 200))
PyGameKit.display_image(img, (100, 100))
rotate
Argument list: surface, angle
Rotates the surface into the specified angle. The angle is in degrees.img = PyGameKit.load_image("cat.png")
img = PyGameKit.rotate(img, 180)
PyGameKit.display_image(img, (100, 100, 200, 200))
flip
Argument list: surface, flip_x, flip_y
Flips the image according to the booleans flip_x and flip_y.img = PyGameKit.load_image("cat.png")
img = PyGameKit.flip(img, flip_x=True, flip_y=True)
PyGameKit.display_image(img, (100, 100, 200, 200))
Key Handling Functions
keypress
Argument list: keycode
Checks if the keycode is pressed or not and correspondingly returns True or False.if PyGameKit.keypress("W"):
# [...] your code
The Key Codes are all same to Pygame but with the removal of 'K_'. For Example, The Escape Key will be ESC as opposed to 'K_ESC'. Also, no need to worry about upper and lower case because PyGameKit automatically sets it to lowercase.
delayed_keypress
Argument list: keycode, delay
Same Functionality as keypress but with a specified delay. The delay is in milliseconds.if PyGameKit.delayed_keypress("W", 200): # Checks for keypress every 200 ms
# [...] your code
Mouse Handling Functions
mouse_getpos
returns both x and y positions of the mouse in the screen.
mx, my = PyGameKit.mouse_getpos()
mouse_pressed
returns a tuple (left: bool, middle: bool, right: bool). If left is True, the left mouse button has been pressed, if middle is true then the middle mouse button is pressed and if right is True then the right mouse button is pressed.
mb = PyGameKit.mouse_pressed()
if mb[0]:
# The Left Mouse has been clicked.
pass
delayed_mouse_press
Argument list: delay
Same thing as mouse_pressed but adds a specified delay to the function. The delay is in milliseconds.mb = PyGameKit.delayed_mouse_press(200)
if mb[2]:
# The Right Mouse has been clicked.
pass
mouse_mov
Equivalent to pygame.mouse.get_rel(). Returns the amount of movement in the mouse from the last time this function was called.
mv = PyGameKit.mouse_mov()
mouse_setpos
Argument list: co_ordinates
Sets the mouse position to the specified co-ordinates.PyGameKit.mouse_setpos((200, 200)) # Don't do this lmao
mouse_setvis
Argument list: value
Sets the visiblity of the mouse. If False, it will make the mouse invisible inside the window.PyGameKit.mouse_setvis(False)
mouse_getvis
Gets the visiblity of the mouse. If False, the mouse is invisible and if True, the mouse is visible.
mouse_visibility = PyGameKit.mouse_getvis()
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file PyGameKit-0.2.tar.gz.
File metadata
- Download URL: PyGameKit-0.2.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
793effbf92e21ff832f0aa8ff955f4978ee386e7ed78d9f6f0b14ef125fa4d2a
|
|
| MD5 |
73814128a36799a2746cbc35cf1de441
|
|
| BLAKE2b-256 |
cdec3c997f67944246d77b998b21055f14f2ed5fd16c86bf7c08f2be0d6b8acc
|
File details
Details for the file PyGameKit-0.2-py3-none-any.whl.
File metadata
- Download URL: PyGameKit-0.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
053946b13d8fb1989e67faba912204c0c63fbf5ab136a6c12ebb7330c217f8df
|
|
| MD5 |
9eea97bf4861db13af83e7da72dadd75
|
|
| BLAKE2b-256 |
b01b1df138137250a09c74fdb882fc46d0789e2f9d0ad868b4968d2b05cea7f7
|