Note: To use Medialib, first you need to install python 3 and then pygame. Visit our Get Started page for more information.
This tutorial was designed for university students from non-technical background who don’t have programming experience and contains exercises related to Medialib with not too difficult maths content. There are 3 hours of practical content suggested being used across two sessions.
Currently, Medialib is used in the Introduction to Computer Science Module for MSc Business Analytics (since 2021) and Master of Data Science (since 2022) at Durham University.
All of the examples in this tutorial can be downloaded from the Gallery.
We are going to learn:
First, let's draw some images:
draw("images/cake.png",100,150)
wait(0.5)
draw("images/cake2.png",180,155)
Remember to “stop” the program after you are done.
If you look at the documentation, you will see the names for the parameters: e.g. draw(imgFileName,x,y)
. The names explain what the parameters are or do.
import
import medialib.py
so we can use the commands in that program, to draw on screen (for example).draw
wait_mouse_leftclick()
text
clear
all_done
See the documentation for more detailed information.
Copy and paste all the code from example.ipynb/example.py into the new file (CTRL+C then CTRL+V in the new file) then save the new program with name: morecakes.ipynb/morecakes.py
Now: modify the program in morecakes.ipynb/py:
Open the file practice1_for learner.ipynb.
print()
to text()
and see what happens.We are going to learn:
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)
Reminder: the top-left corner of the screen is at (0,0).
beginningXPosition=150
, beginningYPosition=100
, length=10
and width=10
.beginningXPosition=150
, beginningYPosition=100
, length=10
and width=10
.We are going to learn:
if
Based on the provided code of Exercise 2 of Practice 1-1, make a program that requires the use of input “2” or “3”.
We are going to learn:
while
play(soundFileName)
Loads the audio file with name soundFileName
and plays it.
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.
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-")
We are going to learn:
while
and if
for
to replace while
To draw background, you can use strings as a parameter to a function.
eg.trees = " BM B B"
andfloor = "GGGWWWGGGWGG"
.
background=[[' ', 'B', 'M', ' ', ' ', ' ', ' ', ' ', 'B', ' ', ' ', 'B'],['G', 'G', 'G', 'W', 'W', 'W', 'G', 'G', 'G', 'W', 'G', 'G']]
We are going to learn:
for
and range
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
Write a function using for
and range
to create an animation which draws a rectangle from big to small.
We are going to learn:
for
and range
Draw a bar chart which consists of several horizontal bars where the length of each bar is stored in the list named 'values'.
Make use of the images saved in the folder named OneDimensionalGame_imgs to design a mini-game by completing the game.jpynb using Medialib. Apply your knowledge of lists
, if
statements, for
loops and while
loops for this task.
The string representing the initial background of the game is 'X-----P--wwwww-----E----X'
. Each character of this string represents the name of an image. H.png represents the player's avatar and its initial position as shown in the image below. 'X ' and 'w ' are the locations the avatar cannot pass. Draw the background of the game as shown below where the first image X.png is at (10,200).
If the player pushes 'q', quit the game and print out “Game Over!”
Control the player's avatar: