Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, June 16, 2015

Pac-Man is Chasing my Planet!

My child's class was recently introduced to Cartesian coordinates in Math. And in our coding class, we have been practicing interactive programming for the last few weeks. So, I thought of putting together a very simple template that  combines both the concepts, that the kids could then remix...  Pac-Man is Chasing my Planet! was the result... took me less than 10 minutes to put together and the kids loved it.

We went through the Pac-Man template code as a group & discussed the use of the XY coordinates. I showed the kids how the XY coordinates displayed under the Scratch animations area change, as I move the cursor around on the screen. Our discussion then proceeded along the following lines:


  • If I wanted my sprite (the planet, in this case) to move anywhere the cursor moves, what values should I use for the sprite's X and Y coordinates? 
  • The above point was also a good place to talk about variables, and how the change of the cursor position is always reflected in the planet's position. 
  • Should I move the planet around for just a few times or all the time? What kind of a loop should I use here?
  • How can I make Pac-Man always follow the planet? Which loop should I use? 
  • What values should I use for Pac-Man's X and Y coordinates? 
  • Here, the kids quickly saw that without a small degree of separation between the coordinate values of the planet & Pac-Man, the two sprites overlap each other.
  • BTW, the "if-else" clause was purely optional, for those to wanted to add another level to their game. The majority went with just a "go to x() y()"
  • And finally, the interactive part of the game... I put in the requirement that there should "a key press" or "the green flag click" to make the game start, and something similar to end the game. 

In the next one hour, the children came up with multiple variations of the game, making their own sprites and designing various versions of tag... All in all, a very fun class for them and me to wrap up the school year.

Happy Summer!!

Saturday, February 28, 2015

A Scratch Project for Drawing Tangrams and Compound Figures


Here is my Scratch project that draws two sample tangrams and the compound figures from this assignment (using Pro-Bots) created for grade 5 students.

I have created 5 different procedures for drawing the 5 components of the tangram. Please note that this is just one way of coding the figures; there are multiple correct ways of writing the programs for the same.


The functions (procedures) that are used for drawing the various polygons in the tangram can be found under the blocks titled:
  • parallelogram
  • square
  • largeTriangle
  • medTriangle
  • smallTriangle

The functions (procedures) for the compound figures that use the square and the triangle can be found under the blocks titled:
  • Square+Triangle
  • Square+Triangle_2

The functions (procedures) for the compound figures using the parallelogram and the small triangles can be found under the blocks titled:
  • TriParall_1
  • TriParall_2

I have given the code for two tangrams here as samples: one is the Monk with No Foot from the famous "Two Monks Paradox" and the other is a tangram for a House. The functions (procedures) for these can be found under the blocks titled:
  • MonkNoFoot
  • HouseTangram



Note:   The various dimensions in the assignment for Pro-Bot have been multiplied by a factor of 10 in my Scratch project, as we are dealing with pixels here and not centimeters, as in the case of Pro-Bot. 


Saturday, August 9, 2014

A Follow-up Exercise to Drawing Digital Numbers using Pro-Bot

The following set of questions is a follow-up to drawing digital numbers using Pro-Bot. Rather than draw every number from 0 through 9, these questions allow the students to draw just a sub-set of the numbers. This is how I plan to use it with my students: Write out one or two of the following questions each on chits and let the kids pick the chits randomly. This way, everyone gets to work on a different problem and it allows for a bit of math practice as well.

  1. Use Pro-Bot to draw the odd numbers from 0 to 9 according to the dimensions given in the figure.
  2. Use Pro-Bot to draw the even numbers from 0 to 9 according to the dimensions given in the figure.
  3. Use Pro-Bot to draw the missing number in the following pattern: 0, 3, ___ , 9, 12
  4. Use Pro-Bot to draw the missing numbers in the following pattern: 13, 11, ___ , 7, ___, 3
  5. Use Pro-Bot to draw the numbers that are multiples of 3 in the group 0 to 9.
  6. Use Pro-Bot to draw the numbers that are divisors of 8 in the group 0 to 9.
  7. Use Pro-Bot to draw the number that you get as the result of the problem 2 x 2 x 2 = ?
  8. The sum of 2 numbers is 5. One of the numbers is one more than the other. Use Pro-Bot to draw the two numbers according to the dimensions given.
  9. The difference between two numbers is 3. The sum of the two numbers is same as 5 x 3. Can you identify the numbers? Draw the numbers using Pro-Bot according to the dimensions given below.
  10. If your birthday is a single digit number, draw it using Pro-Bot with the dimensions given below. Else, add up the digits in your birthday and draw that number using Pro-Bot.
  11. Use Pro-Bot to draw the prime numbers in the group 0 to 9. A prime number is a number greater than 1, that can be divided only by 1 and the number itself. It does not have any other divisors.


