Classes
Window
Camera
Cursor
Key
Base
Rectangle
Image
Text
Circle
Shape
Physics
Methods
random
randint
run
Welcome to the JoBase reference manual.
Below is a list of built in colors.
WHITE
BLACK
GRAY
DARK_GRAY
LIGHT_GRAY
BROWN
TAN
RED
DARK_RED
SALMON
ORANGE
GOLD
YELLOW
OLIVE
LIME
GREEN
DARK_GREEN
AQUA
AZURE
LIGHT_BLUE
BLUE
NAVY
PURPLE
PINK
MAGENTA
Whenever you come across a position, color or size like the one below, it may seem like a tuple object.
Although we have displayed it as a tuple, it behaves quite differently.
size: (float, float)
The variable above is actually a JoBase vector.
This means that its properties are keys.
The example below illustrates how this variable can be used.
box = Rectangle(width = 20, height = 20)
print(box.size) # (20, 20)
box.size = 30, 10
print(box.size) # (30, 10)
box.size.x = 40
print(box.size) # (40, 10)
box.size = 100
print(box.size) # (100, 100)
JoBase vectors also support rich comparisons and numerical operations.
box.size = (5, 7)
box.pos = [5, 7]
print(box.size == box.pos) # True
box.pos += 1
print(box.pos) # (6, 8)
box.pos *= 2
print(box.pos) # (12, 16)
Classes
class Window(caption = "JoBase", width = 640, height = 480, color = WHITE)
This class is for managing the main window.
The inbuilt
window variable is an instance of this class.
Do not try to instance this class directly.
from JoBase import *
window.color = BLUE
window.size = 300, 300
def loop():
window.red += 0.01
run()
caption: str
The title of the window.
red, color.red: float
The red color of the window.
green, color.green: float
The green color of the window.
blue, color.blue: float
The blue color of the window.
color: (float, float, float)
The color of the window.
width, size.x: float
The width of the window in pixels.
height, size.y: float
The height of the window in pixels.
size: (float, float)
The dimensions of the window in pixels.
close()
Close the window.
The program finishes and the window cannot be opened again.
maximize()
Maximize the window.
You can restore the maximized window with the
restore method.
minimize()
Minimize the window.
You can restore the minimized window with the
restore method.
restore()
Restore the window from maximized or minimized.
The window returns to its normal state.
focus()
Give the window input focus.
It can be very disruptive to the user when the window is forced to the top - use this method sensibly.
class Camera()
This class is for managing the game camera.
By moving it around, you can change your view of the objects on the screen.
The inbuilt
camera variable is an instance of this class.
Do not try to instance this class directly.
x, pos.x, position.x: float
The x position of the camera.
y, pos.y, position.y: float
The y position of the camera.
pos, position: (float, float)
The position of the camera.
move_toward(other: object, speed = 1)
Move the camera toward another object at a constant speed.
move_smooth(other: object, speed = 0.1)
Move the camera smoothly toward another object.
It is best to keep the speed between 0 (not moving) and 1 (instant snapping).
top: float
The top of the viewport.
bottom: float
The bottom of the viewport.
left: float
The left of the viewport.
right: float
The right of the viewport.
class Cursor()
This class represents the cursor.
The inbuilt
cursor variable is an instance of this class.
Do not try to instance this class directly.
x, pos.x, position.x: float
The x position of the cursor.
y, pos.y, position.y: float
The y position of the cursor.
pos, position: (float, float)
The position of the cursor.
move: bool
The cursor has moved.
enter: bool
The cursor has entered the window.
leave: bool
The cursor has left the window.
press: bool
A cursor button is pressed.
release: bool
A cursor button is released.
hold: bool
A cursor button is held down.
collides_with(other: object)
Check if the cursor collides with the another object.
class Key()
This class is for detecting keyboard input.
The inbuilt
key variable is an instance of this class.
Do not try to instance this class directly.
press: bool
A key is pressed down.
release: bool
A key is released.
repeat: bool
When the user holds a key down, a series of events are fired.
These events do not occur every frame.
hold: bool
A key is held down.
[name]: {"press": bool, "release": bool, "repeat": bool} or False
Access the state of a specific key.
if key.left:
# the left arrow key is held down
if key.left["release"]:
# the left arrow key is released
if key.right and key.right["press"]:
# the right arrow key was pressed down
If a key is not being held down, it's properties cannot be accessed.
Make sure you check that the key state is not
False before trying to access a property.
Below is a list of all the key names.
space
apostrophe
comma
minus
period
slash
_0
_1
_2
_3
_4
_5
_6
_7
_8
_9
semicolon
equal
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
left_bracket
backslash
right_bracket
backquote
escape
enter
tab
backspace
insert
delete
right
left
down
up
page_up
page_down
home
end
caps_lock
scroll_lock
num_lock
print_screen
pause
f1
f2
f3
f4
f5
f6
f7
f8
f9
f10
f11
f12
f13
f14
f15
f16
f17
f18
f19
f20
f21
f22
f23
f24
f25
pad_0
pad_1
pad_2
pad_3
pad_4
pad_5
pad_6
pad_7
pad_8
pad_9
pad_decimal
pad_divide
pad_multiply
pad_subtract
pad_add
pad_enter
pad_equal
left_shift
left_ctrl
left_alt
left_super
right_shift
right_ctrl
right_alt
right_super
menu
class Base()
This is the base class for all drawing primitives.
The attributes of this class are shared with other objects.
Some of the attributes are only useful when the object is added to the physics engine.
Do not try to instance this class directly.
x, pos.x, position.x: float
The x position of the object.
y, pos.y, position.y: float
The y position of the object.
pos, position: (float, float)
The position of the object.
left: float
The left position of the object.
top: float
The top position of the object.
right: float
The right position of the object.
bottom: float
The bottom position of the object.
scale.x: float
The horizontal scale of the object.
scale.y: float
The vertical scale of the object.
scale: (float, float)
The scale of the object.
anchor.x: float
The horizontal origin of the object.
anchor.y: float
The vertical origin of the object.
anchor: (float, float)
The rotational origin of the object.
angle: float
The angle of the object.
red, color.red: float
The red color of the object.
green, color.green: float
The green color of the object.
blue, color.blue: float
The blue color of the object.
alpha, color.alpha: float
The transparency of the object.
color: (float, float, float, float)
The color of the object.
type: int
The physics body type of the object.
It can be STATIC or DYNAMIC (default).
mass: float
The physics weight of the object - high means heavy.
elasticity: float
The physics bounciness of the object.
In most cases, it is best to keep this value between 0 (not bouncy) and 1 (perfectly bouncy).
friction: float
The physics roughness of the object.
In most cases, it is best to keep this value between 0 (not slippery) and 1 (really slippery).
velocity.x, speed.x: float
The physics x speed of the object.
velocity.y, speed.y: float
The physics y speed of the object.
velocity, speed: (float, float)
The physics speed of the object.
angular_velocity, rotate_speed: float
The physics rotation speed of the object (how fast it's spinning).
rotate: bool
The object is allowed to rotate in a physics engine.
Set it to False if you want the object to stay the same angle.
look_at(other: object)
Rotate the object to look at another object.
move_toward(other: object, speed = 1)
Move the object toward another object at a constant speed.
collides_with(other: object)
Check if the object collides with the another object.
class Rectangle(x = 0, y = 0, width = 50, height = 50, angle = 0, color = BLACK)
This class is for drawing rectangles.
Because it extends the
Base class, it contains all the same attributes.
width, size.x: float
The width of the rectangle in pixels.
height, size.y: float
The height of the rectangle in pixels.
size: (float, float)
The dimensions of the rectangle in pixels.
class Image(name = MAN, x = 0, y = 0, width = 0, height = 0, angle = 0, color = WHITE)
This class is for drawing images.
Because it extends the
Rectangle class, it contains all the same attributes.
The
name is a string of the filepath to the image.
JoBase contains the following built in images.
The example below loads the enemy image and draws it.
from JoBase import *
enemy = Image(ENEMY)
def loop():
enemy.draw()
run()
class Text(content = "Text", x = 0, y = 0, font_size = 50, angle = 0, color = BLACK, font = DEFAULT)
This class is for drawing text messages.
Because it extends the
Rectangle class, it contains all the same attributes.
content: str
The string content of the text.
font: str
The filepath to the font family of the text.
JoBase contains the following built in fonts.
CODECode
DEFAULTDefault
HANDWRITINGHandwriting
JOINEDJoined
PENCILPencil
SERIFSerif
TYPEWRITERTypewriter
The example below loads the pencil font and draws the text.
from JoBase import *
text = Text("Pencil Font", font = PENCIL)
text.font_size = 80
def loop():
text.draw()
run()
font_size: float
The size of the font.
class Circle(x = 0, y = 0, diameter = 50, color = BLACK)
This class is for drawing circles.
Because it extends the
Base class, it contains all the same attributes.
diameter: float
The width of the circle.
radius: float
The radius of the circle (half of the diameter).
class Shape(points = ((0, 25), (25, -25), (-25, -25)), x = 0, y = 0, angle = 0, color = BLACK)
This class is for drawing polygon shapes.
Because it extends the
Base class, it contains all the same attributes.
class Physics(gravity_x: 0, gravity_y: -500)
This class is a physics engine environment.
Add objects to give them realistic physics.
You can run a basic demo by typing the following command into the terminal.
python -m JoBase.examples.box_physics
The example below creates a box that bounces on the ground.
from JoBase import *
floor = Rectangle(width = window.width, height = 20)
floor.bottom = camera.bottom
floor.type = STATIC
floor.elasticity = 0.9
box = Rectangle(angle = 10)
box.elasticity = 0.9
engine = Physics()
engine.add(floor)
engine.add(box)
def loop():
engine.update()
floor.draw()
box.draw()
run()
If you are adding many objects to the physics engine, you can draw them all by iterating the class.
engine = Physics()
# add lots of things
for thing in engine:
thing.draw()
gravity.x: float
The horizontal gravity.
gravity.y: float
The vertical gravity.
A negative number makes the objects fall to the bottom.
gravity: (float, float)
The gravity in the physics environment.
add(other: object)
Add a
Base object to the physics engine.
remove(other: object)
Remove a
Base object from the physics engine.
update()
Calculate the next frame for the physics.
It is best to call this function every frame.
Methods
def random(a: float, b: float) -> float
Find a random decimal between a and b.
def randint(a: int, b: int) -> int
Find a random integer between a and b inclusive.
def run()
Activate the main game loop and display the window.
Always call this method at the bottom of your code.