Medialib v3.0.1 Documentation

Note: To use Medialib, first you need to install python 3 and then pygame. Visit our Get Started page for more information.

Void Functions

A void function is a function which does not return a value.

all_done()

all_done()

This function closes the drawing window. It should be called only once, at the end of the program.

clear()

clear()

clear(r=None,g=None,b=None)

Parameters:
r (int) - red value 0 to 255 inclusive
g (int) - green value 0 to 255 inclusive
b (int) - blue value 0 to 255 inclusive

Clears the drawing window. The default background color is black.

If a color is specified (in RGB format), as in:

clear(0,255,0) ## color is red

the background color is changed. Each successive clear() calls uses the newly set background color.

draw()

draw(imgFileName,x,y)

draw(imgFileName,x,y,width=None,height=None)

Parameters:
imgFileName (string)
x (int) - non-negative x coordinate
y (int) - non-negative y coordinate
width (int) - positive width value
height (int) - positive height value

Loads the image in the current folder, called imgFileName, then draws. If the image is not in the current folder, for example, inside a folder called "img", then use "img/imgFileName".

The top-left corner of the image will be at point (x,y). The size on screen is the same as the image size. If width and height are given, the image is resized to fit the rectangle (x,y,x+width,y+height).

Examples:

draw("banana.jpg",10,50)

draw("banana.jpg",100,150,20,30)

Coordinate System

If the image cannot be loaded a white rectangle will be shown and an error message will be printed in the Python console. No exceptions are thrown, and the program will not break even if the image is not found.

initialize()

initialize()

Always the first instruction of the program.

Initializes default background color, default font, and size of drawing window.

play()

play(soundFileName)

Parameters:
soundFileName (string)

Loads the audio file with name soundFileName and plays it. If the audio file is not in the current folder, for example, inside a folder called "sound", then use "sound/soundFileName".

If the audio file cannot be loaded then no audio will be played and an error message will be printed in the Python console. No exceptions are thrown, and the program will not break even if the audio file is not found.

rect()

rect(x,y,w,h,r=None,g=None,b=None)

Parameters:
imgFileName (string)
x (int) - non-negative x coordinate
y (int) - non-negative y coordinate
w (int) - positive width value
h (int) - positive height value
r (int) - red value 0 to 255 inclusive
g (int) - green value 0 to 255 inclusive
b (int) - blue value 0 to 255 inclusive

Similar to draw(...). This function draws a rectangle from the point (x,y) to the point (x+w,y+h).

By default the rectangle border and inner area are white, but a color can be specified (in RGB format).

Example:

rect(10,15,100,120)

save_screen()

save_screen(file_name,pos_x=None,pos_y=None,width=None,height=None)

Parameters:
file_name (string)
pos_x (int) - non-negative x coordinate
pos_y (int) - non-negative y coordinate
width (int) - positive width value
height (int) - positive height value

Saves an image of the current drawing window into a file, by name.

If the four optional parameters are specified, they will define a rectangle (pos_x,pos_y,pos_x+width,pos_y+height) and only that rectangular area of the drawing window will be saved on file.

Examples:

save_screen("screen_dump.png")

save_screen("testing.png",50,60,500,400)

set_font()

set_font(file_name)

Parameters:
file_name (string)

Sets an external file as the new current font.

Example:

set_font('pacifico.ttf')
text('Hello!',100,150,16)

where 'pacifico.ttf' is a TTF file in the local folder. If the file is broken or not present, the default font will be used.

text()

text(message,x,y,font_size)

text(message,x,y,font_size, r=None,g=None,b=None)

Parameters:
message (string)
x (int) - non-negative x coordinate
y (int) - non-negative y coordinate
font_size (int) - positive font size value
r (int) - red value 0 to 255 inclusive
g (int) - green value 0 to 255 inclusive
b (int) - blue value 0 to 255 inclusive

Writes the message string in the drawing window, at the postion (x,y), with the current font, and the characters will have the size font_size (measured in pixels). If a color is provided (via the r, g and b parameters), then the text will be written in that color. Default color is white.

Example:

text('Hello!',100,150,16)

