Welcome to PyJay!


Table of Contents

Map Arguments
Functions
Other requirements
Example

Interactable Arguments
Functions
Other requirements
Example

Button Arguments
Functions
Example

Particle Arguments
Functions
Example

Animation Arguments
Functions
Example

Announcement Arguments
Functions
Example

Data management Arguments
Functions
Example


pyJay.map()

Required arguments:

pyJay.map(map_info)

Optional arguments:

pyJay.map(map_info,collider,map_x,map_y,extension)

Syntax:

map_info: (str):
- A path to a '.txt' file containing the information for loading the map
collider: (bool):
- Allows you to get hitboxes from the map tiles (default False)
map_x: (int):
- Starting position of the map on the x-axis (default 0)
map_y: (int):
- Starting position of the map on the y-axis (default 0)
extension: (str):
- The extension of your images (default '.png')


pyJay.map() functions

display(surface,display_box)

Displays the map to the surface you put into the surface argument. Does not display if you set collider to True.

display_box is a rect around the screen. PyJay will only draw images that are within the display box to reduce lag.

move(x,y)

Moves the entire map by the number of pixels specified in the x-axis and the y-axis

collide()

Returns a list of rects that you can use as hitboxes to collide with. Only works when collider is set to True


What else to do

Each map will need a map data file. This will be a '.txt' file and contain four (4) lines of text. The first two lines are paths (str) and the last two are numbers (int).

    	        
path/to/image_set/folder
path/to/map_layout/file.txt
tile_size
scale
    	    

path/to/image_set/folder
- This will be a path to a folder containing all the images you need for that map.
path/to/map_layout/file
- This will be a path to the layout of your map. This file needs to follow a few rules as well.
map_layout rules:
- Each character corresponds to an image name in the path/to/image_set/folder (ex: '1' on the map will display '1.png')
- Each tile may only correspond to ONE character in the map_layout
- You cannot use the 0 character.
- The 0 character will display a blank tile.
tile_size
- This is a number (int) that represents the size of the image. (ex: A 32x32 image will be a value of 32 in this line)
scale
- This will scale all tiles by multiplying the image size by this amount (ex: 1 will keep it the same size, and 2 will double the size)

Each map will also need a map_layout file. This will be a '.txt' file and be the "key" that pyJay uses. Make each number correspond to an image tile.

When collider is set to True, every tile that doesn't have a value of 0 will create a hitbox.

	            
1222223
4FFFFF5
4FFFFF5
6777778
	       

Example:

File system:

	           
-> root
	main.py
	pyJay.py
	-> img
		-> basement
			1.png
			2.png
			3.png
		-> floor_one
			A.jpg
			B.jpg
			C.jpg
	-> map_data
		basement_data.txt
		floor_one_data.txt
	-> map_layouts
		basement.txt
		floor_one.txt
	       

main.py

	           
import pygame, sys

#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()

#Define two maps using pyJay
#This one just uses the defaults
basement = pyJay.map('map_data/basement_data.txt')

#This one uses '.jpg' images, so we have to pass all the arguments
floor_one = pyJay.map('map_data/floor_one.txt',False,0,0,'.jpg')

#Game loop
while True:
	screen.fill((0,0,0))
	
	#Make display box around entire screen
	display_box = pygame.Rect((0,0),(screen.get_width(),screen.get_height()))
	
	#Display the basement map
	basement.display(screen,display_box)
	
	for event in pygame.event.get():
		if event.type == pygame.KEYDOWN:
			#Move the map when the arrow keys are pressed
			if event.key == pygame.K_UP:
				basement.move(0,-5)
			if event.key == pygame.K_DOWN:
				basement.move(0,5)
			if event.key == pygame.K_LEFT:
				basement.move(-5,0)
			if event.key == pygame.K_RIGHT:
				basement.move(5,0)
	
	pygame.display.flip()
	clock.tick(60)
	       

basement_data.txt

	           
img/basement/
map_layouts/basement.txt
32
2
	       

floor_one_data.txt

	           
img/floor_one/
map_layouts/floor_one.txt
16
4
           

basement.txt

               
11111
12221
12021
12221
11311
           

floor_one.txt

               
AAAAA
ABBBA
AB0BA
ABBBA
AACAA
           


pyJay.interactable()

Required arguments:

pyJay.interactable(file)

Syntax:

