KTurtle/C3/Grammar-of-TurtleScript-Part-II/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Resources for "Grammar of TurtleScript - Part II"

Title of script: Grammar of Turtle Script-Part II

Author: Sindhu

Keywords:



Visual Cue
Narration
Slide 1 Hi everyone,


Welcome to the tutorial on Grammar of Turtle Script-Part II.

Slide 2 If this is the first time you are using KTurtle,


please view the tutorial Introduction to Kturtle, which will help you understand the installation and interface of the application, and also some of the basic commands to move the turtle.

Slide 3 Also view the tutorial Grammar of Turtle Script-Part I,

to understand few more commands used in the Kturtle environment.


These tutorials can be viewed on the spoken tutorial website (www.spoken-tutorials.org).


This tutorial will introduce some more commands to be used in the KTurtle environment.

Slide 4 We will learn the
  • learn command
  • random command


Slide 5 For this demonstration, I am using the Linux Operating System Ubuntu v11.04, and Kturtle v0.8.1 beta.


Let us first look at the learn command.

Slide 6


'learn' Command


learn is special as it is used to create your own commands.


The commands you create can take input and return output.


Let us take a look at how a new command is created.

Switch to Kturtle Window

type the following code:

repeat 4

{

forward 10

turnleft 90

}


I will launch the Kturtle application


Let us enter the following lines of code in the editor to draw a square:


here the number 10 specfies the length of the side of the square.


Now let us learn the commands involved to draw a square using the learn command.


We will name of this set of commands to draw a square as square.


The command learn is followed by the name of the command to be learnt,


in this case it is square.


So i will enter the command learn before the command square


learn square $x

{

repeat 4

{

forward $x

turnleft 90

}

}


So the new command that we have defined here is called square.


square takes one input argument, i.e $x to set the size of the square.


One important point to be noted here is,


when you run this piece of code,


square returns no output.


The command learn is just 'learning' the other command square to be used later.


The square command can now be used like a normal command in the rest of the code.


Let me add few more lines of code here.


Type the following code in your editor:


learn square $x

{

repeat 4

{

forward $x

turnleft 90

}

}

go 200,200

square 100


Let me select Run from the toolbar to run this piece of code from the editor.


The turtle now draws the square on the canvas.


The command square can now be used anywhere and any number of times in your program now.


Note that this command can be used only within the scope of this program.

Switch to Slide 5 'random' Command


random is a command that takes input and gives output.


The syntax for this command is random X,Y


where X and Y are the two inputs.


X sets the minimum output and Y sets the maximum.


The output is a randomly chosen number between X and Y.


Let us pu the random command to use in the application.


In my editor, I will enter a few lines of code to draw a circle.


reset

$x=random 1,20


repeat 36

{

forward $x

turnleft 10

}


Here, the command random 1,20 selects a random number which is equal or greater than 1 and equal or lesser than 20,


and assigns it to the variable x.


When you run this piece of code, a circle with a radius of size, which is in between 1 and 20 is drawn on the canvas.


Let us execute this code a few times, and you can see that a circle with a different size is generated each time.


Every time you exceute this code, a circle with a different radius is drawn.

Let us now use both the learn and random commands in an example.


Here, i have with me few line of code that demonstrates the use of both the learn and the random command.


i am going to copy the entire code onto my clipboard and paste it in my editor in the Kturtle application.


reset

canvassize 300,300


$R= random 0,255

$G=random 0,255

$B=random 0,255


canvascolor $R,$G,$B


$red=random 0,255

$blue=random 0,255

$green=random 0,255


pencolor $red,$blue,$green


penwidth 2


learn circle $x{

repeat 36{

forward $x

turnleft 10

}

}


go 85,85

circle 5


go 115,115

circle 5


go 145,145

circle 5


go 190,190

circle 5


go 220,220

circle 10


go 0,0


Let us see what each line means here.


'canvassize 300,300' sets the width and height of the canvas to 300 pixels each.


$R, $G, and $B are three variables to which I am assigning random values between 0 and 255.


In the command 'canvascolor $R,$G,$B' ,


the Red-Green-Blue combination is replaced by the values assigned to the variables R, G, and B in the previous step.


The canvas color is set randomly when this command is executed.


$red, $blue, $green are another set of variables


to which values between 0 and 255 are assigned randomly.


Again in the command 'pencolor $red,$green,$blue' the Red-Green-Blue combination values are replaced by the variables


$red, $green and $blue to which random values were assigned


in the previous step.


The color of the pen is also set randomly when the command is executed.


'penwidth 2' sets the width of the pen to 2 pixels.


I have next entered the code to learn to draw a circle.


Here $x represents the size of the circle.


The next set of commands i.e the go commands


followed by the circle commands draws circles with the specified sizes.


The command circle 5 draws a circle of size 5


at the co-ordinates X and Y specified in the go command.


For each circle, I have specified different positions on the canvas.


Lets execute this code now.


I am running this execution at full speed.


But you can execute your code at any of the speeds specified in the Run option.


I will run this code few more times, so that you can see the difference in the randomly set values of pen color and canvas color.


When I execute the code one more time, note the change in the color of the pen and the canvas.


You can exceute this code how many ever times you want and you can note the change in the randomly set values.

Switch to Slide 6


Switch to KTurtle Window & Show Assignment

Assignment


As an assignment for you to solve,


using the learn command,


draw a pentagon, a square, a rectangle, a hexagon on the four corners of your canvas


and a circle at the centre of the canvas.


Using the random command create various colors and customize your geometric shapes and the canvas.


I have created my own customized canvas for the geometric shapes,


and your assignment should look similar to this.


Let me run the piece of code in the application.


You can see the geometric shapes and the color of the canvas changing each time.

Switch to Slide 7 I would like to acknowledge the spoken tutorial project

which is part of the talk to a teacher project.


Supported by the National Mission on Education through ICT, MHRD Government of India.


And you can see more information on this on the Spoken Tutorial Website.

Switch to Slide 8 Thank You.


This is Sindhu signing off. Enjoy playing with KTurtle!

Contributors and Content Editors

Chandrika