pyJay.map(map_info)
pyJay.map(map_info,collider,map_x,map_y,extension)
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')
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.
Moves the entire map by the number of pixels specified in the x-axis and the y-axis
Returns a list of rects that you can use as hitboxes to collide with. Only works when collider is set to True
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
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(file)
file: (str):
- A path to the configuration file
Check to see if the plRect (pygame Rect object) collides with an interactable tile
Move all interactables along the x and y axis (in pixels)
Draw the hitboxes to the suface passed through screen
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
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(x,y,text)
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
Displays the button to the surface you put into the surface argument
Returns True if the mouse_position (tuple) is colliding with the button.
TIP: Pass pygame.mouse.get_pos() into the mouse_position argument
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(startX,startY,color,size,speed)
pyJay.particle(startX,startY,color,size,speed,lifetime)
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)
Moves the particles. Direction is automatically calculated
Returns True if a particle's lifetime is over
Displays the particle to the surface passed
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(folder,noOfFrames)
pyJay.animation(folder,noOfFrames,speed=1,filetype='.png')
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)
"Slides" the animation to the next frame
Displays the current frame to the surface passed to the x and y coordinates
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(text,img,color,font,surface)
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
Display the announcement
Moves the announcement across the screen
Resets the announcement position
TIP: Use if announcement_name.text_x >= screen.get_width(): to see if the announcement is over.
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(file_name)
file_name: (str):
- A path to the save file
Saves a dictionary (data) to the save file
Returns the data in the save file as a dictionary
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"
}