file: (str):
- A path to the configuration file


pyJay.interactable() functions

collide(plRect)

Check to see if the plRect (pygame Rect object) collides with an interactable tile

move(x,y)

Move all interactables along the x and y axis (in pixels)

display(screen)

Draw the hitboxes to the suface passed through screen


What else to do

Instead of individually defining hitboxes in the space, PyJay uses something simmilar to the way maps are made. You will need a data file and a layout file

The configuration file will need to be a .json and contain the following things:

                
{
    "objects": [
        {
            "key": "1",
            "function": "function_when_collide(1)"
        },
        {
            "key": "2",
            "function": "function_when_collide(2)"
        }
    ],
    "map_data": {
        "file": "path/to/map_layout.txt",
        "width": tile_width,
        "height": tile_height
    }
}
            

objects
This is a list containing JSON objects to controll interactable tiles
- - key
- - - This is what character on the layout file does what function
- - function
- - - This is what will run when collided with the key tile. This should be a Python function (which is defined in your main.py)

map_data
This contains information about how to display your interactables
- - file
- - - This is a path to your map layout file
- - width
- - - This is the width of each tile
- - height
- - - This is the height of each tile

You will also need to make a map layout. This follows the same rules as the pyJay.map() rules


Example:

File system:

                
-> root
    main.py
    pyJay.py
    -> layouts
        map_layout.txt
    -> interactable
        config.json
            

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()

#define an interactable map
interact = pyJay.interactable('interactable/config.json')

#Define something to happen when you hit an interactable
def quit():
    pygame.quit()

#game loop
while True:
    screen.fill((0,0,0))
	
	#display hitboxes for debugging
	interact.display(screen)
	
	for event in pygame.event.get():
		if event.type == pygame.KEYDOWN:
			#Move the map when the arrow keys are pressed
			if event.key == pygame.K_UP:
				interact.move(0,-5)
			if event.key == pygame.K_DOWN:
				interact.move(0,5)
			if event.key == pygame.K_LEFT:
				interact.move(-5,0)
			if event.key == pygame.K_RIGHT:
				interact.move(5,0)
	
	pygame.display.flip()
	clock.tick(60)
            

config.json


{
    "objects": [
        {
            "key": "1",
            "function": "quit()"
        }
    ],
    "map_data": {
        "file": "layouts/map_layout.txt",
        "width": 64,
        "height": 64
    }
}
            

map_layout.txt

                
0000000
0000000
0001000
0000000
0000000
            


pyJay.button()

Required arguments:

pyJay.button(x,y,text)

Syntax:

x: (int):
- The x location for the center of the button
y: (int):
- The y location for the center of the button
text: (str):
- The text the button displays


pyJay.button() functions

display(surface)

Displays the button to the surface you put into the surface argument

get_click(mouse_position)

Returns True if the mouse_position (tuple) is colliding with the button.

TIP: Pass pygame.mouse.get_pos() into the mouse_position argument


Example:

File system:

                
-> root
	main.py
	pyJay.py
	        

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()

#Define a button using pyJay
exit_button = pyJay.button(350,350,'Exit Game')

#Game loop
while True:
	screen.fill((0,0,0))
	
	#Display the button to the screen
	exit_button.display(screen)
	
	for event in pygame.event.get():
		if event.type == pygame.MOUSEBUTTONDOWN:
			#Check to see if button was clicked
			if exit_button.get_click(pygame.mouse.get_pos()):
				pygame.quit()
				sys.exit()
	
	pygame.display.flip()
	clock.tick(60)
	        


pyJay.particle()

Required arguments:

pyJay.particle(startX,startY,color,size,speed)

Optional arguments:

pyJay.particle(startX,startY,color,size,speed,lifetime)

Syntax:

startX: (int):
- The x location the particles emmit from
startY: (int):
- The y location the partciles emmit from
color: (tuple):
- RGB value of the particle color
size: (int):
- The size (in pixels) of each particle
speed: (int):
- How fast (in pixels per frame) the particle moves
lifetime: (int):
- How long (in frames) the particles may last up to (Default 60)


pyJay.particle() functions

move()

Moves the particles. Direction is automatically calculated

lifetime_check()

Returns True if a particle's lifetime is over

display(screen)

Displays the particle to the surface passed


Example:

File system:

                
-> root
	main.py
	pyJay.py
	        

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()

