Tuesday, December 13, 2011

Project: Magic Wand

With Christmas so near, I sense magic in the air! Magic is exactly what we are going to program today - Okay, maybe that doesn't make it magical anymore…but anyways, this is what we will create:

I am not embedding the online version here because the wand does not behave as it should online. So, to see it in action you will need to download and run the project on your computer.

There are 16 sprites and 33 scripts in this project. Only five of them are unique - the rest are copies - and only four truly matter. Let's see what we have…

The wand

The wand is easy to code. Easy to create too. I drew it on the Scratch Paint Editor using two lines of different shades. Its script simply instructs it to follow the mouse pointer at all times:

Scripting wand to follow the mouse pointer

The stage

I put this script on the Stage, but it could really be on any sprite. All this script does is to broadcast a "click" message when it senses a mouse down event.

Broadcast 'click' when mouse down event occurs 

The magical round things

These are the stuff that emerge out of the wand tip. They each have a copy of the same script and their own variables.

A "magical round thing" (yes, I have no better name for it. Ideas, anyone?) has four variables:
  • outOfWand indicates whether the sprite has shot out of the wand and is moving on the stage. When outOfWand is 0, it means that the sprite is not on stage - you can think of this as the sprite being "inside the wand" and has not emerged yet. When outOfWand is 1, it means that the sprite has left the wand and is out on stage.
  • v is the y-coordinate change for the sprite.
  • dv controls the rate of fall for the sprite. You would easily understand what v and dv do if you have seen past tutorials and projects related to falling. (If you have not, you may want to take a look at some of these: Programming Falling Objects in Scratch, Three Ways to Program Falling Objects, Keep Afloat, and Run, Scratch Cat, Run!)
  • u is the x-coordinate change for the sprite. We want the sprite to fall some distance to the left or right of the wand. u is the change in x that will help do this.
We will soon see how these variables are used. Before that, we need to do some initialization:

Initialize outOfWand to zero when program starts
At the very start of the program the sprite stays "in the wand", so we set outOfWand to 0.

Setting values of variables when 'click' message is received
Then, when the sprite knows that the the mouse is clicked, it will proceed to set the other variables. However, it only does this when it is not out of the wand yet. We really don't want to mess with these variables if the sprite's already on stage. Note that it now sets outOfWand to 1, indicating that it is now ready to emerge. It then positions and shows itself at the wand tip with the go to Wand and show blocks.

The rest of the code in the repeat until (outOfWand = 0) block controls the behavior of the sprite after it has emerged:

Behaviour  having emerged from the wand
First, it changes its colour effect. Then it falls down and to the right or left depending on the random values that u and dv were set to.

It also checks whether it is touching the edge of the stage. If it is, then it hides itself and sets outOfWand to 0 to indicate that it has left the stage and - in a sense - has gone back in the wand. This means two things:
  1. The repeat until block terminates, since outOfWand is now 0.
  2. The next time the mouse is clicked, the if (outOfWand = 0) block will evaluate to true and it will continue the process of resetting its variables and emerge again.
We make 14 duplicates of this sprite and finally have…our magic wand! You may experiment by changing other effects like size and ghosting, or make more duplicates of the "magical round things". Have a magical time programming.

No comments:

Post a Comment