Note: To use Medialib, first you need to install python 3 and then pygame. Visit our Get Started page for more information.
A void function is a function which does not return a value.
all_done()
This function closes the drawing window. It should be called only once, at the end of the program.
clear()
clear(r=None,g=None,b=None)
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(imgFileName,x,y)
draw(imgFileName,x,y,width=None,height=None)
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)
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.
draw("images/cake.png",100,150)
wait(0.5)
draw("images/cake2.png",180,155)
initialize()
Always the first instruction of the program.
Initializes default background color, default font, and size of drawing window.
play(soundFileName)
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(x,y,w,h,r=None,g=None,b=None)
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(file_name,pos_x=None,pos_y=None,width=None,height=None)
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(file_name)
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(message,x,y,font_size)
text(message,x,y,font_size, r=None,g=None,b=None)
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.
draw("images/cake.png",100,150)
wait(0.5)
draw("images/cake2.png",180,155)
wait(secs)
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()
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...")
A fruitful function is a function which returns a value.
a_to_b(a,b,tPercentage)
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(x1,y1,x2,y2)
Returns the distance in pixels between the two points (x1,y1)
and (x2,y2)
.
get_mouse_pos()
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(message,x,y,font_size)
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(key_name)
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()
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(px,py,pos_x,pos_y,width,height)
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()
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-")