#Make a list containing multiple particles to make a particle effect!
particles =  []
for i in range(0,20):
	par = pyJay.particle(50,50,(255,255,0),5,2)
	particles.append(par)

#Game loop
while True:
	screen.fill((0,0,0))
	
	#Display the button to the screen
	exit_button.display(screen)
	
	for particle in particles:
		#Move every particle
		particle.move()
		#Display every parrticle
		particle.display(screen)
		#Delete particle if it's lifetime is over
		if particle.lifetime_check():
			particles.remove(particle)

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
	
	pygame.display.flip()
	clock.tick(60)
	        


pyJay.animation()

Required argments:

pyJay.animation(folder,noOfFrames)

Optional arguments:

pyJay.animation(folder,noOfFrames,speed=1,filetype='.png')

Syntax:

folder: (str):
- Path to a folder with all your frames
noOfFrames: (int):
- The number of frames in the animation
speed: (int):
- How many game frames it takes to update the animation frame (Default 1)
filetype: (str):
- The filetype of the images (Default '.png')

NOTE: In your folder, name the frames as the frame number. Start with Zero (0)


pyJay.animation() functions

slide()

"Slides" the animation to the next frame

display(screen,x,y)

Displays the current frame to the surface passed to the x and y coordinates


Example:

File system:

                
-> root
    main.py
    -> player_idle
        0.png
        1.png
        3.png
        4.png
        5.png
        6.png
            

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()
font = (pygame.font.Font(None,50)

#Define your animation
idle = pyJay.animation('player_idle',7,30)

#Game loop
while True:
    screen.fill((0,0,0))
    
    #Update the animation, then display it
    idle.slide()
    idle.display(screen,50,50)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    pygame.display.flip()
    clock.tick(60)
            


pyJay.announcement()

Required arguments:

pyJay.announcement(text,img,color,font,surface)

Syntax:

text: (str):
- The text that should display when the announcement is shown
img: (str):
- A path to the image that should be displayed behind the text
color: (tuple):
- A tuple containing RGB values for the text color
font: (pygame object):
- A font that has been loaded with pygame.font.Font()
surface: (pygame object):
- A pygame surface that the announcement will be displayed to


pyJay.announcement() functions

announcement.display()

Display the announcement

announcement.move()

Moves the announcement across the screen

announcement.reset()

Resets the announcement position

TIP: Use if announcement_name.text_x >= screen.get_width(): to see if the announcement is over.


Example:

File system:

                
-> root
	main.py
	pyJay.py
	-> img
		->ui
			banner.png
            

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()
font = (pygame.font.Font(None,50)

#Define announcement
level_up = elem.announcement('Level Up!','img/ui/banner.png',(0,255,0),font,screen)

#Game loop
while True:
    screen.fill((0,0,0))
    
    if level_upping:
        #move announcement
        level_up.move()
        #Display announcement
        level_up.display()
        #Reset when it goes off-screen
        if level_up.text_x > screen.get_width():
            level_upping = False
            level_up.reset()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    pygame.display.flip()
    clock.tick(60)
            


pyJay.data_management()

Required arguments:

pyJay.data_management(file_name)

Syntax:

file_name: (str):
- A path to the save file


pyJay.data_management() functions

data_management.save(data)

Saves a dictionary (data) to the save file

data_management.load()

Returns the data in the save file as a dictionary


Example:

File system:

                
-> root
	main.py
	pyJay.py
	-> saves
		save_file.json
            

main.py

                
import pygame, sys
#Don't forget to import pyJay!
import pyJay

#Pygame setup
pygame.init()
screen = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()

#Define a save object
save_obj = pyJay.data_management('saves/save_file')

#Make a dictionary with defaults (In case a save file isn't found)
player_data = {
	'x_pos': 100,
	'y_pos': 300,
	'skin': 'tennis_steve'
}

#Replace dictionary with data from the save file
try:
player_data = save_obj.load()
except:
	#If something fails, return an error and continue
	print('Save file not found')
	print('Loading with defaults')

#Game loop
while True:
	screen.fill((0,0,0))
	
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
			#Save data when quitting game
			save_obj.save(player_data)
	
	pygame.display.flip()
	clock.tick(60)
            

save_file.json

                
{
	"x_pos": 150,
	"y_pos": 200,
	"skin": "tuxedo_steve"
}