All measurements in the figure below are in centimeters. 





















Here is a set of programs for drawing the various numbers.


Thursday, July 31, 2014

Solutions: Art with Pro-Bot - A Soccer Game

The soccer game picture is a follow-up to the "Art with Pro-Bot: A Dancing Robot" exercise, with one of the figures being the exact same as the Dancing Robot and the other being its mirror image.


Let's name the figure on the left as Team A and the one on the right as Team B. You would recognize from the figure that both Team A and Team B share some parts in common, such as the head and the neck. So, it's easier for the programmer to write the code for these parts just once and store it as a procedure; it can then be used multiple times. 

// Proc 1 - procedure for the neck and head


Fd 1
Lt
Fd 1
Rt
Rpt 3 [
Fd 2
Rt
]
Fd 1





Team A:  


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Rt
]
Fd 3
Lt
Proc 1


Team B:


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Lt
]
Fd 3
Rt
Proc 1


Art with Pro-Bot: A Soccer Game


Let’s use Pro-Bot to draw a picture of robots playing soccer.

We shall work using both Repeat Loops and Procedures in Pro-Bot for this assignment. This "art" project was designed to use the concepts of Repeat Loops and Procedures in a fun way.

There are 2 robots in the picture, one representing Team 1 and the other, Team 2. The two robots are playing each other in a game of soccer. The dimensions are as follows: Each robot's body is a 6 cm side square, the limbs are 4 cm long, the neck is 1 cm long and the head is a 2 cm side square. 

While writing the programs for Pro-Bot to draw the figures, remember to use Repeat Loops for patterns that repeat. You can also see from the pictures that the head and neck is exactly the same for both pictures. Hence, you can write the program for the head and neck once, store it as a procedure in Pro-Bot's memory and call it for drawing both robots. 

Click here for a set of solutions.

Draw in as many members for each team as you like. You can even store the programs for each robot as separate procedures which makes it easier for you to draw multiple figures in your art project. You can put in a background of your choice and draw a soccer ball in the middle once you have drawn both teams. It does not have to be just soccer; students in my class have used this theme to depict scenes from various sporting events. We worked on this project the day after a major basketball game and there were quite a few children who made pictures with basketball scenes... Let your imagination run free in creating your art work. 























Wednesday, July 30, 2014

Procedures in Pro-Bot


Let’s start this lesson by taking a look at your typical school day. What are some of the functions that you perform on a given school day? Here are a few: you wake up, make your bed, brush your teeth, take a bath, get dressed, eat breakfast, travel to school, study, play, eat lunch, study again, travel home, play, sports/activities, watch TV, read a book, do homework, eat dinner, brush your teeth, sleep... If you look closely at each of these functions, you notice that each one of these involves multiple steps.

For example, brushing your teeth more or less involves the following steps: (1) pick up toothbrush (2) pick up toothpaste (3) squeeze the toothpaste onto the brush (4) actual brushing of the teeth (5) open the tap for water (6) fill up your glass (7) close tap (8) rinse your mouth (9) put away the toothbrush and toothpaste.

We bunched up all these steps and gave it a unique name: “brush teeth”. When your dad or mom wants you brush your teeth, do they say all these 9 steps one by one, or do they just say “brush teeth”? Why? Because you understand what “brush teeth” means. You have been taught in your younger days what that means and how to do all those 9 steps. All those steps are in your memory. So when you hear it, you know all the steps to do.

Now let us take a look at a computer… like your Pro-Bot. Don’t you think if a computer is first told how to draw a 6 cm side square, then each time you want to draw a 6 cm side square, you should be able to say a unique name like “square” or draw square”, or something like that, instead of telling it each time “Fd 6, Rt, Fd 6, Rt, Fd 6, Rt, Fd 6, Rt”? Guess what? The computer can do that! And that is what we call “Procedure”.

A procedure is a set of computer program instructions that performs a specific task.
  
In your daily life example, “brush teeth” is a procedure. It is a small group of instructions that performs a specific function. Any time your parent wants you to perform this function (which involves those 9 steps), he or she would just call it by the name “brush teeth”, rather than specify every step involved in it. This way, they get to reuse the “procedure” they taught you and made you memorize. Similarly, in a computer program, you write those steps once into the computer’s memory and give it a unique name. Then you can use it multiple times in your program.

