The Complete Magazine on Open Source

From LOGO to Scratch

4.19K 0

Programming on LOGO and Python Scratch

Many Indian states like Goa are trying to empower children in schools by teaching them computing. The author has become involved with a school’s hobby club, and this article lists a few programming environments he feels school students should be familiar with.

I was apprehensive when I volunteered for a hobby programming session, and when it was suggested that Scratch would be the ideal environment to work in. I had never used MIT Scratch and am more comfortable writing code rather than dragging and dropping graphical tiles to create code. I was hoping to drive the students towards Python’s Turtle module. However, Scratch was the environment that appealed to the students. After all, animation is far more exciting!

In the process, what we discovered was that different environments make it easier to explore different programming concepts. The following is a short list of programming environments students could explore. In fact, they should explore each one of them, as that will ensure that they have familiarised themselves with the concepts rather than the syntax. Moving to ‘real’ programming using languages like C++, Java, etc, should then be far easier.

Figure 1 Turtle Art script

Figure 1: Turtle Art script

TurtleArt

TurtleArt is a part of the Sugar desktop environment, better known as the software used on OLPC (one laptop per child). This can be installed and used with any Linux desktop environment as well. For example, on Fedora 24, the installation (and correction for any inconsistency in directory names) is as follows:

$ sudo dnf install sugar-turtleart 
$ cd /usr/share/sugar/activtities
$ sudo ln -s TurtleBlocks.activity TurtleArt.activity
$ cp TurtleArt.activity/turtleblocks.desktop ~/Desktop/

Now, clicking on the TurtleBlocks icon should start the TurtleArt development environment.

TurtleArt is an implementation of the LOGO programming language, with extensions, but uses the concept of pluggable blocks from Scratch.

You may think of a turtle as an object that can move, turn and carry a pen which may be moved up or down. And, of course, the colour of the pen can be changed. As soon as the turtle has to create anything more than simple lines, it is obvious that the concept of a function becomes very important.

The example in Figure 1 illustrates the code for drawing squares, with the starting position (0,0) of the turtle as the centre of the squares. There are no parameters to functions in TurtleArt. A student stores values in a box and retrieves them from the box when needed.

Python Turtle

The turtle module in Python is a part of the Tkinter package. The LOGO commands are closely modelled following the Python syntax. A turtle is just a Python class. You may create and manage turtle objects using the full scope of Python.

In particular, it is easy to create multiple turtle objects and control the movement of each. The simple example below illustrates a race between 10 turtles, where their movement is controlled by a random value:

from turtle import *
import random

def create_turtle(y):
turtle = Turtle()
turtle.penup()
turtle.goto(0,y)
turtle.pendown()
# customize the pen of each turtle
turtle.pensize(width=5)
turtle.pencolor((random.random(),random.random(),random.random()))
return turtle
# create a new turtle and position it at different heights.
turtles = []
for number in range(10):
turtles.append(create_turtle(20*number – 100))
# The race
while True:
n = random.randint(0,9)
turtle = turtles[n]
turtle.forward(1)
if turtle.xcor() == 100:
print(“Turtle %d wins”%(n+1))
break
Figure 2 Scratch script for the sprite

Figure 2: Scratch script for the sprite

Figure 3 Scratch script for the stage

Figure 3: Scratch script for the stage

Scratch

Scratch is the common option for Linux. Scratch 2 is not easy to run on Linux as it uses AdobeAIR, which is no longer available for Linux. The major difference between the two is that Scratch 2 allows the creation of custom blocks, which will be discussed later.

Scratch uses blocks grouped into various categories for constructing programs. A block may have parameters. It is easy to drag and connect various blocks to construct a program visually.

The key components of Scratch are sprites, costumes, sounds and scripts. A sprite is a graphic object (perhaps a virtual elf or fairy?), which is manipulated as an entity. Each sprite can be represented by different shapes, called costumes. Sounds are exactly what you may expect.
One or more scripts can be applied to each sprite. The triggering of each script is via an event. Hence, Scratch is a great way to get exposed to event-driven programming.

The example shown in Figures 2, 3 and 4 uses the default sprite with two costumes and a sound. Add three backgrounds to the stage. In the example, the sprite alternates between costumes and moves forward, giving the illusion of walking. Once it hits the end of the stage, it goes to the other end and sends a message to change the background. The sound is triggered by pressing the space bar.

Figure 2 is the script for the sprite and Figure 3 is the script for the stage. Figure 4 shows a few scenes of the stage.

Snap!

This was a project at the University of California Berkeley —to add custom blocks to MIT’s Scratch. It was initially an enhancement of Scratch and called BYOB (build your own block). Meanwhile, Scratch 2 was released, offering the same functionality, though written in AdobeAIR. Snap! is a new implementation modelled on Scratch in JavaScript.
It is expected to be used online; however, the source can be downloaded and installed on a local machine as follows:

$ wget http://snap.berkeley.edu/snapsource/snap.zip
$ cd ~/public_html
$ unzip ~/snap.zip

Apache should be running and user directories should be enabled. For example, on Fedora, it is in the file /etc/httpd/conf.d/userdir.conf.

The source does not have a library of sample sounds, costumes and backgrounds. However, that is not a limitation. You can drag and drop these from the Scratch installation in /usr/share/scratch/Media.

Figure 4 Scratch stage scenes

Figure 4: Scratch stage scenes

Figure 5 Snap script

Figure 5: Snap script

In a revised implementation of the example above, create a block Walk with the number of steps as a parameter. The block issues the message ‘Next room’ once it reaches the end.

Figure 5 shows the block definition and the sprite scripts. There is no change in the stage script from the Scratch example.

You can clone sprites and write recursive code in custom blocks. Each of the environments allows a student to learn and create complex projects, which is fun.

The wonderful aspect of programming is that you learn by doing. And open source software is truly amazing!