+ All Categories
Home > Documents > 1 Project designed and created by M. Shajith Kumar.

1 Project designed and created by M. Shajith Kumar.

Date post: 19-Jan-2016
Category:
Upload: vanessa-newman
View: 214 times
Download: 1 times
Share this document with a friend
15
1 Draw a multicolor flower Project designed and created by M. Shajith Kumar
Transcript
Page 1: 1 Project designed and created by M. Shajith Kumar.

1

Draw a multicolor flowerProject designed and created by

M. Shajith Kumar

Page 2: 1 Project designed and created by M. Shajith Kumar.

2

In this project we build a multi-coloured flower like the one below using the Scratch Programming environment

Draw a multicolor flower

Page 3: 1 Project designed and created by M. Shajith Kumar.

3

Let us first break up this problem into manageable parts:

In programming we call this “Decomposition”

The reason we decompose is to solve each individual smaller problems and then combine together those solutions to address and solve the larger problem.

“Pattern Recognition”

If you observe the flower closely, you find that it is made of individual arms (we call it petals) and each petal is identical except for the colour. You can also see a pattern in which the petals are arranged to create the flower.

So, if we can draw one petal, then we should be able to draw that flower… may not be that easy, but should definitely be possible.

Drawing the petal

If you examine the petal, it is thin and point sized at one end and thick and oval at the other.

So, if we have a pen which can grow in size as we draw, then we should be able to draw a petal.

Let us see how we create this in Scratch

In this flower project, we need to use a concept that computer scientist use very regularly :

Building our understanding of the problem

Page 4: 1 Project designed and created by M. Shajith Kumar.

4

Set the pen size to the lowest value 1

Let’s make the pen (i.e the cat) move. Let us try 10 steps first

Now the size of the pen should change by some value - Let’s say 5

1

5

STEP 1

STEP 3

STEP 4

Now, put the pen down, ie. let it touch the screen STEP 2

10

We now repeat Step 3 and 4 a definite number of times. For example, if we repeat 3 times, our code should look like this

But, instead of writing the sameset of code again and again, we can use, the

block from the “control” tab in the “scripts” menu to repeat a finite number of times

Our code will now look like this

STEP 5This is our

1 time

2 times

3 times

Steps for drawing one petal

Page 5: 1 Project designed and created by M. Shajith Kumar.

5

Test your Understanding

1. What will be the pen size at the end of repeating 20 times for the code below?

2. In this code, the blocks within the repeat loop has been interchanged. Will your answer to question 1 be the same in this case also?

3. Will the above two codes give the same result i.e will they draw the same thing?

Try to answer the questions only by looking at the code and then check your answer by trying it out in Scratch. This will help you improve your thinking skills and also code reading skills (i.e ability to understand by just looking at the code). For questions 1 & 2, it is not possible to test the answer using Scratch. So think smartly

Page 6: 1 Project designed and created by M. Shajith Kumar.

6

The block gives us a short cut to save time. Imagine if we had to do the same set of blocks a 1000 times. We would have taken a whole day to arrange the blocks (or write the code if we were doing this in a text based programming language like Java, Python or C++).

The repeat block in Scratch is what we call in programming “Loops”

Loops are convenient as it allows us to repeat the same set of blocks any number of times we want to. In our example and are repeated 3 times. If we want to make it 50 times, we just have to type 50 inside the white oval area within the block

510

Getting back to our petal, lets see how our code works, but before that let us add a few more blocks just above the blocks we had created

We will discuss why we add these blocks shortly

Our petal code should now look like this

Steps for drawing one petal

Page 7: 1 Project designed and created by M. Shajith Kumar.

7

If there is something already drawn on the screen, this block will erase it. This block ensures that we start with a clean slate

“Pen” tab” in “Scripts” menu

The stage in Scratch is like a graph and has (x,y) co-ordinates. Setting it at (0,0) will put the sprite at the middle of the screen when it starts drawing the petal

“Motion” tab in “Scripts” menu

This is the direction which the sprite will face. 90 (face right), - 90 (face left), 0 (face up) and 180 (face down)

“Motion” tab in “Scripts” menu

For the entire script (all the blocks together) to work, the user (you) have to do something. It could be, clicking the mouse, pressing any button on the keyboard etc. In this case we have chosen clicking the green flag on the screen. Only then will Scratch execute all the other blocks below it

“Events” tab in “Scripts” menu

Why did we used additional blocks

Block locationBlock Why use this block

Page 8: 1 Project designed and created by M. Shajith Kumar.

8

Now, let us try running this script

Click on the green flag.

Can you see what happens?

Noting much changes, right?

To see what has happened, just pull the cat away using your mouse. You may see the below picture on the screen

A very small petal has been drawn. Let us see why that has happened.But before that try clicking the green flag again.

Oops! The petal has become a weird design depending on where you dragged the cat. Why?

The reason this happens is that the pen is still down as cat travels from where it has been dragged after executing the block to the point (0,0) while executing

