Using the Scratch Media Bundle in Kojo

One of the main areas of usage of image files (backgrounds and costumes) and sound files in Kojo is sprite based animations and games (in Kojo terminology, sprites are turtles with costumes).

Many different backgrounds, costumes, and sound files are available in the Scratch Media Bundle. To use these files, download the media bundle zipfile and unzip it under your home directory. You can then access the media files from inside Kojo using their absolute path, or their path relative to your home directory. Let's explore this further.

Kojo has a built-in function called homeDir that tells you what your home directory is. Let's say that you call this function in the Script Editor, and it tells you that your home dirrectory is /home/yourname.

You now download the media bundle and unzip it in your home directory, so that you now have a folder called /home/yourname/Media which contains the media files. Two of the files inside the Media directory are Media/Backgrounds/Nature/stars.gif and Media/Costumes/Fantasy/ghost1.png. The following code (that you can run in the Script Editor) uses these two files:

// the initial turtle in Kojo is called turtle0; you set its costume to specify a background image
turtle0.setCostume("~/Media/Backgrounds/Nature/stars.gif") // ~ in the file path stands for your home dir

val ghost = newTurtle(0, 0, "~/Media/Costumes/Fantasy/ghost1.png")
ghost.act { self => // we give this turtle the name 'self' within act {...}
    repeatWhile(true) {
        self.changePosition(1, 0)

playMp3Loop("/home/yourname/Media/Sounds/Music Loops/Medieval1.mp3") // you can also use absolute paths

So, to repeat, you can use media files in you programs using either their:

  1. Absolute paths - e.g. "/home/yourname/Media/Sounds/Music Loops/Medieval1.mp3"
  2. Paths relative to your home directory - e.g. "~/Media/Backgrounds/Nature/stars.gif"

Note for Windows users

Pathnames in Windows include the back-slash \ character. If this character is put as-is inside filepath strings in Kojo, Kojo will report an invalid string error (because \ is an escape character in strings). To work around this, you have three options:

  • use triple-quoted raw strings: """some\windows\path"""
  • use interpolated raw strings: raw"some\windows\path"
  • change the back-slashes to forward-slashes: "some/windows/path"
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License