writes the string 'Hello!' at position (100,150), and the size of the characters is 16 pixels.

wait()

wait(secs)

Parameters:
secs (float)

This blocking command stops the control flow of the program for a certain number of seconds.

Real numbers can be used for the seconds.

Example:

wait(1.5) ## pause the program for 1.5 seconds

wait_mouse_leftclick()

wait_mouse_leftclick()

This blocking command stops the control flow of the program until the mouse's left button is pressed.

Then the program continues normal execution.

Example:

print("Press the left mouse button to continue.")
wait_mouse_press()
print("Well done! Let's continue...")

Fruitful Functions

A fruitful function is a function which returns a value.

a_to_b()

a_to_b(a,b,tPercentage)

Parameters:
a (int) - non-negative a value
b (int) - non-negative b value
tPercentage (int) - value between 0 and 100 inclusive
Returns:
(float)

This function computes a linear interpolation between the two values a and b.

The parameter tPercentage should be an integer number between 0 and 100.

When tPercentage=0 the function will return a, when tPercentage=100 the function will return b, and when tPercentage=50 the function will return (a/2 + b/2), the value in between a and b.

Example:

print( a_to_b(100,200, 0) )   ## prints 100
print( a_to_b(100,200, 100) ) ## prints 200
print( a_to_b(100,200,  50) ) ## prints 150
          
distance()

distance(x1,y1,x2,y2)

Parameters:
x1 (int) - non-negative x position
y1 (int) - non-negative y position
x2 (int) - non-negative x position
y2 (int) - non-negative y position
Returns:
(float)

Returns the distance in pixels between the two points (x1,y1) and (x2,y2).

get_mouse_pos()

get_mouse_pos()

Returns:
(int) - non-negative x coordinate
(int) - non-negative y coordinate

This non-blocking function returns a tuple of 2 values: the x and y coordinates of the mouse, calculated with respect to the drawing window. This means that x is always >=0 and <800, and y is always >=0 and <600.

Example:

a,b = get_mouse_pos()
print("mouse at: " , a , " and ", b)
get_text_rect()

get_text_rect(message,x,y,font_size)

Parameters:
message (string)
x (int) - non-negative x coordinate
y (int) - non-negative y coordinate
font_size (int) - positive font size value
Returns:
(int) - positive width value
(int) - positive height value

This function does not print the text on screen, but instead calculates the size of the rectangle that contains all pixels of the text; this function then returns 2 values: the width and height of the rectangle containing the text, measured in pixels.

Example:

w,h = text_size('Hello!',100,150,16) ## the text is not shown on screen
rect(100,150,w,h)                    ## this rectangle will contain the text 
  
is_key_press()

is_key_press(key_name)

Parameters:
key_name (string)
Returns:
(bool) - True or False

This function does not block the program. It checks whether the key keyName was pressed just before the function was called; if so, the functions returns True, otherwise it returns False.

Example:

if is_key_press("a"):
    print("you pressed -a-")
is_mouse_leftclick()

is_mouse_leftclick()

Returns:
(bool) - True or False

This function is the non-blocking version of wait_mouse_leftclick(). If the left mouse button is pressed just before this function is called, the function will return True; False is returned otherwise.

Example:

if is_mouse_leftclick():
    print("mouse pressed!")
point_inside_rect()

point_inside_rect(px,py,pos_x,pos_y,width,height)

Parameters:
px (int) - non-negative x position
py (int) - non-negative y position
pos_x (int) - non-negative x position
pos_y (int) - non-negative y position
width (int) - positive width value
height (int) - positive height value
Returns:
(bool) - True or False

This function tests if a point (px,py) is inside the rectangle (pos_x,pos_y)-(pos_x+width,pos_y+height).

The result is a boolean.

Example:

if point_inside_rect(100,120,50,60,100,110):
print("the point is inside")
      
wait_key_press()

wait_key_press()

Returns:
(string) - character or code

Pauses the program until a key is pressed (any key).

If ESC, a number or a character is pressed, it will return a character. Otherwise it will return a code.

Example:

k = wait_key_press()
if k=="a":
    print("you pressed -a-")



Creative Commons Licence
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.