Table of Contents

Notes about RIBS
Keyword and default arguments
key_down
key_released
key_pressed
draw_transformed
draw_text
overlap_data
solve_rect_overlap
set_screen_size
set_frame_rate
time
delta
restart
start_game

Snake Ribs

A small and simple PyGame wrapper, for getting started quick and easy.

Notes about RIBS

Description
Ribs is super small and easy to use collection of functions to help make pygame easier to work with. This includes things like drawing images in a single function, handling user input, and a few other things.

If you want do do something in your game but can't find a suitable function, you may want use pygame directly. All documentation for pygame can be found at https://www.pygame.org/docs/

Also note that not everything is documented, if it's not documented you probably don't have to care about it.

Keyword and default arguments

Description

Some of the functions in ribs use a Python feature you may not have come across: default arguments. These allow you to only specify a few of the arguments to a function, and leave the rest. For example, the definition of draw_transformed looks like this:

draw_transformed( img, position, scale=(1., 1.), degrees=0 )

The =x part of the scale and degrees mean that you can call the function as one of

draw_transformed( img, (100, 100)) # Default scale and degrees
draw_transformed( img, (100, 100), (2, 2)) # Scale up by 2, leave degrees unchanged
draw_transformed( img, (100, 100), (2, 2), 90) # Scale up by 2, rotate by 90 degrees
You can also specify one of many arguments by typing out its name. This is useful if you want to leave the first argument as default, but change another

draw_transformed( img, (100, 100), , degrees = 90) # Rotate 90 degrees, default scale

key_down( key )

Description

Takes a key, that's either a keycode or a character, and returns true if that key is held.

keyA string with the character or keycode on the form (pg.K_****) of the button to check.

For a list of keycodes, see the table in the pygame documentation

Example

if key_down("a"):
    print("COWABUNGA")
This code would print "COWABUNGA" to the console while the "a" button is held.

Example

if key_down(pg.K_LEFT):
    print("Move left")
This code would print "Move left" to the console while the left arrow key is held.

key_released( key )

Description

Takes a key, that's either a keycode or a character and returns true if the key was released this frame.

keyA string with the character or keycode on the form (pg.K_****) of the button to check.

For a list of keycodes, see the table in the pygame documentation

Example

if key_released("a"):
    print("hold longer maybe?")
This code would print "hold longer maybe?" to the console when the "a" button was released.

key_pressed( key )

Description

Takes a key, that's either a keycode or a character, and returns true if that key was pressed this frame.
keyA string with the character or keycode on the form (pg.K_****) of the button to check.

For a list of keycodes, see the table in the pygame documentation

Example

if key_pressed(pg.K_SPACE):
    print("JUMP")
This code would print "JUMP" to the console when the spacebar is pressed, and only that frame.

draw_transformed( img, position, scale=(1., 1.), degrees=0 )

Description

Draws a sprite centered around position scaled by scale and rotated clockwise degrees. The position to draw the sprite is given in pixels relative to the top left corner of the screen.

The img argument is an image object loaded by pg.image.load. Note that if you want to draw lots of images, it is not a good idea to re-load the images every frame. Instead, load images into a variable in the init function and do the drawing in update

imgThe image to be used as a sprite.
positionThe center position of the sprite.
scaleA scale factor to apply. (Optional)
degreesRotate the sprite. (Optional)

Example

assets["teapot"] = pg.image.load("teapot.png")
draw_transformed(assets["teapot"], (100, 100))
This code would draw the "teapot.png" image (given that you have one) 100 pixels to the right of the left edge, and 100 pixels below the top edge of the window.

Example

assets["teapot"] = pg.image.load("teapot.png")
draw_transformed(assets["teapot"], (100, 100),
                 (1, 0.5), time)
This code would draw the "teapot.png" image (given that you have one), and given that time was the "time since the program started", this would draw a sprite at (100, 100) shrunk to half size along the y axis, rotating around it's center.

draw_text( text, position, size=32, color=pg.Color(255, 255, 255), font=None )

Description

Draw text at position, which is given in pixels from the top left corner. Optional arguments include size given in points, color which is a standard pygame color and font which is a string. To get a list of available fonts, use pg.font.get_fonts().
textThe text to be drawn.
positionThe top left corner of the text, in pixel coordinates.
sizeThe font size in points. (Optional)
colorThe color of the text. (Optional)
fontName of the font to use. (Optional)

Example

draw_text("Level: 1", (0, 0))
Draw "Level: 1" in the top left corner of the screen.

