Chapter 1. Getting to know Strype
This chapter will show you how to get started with Python programming in Strype. We will work on our first project and start to see what programming in Strype is like, and what we can do with it.
1.1. Getting started
Getting started with programming in Strype is easy: Point your web browser at strype.org, click on the "Start coding now" button, and you will see the Strype editor. Here, you can enter, edit and run Strype programs. Figure 1.1 shows a screenshot of the editor.
When you first open Strype, you will see a short program to get you started.
1.2. Running a program
The first thing to try out is to run the sample program. To do this, click the "Run" button on the right and observe what happens.
After clicking "Run", you should see the text "Hello from Strype" appear in the bottom right area of the window (Figure 1.2). This area is called the "Console", and it is where Strype will display text output. (You will note that there is also a "Graphics" tab next to the Console –- this is for graphical output, and we will use this shortly.)
Clicking the "Run" button executes the program shown in the editor and displays its results.
1.3. Editing a program
You can also change (edit) the current program and then run it again. For example, you could change the text that is printed by the sample program to print something different. Try this now. (If you are not familiar with the basics of editing in Strype, you should read Appendix A: Frame-based editing – the basics first.)
You will note from this paragraph that Python (and other programming languages) use the term "printing" somewhat differently from how we normally use it. Here, it does not mean to print something on paper with an external printer, but simply to write something to the Console.
Exercise 1.1 In the Strype editor, change the green text that reads "Hello from Strype" to say Hello, followed by your own name. Make sure to leave the double quotes around the text you wish to print out. When you have done this, run the program again.
Exercise 1.2 Add a new function call frame at the end of the program. In it, call the print function and print out the words "How are you today?" You should now see two lines of text printed to the Console.
Now that we know how to edit and run programs, we can, of course, write many more programs that produce text output (and much more interesting programs too), and we will do so later in this book. First, however, let us look at another type of output: graphics.
1.4. Starting with graphics
We will start our exploration of graphics programming in Strype with a very simple program that demonstrates some of the basic concepts.
Open the yellow-fish project from the Chapter 1 book projects. Execute the program. You should see a result similar to the one shown in Figure 1.3.
1.5. Importing the graphics library
This yellow-fish program makes use of the Strype graphics library. We can see this near the top of the program, in the section labelled "Imports".
Python is an extendable language, which has some functionality built in, and makes use of "libraries" to add more functions as we need them. In this book, you will learn to use both standard Python functions, as well as commonly used libraries. What is more, you will learn how you can find out what functionality is available in a library, so that you can teach yourself to use it later.
Producing graphical output is an area that is not built in to the standard Python language, so we need to use a library to enable this functionality. This has been done already in the project by adding the following line in the "Imports" section:
The * symbol at the end of this line is a shortcut for "everything". So this line in effect tells the system to import everything from the Strype graphics library. "Importing" means to make it available for us to use in our program, so the effect is that we can now use all of the functionality of the graphics library in our program.
We will start by experimenting with some of the functionality that the library provides.
1.6. Setting the background
Let’s have a look at the first line of code in our program, in the "My code" section:
This kind of statement is called a "function call". A function call executes a function that is defined somewhere else (in this case: in our graphics library) and that performs some kind of action. Later on, we will need to work out how we can find out about all the functions that are available in the library, but for now we will just experiment with a few of them.
In this case, "set_background" is the name of the function we are calling, and it is followed by a pair of parentheses. Between the parentheses is what is called a "parameter". A parameter provides some additional information to tell the function more precisely what to do. Here, we are calling the function set_background, whose job it is to set the background of the graphics world, and we are providing the parameter "LightBlue" to let it know what colour we would like it to use.
Let us experiment a bit with this function call to see how we can influence its behaviour.
Exercise 1.3 Change the parameter of the set_background function call to "DarkRed". Make sure you keep the quotes around the name. Execute the program to see the effect.
Exercise 1.4 The colour names you can use as a parameter are the standard colour names used in HTML for writing webpages. You could find a list of available colour names by doing a web search for "html colours". There is, however, an easier way to try out some other colours: Delete the colour name and the quotes from your function call. You should then see a white slot that lets you enter a new parameter. With the cursor in this slot, click on the "Colour picker" option on the right. You will see a dialogue that lets you pick a colour. Choose a colour that you like.
1.7. Creating actor objects
The next line of code looks like this:
There are several things happening in this one line of code, so we will investigate this in some detail.
In the Strype graphics system, you can add graphical elements to the graphics world by creating "Actor" objects. Actor objects (we may also call them just "actors") are created by inserting a function call frame into your code, and then writing the word "Actor" in place of the function name. Try this out now.
Exercise 1.5 Insert a function call frame at the end of your code. To do this, move the frame cursor to the end of the code and then press space c (or click on the function call option in the frame palette). In place of the function name, write "Actor".
Once you have completed the exercise above, you will see the call to create an actor:
The frame also tells you that a parameter is expected here. It shows the grey word "image" on a white background. This tells us that creating an actor requires an image as a parameter. If you were to execute your program now (try it!), you will see that an error is reported in this frame when we try to run the program without providing an image. So let’s fix this, and add an image.
1.8. Working with images
You can add images to your program, by copying them from outside of Strype (for example, from the web, or from a file in your file system) and pasting them into Strype (Figure 1.4).
Exercise 1.6 Add your own image to the actor object now. You can, for example, do a web search for "beetle icon", find an appropriate image, and copy it. (The image does not need to be an animal. It could be a car, or a spaceship, or anything else that moves.) To copy the image, you can either copy it straight out of your web browser, or save it to your disk first and then copy it from there. Figure 1.4 shows the context menu of a local file with the "Copy" operation selected. This menu will look different on different operating systems, but you should be able to find the copy operation and use it. After copying the image, paste it into the Actor parameter.
Once you have pasted in your own image as the actor image, try running the program again. You should now see the new actor on screen. If you have left the line of code that creates the fish actor in place, you may also see the yellow fish. Both actors will, however, be drawn in the centre of the screen, so if your new actor is larger than the fish, its image may entirely cover the fish image.
It is quite possible that your new image does not look exactly as you would like it to. For this situation, Strype offers some simple image editing functions to adjust the image to make it more appropriate for our task. When you hover the mouse pointer over an image in the Strype code, you will see a preview of the image with an option to open a simple image edit function (Figure 1.5). Here, you can adjust the image for your project. If you need more sophisticated image editing functions, you can of course always use a separate image editing program before using the image.
1.9. The world coordinate system
When we placed our second actor into the world, we saw that they are both drawn on top of each other in the centre of the graphics world. We can change this by assigning initial coordinates to a new actor:
The two additional parameters to the actor specify x and y coordinates for the positioning of the actor. You have probably encountered such coordinates in mathematics; x is the horizontal position and y is the vertical position. Here, the actor is placed at x-coordinate 200 and y-coordinate 100. The coordinate system of the graphics world reaches from -399 to 400 in the horizontal (x) dimension, and -299 to 300 in the vertical (y) dimension. It is shown in Figure 1.6.
Exercise 1.7 Place your own actor into the world somewhere near the top left corner, using the coordinates as shown above.
We have now reached a point where we can create actors, give them images and place them into the world at a specific location. However, currently our actors do not actually do anything. In the next chapter, we will look at making our actors move, so that we can see more interesting things happen.
1.10. Summary
In this chapter, we have jumped straight into the start of our first Python program: showing a graphic. The intention is that we will develop this project into a simple animated game. So far, we only have the very beginning of the project implemented, but we will continue to work on this over the following chapters.




