dark mode light mode Search Menu

Code with Pics in Pyret

Doubbt Official on Flickr

Are you interested in text-based coding but prefer working with images and colors over numbers and letters? If so, you may want to try out the Pyret language. It shares many similarities with Python, and has a free, easy-to-use, online editor that does not require anything to be downloaded or installed.

To get started coding with Pyret, sign in to the online Pyret editor found at https://code.pyret.org/. Create an account or sign in through your Google account to be sure that you can save your work.

When the editor opens you will see the screen below:

Notice that the screen is split. The right side acts as a powerful calculator that can perform calculations and also test out code. Type in the following examples at the chevron (>>>) on the right. After each example press enter to see the result.


8 + 6
7 * 5
20 / 4
7 - 10
2/5 + 3/4

Be sure to always include a space between the operator — such as the + , – , … — and the numbers you wish to calculate. If everything worked out, you should see the following results:

The editor can also perform more advanced calculations. If you are familiar with exponents or square roots, try out the examples below, still using the right-hand side of the Pyret editor:

num-expt(6, 2)
num-expt(2, 3)

Did you notice that num-sqrt() finds the square root of the number in parentheses, and that num-expt() uses the first number as the base and the second number as the exponent? If done correctly, these previous four examples produce the following results: 5, 4, 36, 8.

While the right side of the screen makes a great calculator, the left side is where we can code. One of the first steps when coding in Pyret is defining a variable. For example, in the third example above, we used num-expt(6, 2) to raise 6 to the 2nd power. But num-expt() is quite a lot to type every time we want to raise a number to a power. By defining a variable, let’s say x, on the left side of the editor, we can ‘rename’ num-expt(6, 2) and save ourselves some typing. Try this out by entering the code as we did below:

Be sure to click on the blue Run button on the upper right side of the screen so that the Pyret editor knows what we mean by x.

Once we have run our code, we can type x on the right hand side and we’ll get the same result as we would get by typing num-expt(6, 2).

One of the funnest parts about programming with Pyret is that we can easily move from working with numbers to working with images. So far we have only used numbers, but by accessing the Pyret image library, we can start making pictures with code also!

Let’s get started with images by typing the following at the top of the left hand side of the Pyret window and clicking the blue Run button:

If there are no mistakes, it will appear that nothing happened. If there was a typo you will see an error message when you run the code. Now we can start working with Pyret’s image library. On the right hand side test out the following image functions:

circle(50, "solid", "magenta")
triangle(70, "outline", "blue")
star(35, "solid", "orange")
rectangle(60, 80, "outline", "purple")

Refer to the image below to double check your work:

Notice that the code for all the images follows a pattern. Each requires a size (a number), a fill (solid or outline), and a list of Pyret colors at https://www.pyret.org/docs/latest/color.html. Also note that the fill and color — called ‘strings’ — must be in quotation marks, while the size (a number) does not need quotation marks around it.

Once you have an image you like that you may want to use multiple times, you can rename it using a variable. For example, if I want to create a scene that has several solid orange stars of size 35, I can rename my star as star35 on the left side of the screen and click Run. Now every time I want another star, all I need to type is star35 on the right.

The size, fill, and color that we type into each function are called the function’s arguments. Actually, num-sqrt()and num-expt() from the beginning of this article are also functions that take in arguments but instead of creating images, they perform a calculation. The Pyret language has many more image functions listed on the documentation site at https://www.pyret.org/docs/latest/image.html. A couple more fun functions to try out include overlay() and image-url().

The overlay()function takes in two image arguments and centers the first one on top of the second one. Refer to the code below to overlay a smaller star on top of a larger star:

Again, if we want to quickly refer back to an image, we can store it in a variable. Below, we renamed the overlaid stars ‘doublestar’.

If we want to use images that we have created or found online, we can use the image-url() function. It is a good idea to first upload the images we want to use into our Google drive. In the Pyret editor we click the Insert button on the upper left hand side of the screen. This gives us access to the images in our Google drive. Now we can select the image we want to appear in our Pyret code.

For example we found a jack-o-lantern image by Yeong Rong Kim from the Noun Project, at http://thenounproject.com, and uploaded it to our Google drive. Within the Pyret editor we can click Insert and our Google drive images appear as shown below:

After selecting the jack-o-lantern from our drive, we are able to select the import style:

Finally, the image-url() function appears in the Pyret editor, with the jack-o-lantern image loaded into it. We decided to rename our image jack.

If the image is not the size that we hoped for, we can use the scale()function to resize it. For example, if we want our image, jack, to be 25% of its original size, we do scale(0.25, jack):

In the December issue of this magazine we will demonstrate how to create a more complex scene, as well as how to write our own functions. Until then, try fiddling with the built-in functions described in this article and see if you can overlay images to make a scene of your own!

Learn More

Pyret Editor


Bootstrap World Middle and High School Curriculum/Courses (Pyret-based)


Why Pyret?


Pyret Documentation


Pyret to Python


The Pyret Crew