Example

draw_text("Level: 2", (0, 0), size=100)
Draw "Level: 2" with large letters in the top left corner of the screen.

Example

draw_text("Level: 3", (0, 0), color=pg.Color(255, 0, 0))
Draw "Level: 3" in red in the top left corner of the screen.

Example

draw_text("Level: 4", (0, 0), font="Times New Roman")
Draw "Level: 4" in the top left corner of the screen using the 'Times New Roman' font. Note that only fonts installed on the system can be used. Use pg.font.get_fonts() to get a list of fonts installed on your system.

Example

draw_text("Level: 5", (1000, 1000), 50, pg.Color(0, 255, 0), "Comic Sans MS")
Draws the text "Level: 5", where the top of the "L" is at the coordinates (1000, 1000), a font size of 50, colored with green (you could use a swatch here), all written in Comic Sans.

overlap_data( a, b )

Description

Returns the axis that points from a to b, and the depth of the collision. If the depth is negative it means they are far away from overlapping.

Note that this function is not super important, you probably want to use solve_rect_overlap(a, b)

aA pg.Rect(minx, miny, width, height).
bA pg.Rect(minx, miny, width, height).

Example

rect_a = pg.Rect(200, 200, 100, 100)
rect_b = pg.Rect(250, 250, 50, 100)
normal, depth = overlap_data(rect_a, rect_b)
overlap = depth > 0
print(overlap)
This code would print "True" since the rectangles overlap, and thus the depth of their overlap is positive.

solve_rect_overlap( a, b, vel_a=(0, 0), vel_b=(0, 0), mass_a=1, mass_b=1, bounce=0 )

Description

This function checks if a and b overlap, if they do the collision is solved and two new rectangles moved out of eachother along the shortest collision vector are returned along with their velocities.

Note that a and b are modified inplace, and might be changed after calling this function.

aA pg.Rect(minx, miny, width, height).
bA pg.Rect(minx, miny, width, height).
vel_aThe velocity of a, given as a tuple.
vel_bThe velocity of b, given as a tuple.
mass_aThe mass of a. 0 mass means it's static and immovable.
mass_bThe mass of b. 0 mass means it's static and immovable.
bounceHow bouncy the collision should be. Values higher than 1 will introduce more energy into the system and might cause glitches. Values lower than 0 don't make sense.

Example

rect_a = pg.Rect(200, 200, 100, 100)
vel_a = (0, 1000)
rect_b = pg.Rect(250, 250, 50, 100)
vel_b = (0, 0)
vel_a, vel_b, hit = solve_rect_overlap(rect_a,\
                                       rect_b,\
                                       vel_a,\
                                       vel_b,\
                                       bounce=1.0)
This code will create a collision between rect_a and rect_b. The collision will send rect_b flying since all the energy from rect _a is transferred to rect_b.

set_screen_size( width, height )

Description

Sets new dimensions for the screen that renders the game.
widthThe new width of the screen in pixels.
heightThe new height of the screen in pixels.

Example

set_screen_size(1337, 420)
This line of code would set the window's dimensions to 1337 pixels wide and 420 pixels high.

set_frame_rate( fps )

Description

Updates the frame rate limit to the supplied FPS, if set to 0 there will be no limit.
fpsThe frame rate to limit to, 0 means no limit.

Example

set_frame_rate(0)
This line of code would unlimit the frame rate.

Example

set_frame_rate(60)
This line of code would limit the frame rate to 60 frames per second.

time( )

Description

Return the number of seconds passed since the start of the game, or the latest restart.

delta( )

Description

Return the number of seconds passed between this frame and the previous. The number is usually quite small (~0.16).

restart( )

Description

Restart the game and reset all state from the engine's side of things. Note that it might not clear your stored state, which might cause the game to behave strangely when restarting.

Note that calling restart() won't terminate the "run".

start_game( init, update )

Description

The code that calls into the ribs.py and sets up everything that needs to be set up. You just give in your function.

If you're using the supplied template, you don't need to worry about this.

initThe initalization function, this one is only called once. Takes no arguments.
updateThe update function, expected to be an iterator that returns when the game is finished. Takes no arguments.

Example

def init():
    print("Initialized, only called once!")
</p><p>def update():
    print("Called for every start and restart!")
    while True:
        print("Update!")
        yield
</p><p>start_game(init, update)
This code would create a game that prints the following:

Initialized, only called once!
I'm called for every start and restart!
Update!
Update!
Update!
.
.
.