Cat dragged slightly to the right

0 0

Therefore, we have to add ablock at the end of our script.

Now, try clicking the green flag again. If the same thing happens, click the green flag again. It will work.Our revised script will be

Cat dragged bottom left

Now, let’s try to make the petal to look like what we want to.

Up & Down with pen

Page 9: 1 Project designed and created by M. Shajith Kumar.

9

To get us our ideal petal we have to tinker around with 3 values within our repeat loop.

Number of times to repeat the blocks in the loop Steps to move in each repeat

Size by which the pen changes each time

The values that we provide in the white oval spaces are called “Arguments” or “Parameters”. So, if we change one or some or all the arguments, the petal should look different.

1. In the script the repeat loop executes the block within the loop 3 times. What is the size of the pen after repeat loop is executed 1 time, 2 times and 3 times?

2. What is the total length of petal (in steps/ pixels) after the entire script is executed?

Quick Questions

Try it yourself

Change the values of the arguments and try to create your own version of a petal. See how

changing each argument changes the shape, length and smoothness of the petal.

To get the petal as in the flower in our opening page of this lesson, the arguments used are :

250 3

Tinkering the petal code

Page 10: 1 Project designed and created by M. Shajith Kumar.

10

We have now finalised our code for the petal which should look like this

But our cat is at the end of the petal with the pen up.

So, to draw the next petal, the cat has to be brought back to its initial position, i.e (0,0) usingand adding arguments 0 and 0 in the white oval space.

Now our cat is back to its initial position.

Think & answer

1. What is the size of the pen after the cat completes drawing a petal?

2. Should we now change the size of the pen back to 1. Which block should we use to do that?

The size of the pen is so large that when it returns back to draw the next petal, we need to reset it to its lowest size. Otherwise, our next petal will be jumbo size. Our script will now look like this

Our cat is back to its original position

The final petal code

Page 11: 1 Project designed and created by M. Shajith Kumar.

11

So, to draw each petal, the cat has to rotate 360 divided by 16 i.e 22.5 degrees each time in one direction (to its right or its left). For doing this in Scratch, we either use the or and add the argument 22.5 inside it. The turn blocks are available in the “motion” tab.

To draw our next petal, our cat has to turn a little bit and then we run the same script which we have made for one petal to draw the next petal, the next petal and so on. The number of times we do this depends on the total number of petals in our flower. In our project, the total number of petals are 16.

We use our understanding of geometry to draw the whole flower. To draw the whole flower, the cat has to rotate a full circle. To go a full circle we need to rotate by 360 degrees.

22.5 o

22.5

Our code will now look like this

Rotating the cat

Page 12: 1 Project designed and created by M. Shajith Kumar.

12

Try running this code now:

Does it draw a flower?

No. Why?

Let us analyse the code and see what each block or set of blocks does and find out what is missing to complete drawing a flower.

We have to do to to complete drawing one petal and get ready to draw the next petal.

So, if we repeat this 16 times, we will be able to draw the complete flower.

The loop draws one petal

Puts the pen down on the screen

Withdraws the pen from the screen (but still has the pen)

Brings the pen size back to 1 from 51 when competing the loop

Turns the cat 22.5 degree to the right (getting ready to draw the next petal)

1

23

45

6

We now repeat blocks to 16 times inside a repeat loop to draw the complete flower. 6

6

7

89Brings the cat back to the middle of the screen

10

11

11

Getting ready for next petal

11

Page 13: 1 Project designed and created by M. Shajith Kumar.

13

Blocks that puts pen down, draw one petal, pulls pen up, returns to initial position, resets size of pen and turns 22.5 degrees

Repeat block that repeats all that is done inside 16 times

These blocks help set initial values before the functional code (i.e code that draws the flower) is executed. These values will change as the script is run. This is called “Initialisation” and is a very important concept in programming.

Understanding the set of codes

Page 14: 1 Project designed and created by M. Shajith Kumar.

14

Now our code draws a flower but not exactly what we intended. Our flower is single coloured but we had earlier decided to draw a multi-coloured flower with each petal having different colour.

To do this, we have to set an initial value for the pen colour and then change the value by a constant number as we change from one petal to another. In Scratch, we can initialise the pen colour using and change the colour every time inside the loop that draws a new petal (the outer loop that repeats 16 times) using the block. In Scratch we can use colour in the range 0 to 200.

Let us set the initial value to 0 and change the pen colour by 10 every time a new petal is drawn.

Final code after adding colours will look like this

Initialised colour

Changing pen colour for each new petal

The Final code

Page 15: 1 Project designed and created by M. Shajith Kumar.

15

Exercise

Now that you know to draw a flower, try to do the following

1. Draw a single coloured flower with 8 petals2. Draw different coloured flower with 5 petals which are thicker in size3. Draw a flower with a fixed petal size of 54. Draw a flower which has petals that is thicker in the beginning and

thinner at the end


Recommended