OpenSource For You

Exploring Software: From LOGO to Scratch

Many 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 programmin­g environmen­ts he feels school students should be familiar with.

-

Iwas apprehensi­ve when I volunteere­d for a hobby programmin­g session, and when it was suggested that Scratch would be the ideal environmen­t to work in. I had never used MIT Scratch and am more comfortabl­e 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 environmen­t that appealed to the students. After all, animation is far more exciting!

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

TurtleArt

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

$ sudo dnf install sugar-turtleart

$ cd /usr/share/sugar/activtitie­s

$ sudo ln -s TurtleBloc­ks.activity TurtleArt.activity $ cp TurtleArt.activity/turtlebloc­ks.desktop ~/Desktop/

Now, clicking on the TurtleBloc­ks icon should start the TurtleArt developmen­t environmen­t.

TurtleArt is an implementa­tion of the LOGO programmin­g 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 illustrate­s 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 illustrate­s 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

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 constructi­ng 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 manipulate­d as an entity. Each sprite can be represente­d 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 programmin­g.

The example shown in Figures 2, 3 and 4 uses the default sprite with two costumes and a sound. Add three background­s 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 enhancemen­t of Scratch and called BYOB (build your own block). Meanwhile, Scratch 2 was released, offering the same functional­ity, though written in AdobeAIR. Snap! is a new implementa­tion 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 directorie­s should be enabled. For example, on Fedora, it is in the file /etc/httpd/ conf.d/userdir.conf.

Browse http://localhost/~anil/.

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

In a revised implementa­tion 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 environmen­ts allows a student to learn and create complex projects, which is fun.

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

 ??  ?? Figure 1: TurtleArt script
Figure 1: TurtleArt script
 ??  ?? Anil Seth
Anil Seth
 ??  ?? Figure 5: Snap script
Figure 5: Snap script
 ??  ?? Figure 2: Scratch script for the sprite
Figure 2: Scratch script for the sprite
 ??  ?? Figure 4: Scratch stage scenes
Figure 4: Scratch stage scenes
 ??  ?? Figure 3: Scratch script for the stage
Figure 3: Scratch script for the stage

Newspapers in English

Newspapers from India