What is Python?
Python is an easy-to-learn programming language that has some really useful features for a beginning programmer. The code is quite easy to read and with PictoBlox it has an interactive interface into which you can enter your programs and see them run.
How does Python Work in PictoBlox?
PictoBlox runs Python Program in Script Mode.
In script mode, we type the Python program in a file and then use the interpreter to execute the content from the file. Let’s see how it works.
Open PictoBlox. You will find that it asks you to select the Coding Environment and gives you two options:
- Block Coding
- Python Coding
You have to select Python Coding. The Python interface will then open. Also, you can set Python Coding as the default coding environment (By clicking on settings -> Preferences, from the top right corner):
Exploring the Interface
Python Coding Area
The following is the editor where you write your python codes. It gives you the option to add files, and modules and writes programs, all at once place.
The following is the area where the Python output can be observed. It has a terminal to see the programs’ output and a log to see the errors that come while running the project.
It also has two buttons: Run and Run All. The run button runs the selected Python file from the editor.
Stage and Sprites
PictoBlox also allows you to use sprites and stages to create games, animations, and interactive projects with characters.
Activity 1: Hello World
Let’s create the first program in Python. In this program, we are going to print Hello World in the terminal.
Simple as that.
Now run the program with the Run button.
You can observe in the terminal that the message has been printed:
>> Hello, World!
print() is a function that allows us to write on the terminal. We will use this function very often.
Activity 2: Introduce Yourself with Tobi
Let us see how we can program the characters on the stage.
What is the Stage?
The stage is a background or a backdrop for your projects. It is a white background in the top left corner; you will see a bear standing there. His name is Tobi and he is what is called a sprite (we’ll see in a moment what it is).
The Stage is where the sprite moves, draws, and interacts with other sprites and/or hardware. It has its own set of scripts, images, and sounds.
What is a Sprite?
A Sprite is an object or a character that performs different actions in the projects. It understands and obeys the instructions that you’ll give them in your projects. It can move and can be moved to any place on the stage (you can try by clicking on them and moving them around).
Controlling the Sprites
Let’s see how we can control Tobi with Python.
Controlling sprites is tricky. We have to first define the sprite in the program. We do this using the following statement:
sprite = Sprite('Tobi')
It defines an object named sprite which controls the character Tobi.
Now, I can make Tobi say something with the say() function:
sprite.say("Hello everyone!", 2)
say() function takes 2 inputs:
- The text we want to write: “Hello everyone!”
- Time for which we want to have the character say the text. In this case, it’s 2 seconds.
It’s important that we follow the syntax. Otherwise, you will get the error on execution. So let’s test the code till now:
sprite = Sprite('Tobi') sprite.say("Hello everyone!", 2)
Run the code and you will get the following output on the stage:
Next, we want the Tobi to say the following:
- Greet everyone
- Say your name.
- Say which class you are studying in?
- Say your hobbies.
- Say thank you.
The final code will look like this:
sprite = Sprite('Tobi') sprite.say("Hello everyone!", 2) sprite.say('My name is Tobi', 2) sprite.say('I am studying in 7th grade', 2) sprite.say('My hobbies are playing cricket and listening to music', 4) sprite.say('Thank You', 2)
Run the code to test the output:
Saving the Project
- To save the program, click on File > Save As.
- A window will pop up. Choose the location as Desktop or any other relevant folder. Name the file Tobi Walking.
- Click on Save.
The file is saved with a .sb3 extension on your computer.
In conclusion, Python is a powerful programming language that has a lot of useful features for beginners. In PictoBlox, you can use Python to control the sprites and stages and create programs, animations, and games. As we have seen in this lesson, Python code is quite easy to write and understand. By following the syntax and the instructions, you can create amazing projects with Python in PictoBlox!