Maybe, the dentist has advised you and your parents to include flossing into your routine. Flossing is also a procedure just like “brush teeth”. It involves some of these steps: (1) take the box of floss, (2) cut off about 10 inches of the flossing thread (3) coil it around your fingers (4) insert it between 2 of your teeth and pull (5) repeat this for all the gaps between your teeth (6) uncoil the string from your fingers (7) throw it into the garbage. Your parents work with you to teach you these steps until you have learned it. Then while brushing your teeth, he or she may remind you to “floss”. They don’t have to tell you all those 7 steps every time anymore. So here you can see that the “floss” procedure is called from within the “brush teeth” procedure.

Now let’s go back to the computer. Let’s say you want it to make a beeping sound after you draw the square. What would you do? You could write a procedure to make that beeping sound. Then call that “beep” procedure from within your “square” procedure after all those steps to draw the square. In this way, you just easily modified your first procedure to do something different.

Thus, you can call other procedures from within a procedure.

Back to our “brush teeth” example. You may decide to floss before you do other steps in your “brush teeth” procedure, or after you have done those steps. Similarly you may decide to make the Pro-Bot “beep” before it starts drawing the square instead of after drawing the square. Can you see how easy that would be? You just have to call the “beep” procedure first instead of last within your “square” procedure.

You can think of “school day” as a Program, and functions like “wake up”, “make bed”, “brush teeth”, etc., as Procedures inside the program. Now, try writing two different programs using the procedures for the daily functions: one program for a school day and the other program for a holiday. You can easily see that you would not call every one of the procedures for either program. There might be some procedures that are common to both programs and there might be some that are exclusive to each. Such as, you don’t “travel to school” on a holiday, but you “brush teeth” every day. You might also call them in different orders for the two programs. For example, you may watch TV at different times during a holiday, than on a school day.

Procedures thus help us to customize our programs easily.

We already mentioned that procedures and programs can call other procedures from within. They can be called multiple times as well. You can write a procedure for drinking water. You might call this procedure multiple times throughout the day, while performing other functions. This means that you can call “drink water” from within your “eat lunch” procedure or from within your “play” procedure or from within your “do homework” procedure, etc.


How to create and edit a procedure for Pro-Bot
Pro-Bot lets you create and store up to 32 procedures in its memory. In the case of Pro-Bot, these procedures are named Proc 1, Proc 2, …, …, Proc 32.
Pro-Bot’s Menu button provides two options to access procedures:
·      New Proc
·      Edt Proc

Let’s discuss each of these options.
1.   New Proc:
Press the Menu button & then scroll down to “New Proc”. Choose this option when you want to create a new procedure. It will show you a list of 32 procedures on the screen. Choose any procedure name from this list (Proc 1 to Proc 32). Let’s say that you choose Proc 3. Your Pro-Bot screen will now show Proc 3. Type in the instructions for the specific function that you want Proc 3 to perform. Next, press Menu. This will take you back to the Main program. You can now call Proc 3 from your Main program anytime you want to use it. You do this by pressing the “Proc” button and then the number key 3.

2.   Edt Proc:
Press the Menu button & then scroll down to “Edt Proc”. Choose this option when you want to edit a procedure that you created earlier. It will show you the list of 32 procedures on the screen. Choose the procedure that you want to edit from this list. Let’s say that you choose Proc 3. Your Pro-Bot screen will now show the instructions that you put in earlier for Proc 3 (or an empty program if you put in no instructions earlier). Make the changes that you want for this procedure. Next, press Menu to go back to the Main program. You can now call Proc 3 from your Main program and it will provide you with the updated functionality.

For example; you can write a procedure to draw a square of side 6 cm. Let’s say we name this procedure “Proc 1”. Proc 1’s code will be “Rpt 4 [ Fd 6   Rt ]”. You can store this procedure in memory and anytime you need to draw a 6 cm square, you just call Proc 1 from your Main program.


In addition to the 32 procedures that you can create and edit, Pro-Bot also provides 5 other procedures with pre-defined names related to the touch, light & sound sensors. You can edit these using the Edt Proc option from Menu. These procedures are:
·      33 FRONT (runs when the front touch sensor is triggered)
·      34 REAR (runs when the rear touch sensor is triggered)
·      35 DARK (runs when the light sensor goes from light to dark)
·      36 LIGHT (runs when the light sensor goes from dark to light)
·      37 SOUND (runs when the sound sensor is triggered)

There are also 3 built-in, pre-defined procedures that you cannot edit, but can call from Main. These are:
·      38 HEXGN: draws a hexagon
·      39 DIAMND: draws a diamond
·      40 FLOWER: draws eight diamonds using Proc39

Note: You can call other procedures from within any other procedure. Just take care not to run procedures that call each other, in which case you would get into an endless loop.






Solutions: Debugging

1.  The program to trace the number 2 has bugs in instructions & data.

   Fd 6
   Rt
   Fd 6
   Rt
   Fd 6
   Rt          //Wrong instruction, Rt instead of Lt
   Fd 6
   Lt
   Fd 12    //Wrong data, 12 cm instead of 6 cm

Here is the program with the bugs fixed:

   Fd 6
   Rt
   Fd 6
   Rt
   Fd 6
   Lt      
   Fd 6
   Lt
   Fd 6  




2.  The missing square bracket for the Rpt instruction is a very common bug.

    Rpt 2 [
    Fd 6
    Lt
    Rpt 3 [
    Fd 6
    Rt
    ]
    Rt
    Rt
    // The closing square bracket is missing as the last instruction here.
    // Without it, the outer loop executes only once, not twice as intended.


Here is the program with the bugs fixed:

    Rpt 2 [
    Fd 6
    Lt
    Rpt 3 [
    Fd 6
    Rt
    ]
    Rt
    Rt
    ]



3.  A stack of squares, using a Nested Loop:  The given program has bugs in data, punctuation (missing square bracket) and missing instructions.

    Rpt 2 [     // Wrong data, need 3 squares not 2
    Rpt 5 [
    Fd 6
    Rt
    ]
    // Missing a few instructions here, as well as a closing square bracket for the outer loop.


Here is the program with the bugs fixed:

    Rpt 3 [  
    Rpt 5 [
    Fd 6
    Rt
    ]
    Fd 6
    Lt
    ]



4.  A square of sides 6 cm divided into two equal parts: The given program has wrong data and wrong instructions.
   

    Rpt 4 [
    Fd 6
    Rt
    ]
    Fd 2     // Wrong data, need to move 3 cm to get to the midpoint 
    Rt
    Bk 6    // Wrong instruction, need Fd 6

Here is the program with the bugs fixed:
    Rpt 4 [
    Fd 6
    Rt
    ]
    Fd 3     
    Rt
    Fd 6    



5.  Three squares stacked on top of each other. 

    Rpt 2 [     // Wrong data: need 3 squares, so repeat 3
    Rpt 5 [
    Fd 6
    Rt


    ]
    // Missing instructions here
    ]

Here is the program with the bugs fixed:
    Rpt 3 [     
    Rpt 5 [
    Fd 6
    Rt


    ]
    Lt
    ]

Debugging

Debugging is the process of finding and fixing “bugs”(errors) in your program.

Bugs can be of different forms, Some bugs that you would find in the following questions include instructions that have been typed in wrong, wrong logic, punctuation errors, missing instructions, wrong data, etc.

(1)   Here is a program for tracing the number 2 as seen on a digital clock using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program (the parts of the program that are not working correctly) and fix them. Circle the wrong instructions in the given program and write the correct ones instead.























Fd 6
Rt
Fd 6
Rt
Fd 6
Rt
Fd 6
Lt
Fd 12


(2)   Here is a program for tracing the following figure using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. Circle the wrong instructions in the given program and write the correct ones instead.





















Rpt 2 [
Fd 6
Lt
Rpt 3 [
Fd 6
Rt
]
Rt
Rt



(3)   Here is a program for tracing the following figure using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. See if you need to add any other instructions as well. Circle the wrong instructions in the given program and write the correct ones instead.






















Rpt 2 [
Rpt 5 [
Fd 6
Rt
]


(4)  Here is a program for tracing the figure below using Pro-Bot. Each side of the square is 6 cm long. The square is divided into two equal parts by the line in the middle. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program (the parts of the program that are not working correctly) and fix them. Circle the wrong instructions in the given program and write the correct ones instead.















Rpt 4 [
Fd 6
Rt
]
Fd 2
Rt
Bk 6


(5) Here is a program for tracing the following figure using Pro-Bot. There are three squares stacked up and each square has 6 cm long sides. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. See if you need to add any other instructions as well. Circle the wrong instructions in the given program and write the correct ones instead.