Saturday, February 5, 2011

Lesson 2: Objects

Last week, we learned pixels, and painted a copy of The Pear.  She looks great, so The Monster was ready to put her into Scratch!

Now was the time to talk about what an object is, or at least an object in the language of Scratch.  We talked about how computer programs are comprised of data and instructions, and that when we think of an object, it's about combining the data and instructions into one thing.  In Scratch, this works very cleanly with the fact that scripts are attached directly to sprites.  The programmer never dictates what data (sprite) is being addressed by the script, because the script is tied directly to the sprite (no sprites = no code).

Scratch is imperfect here too... it's actually quite tricky to instantiate a new sprite, and I made the mistake last week of having The Monster create a new "costume" (new sprite animation state) from the original default sprite, rather than starting anew with a fresh sprite.  A little tomfoolery though and The Pear was its own sprite, and we were ready to go.

I was amazed at how quickly The Monster was able to get The Pear under her command.  I gave her the assignment to make The Pear do a complete spin and though she didn't do it the most elegant way (hmm, looks like maybe loops are a future lesson?), her solution does indeed have The Pear spin, just about right:

(sorry for the lousy photo quality)

Well, almost.  The Pear was spinning a little bit too much.  Each of the steps she plopped in was rotating the sprite 15 degrees, and she was overspinning a bit.  Time for a lesson in measuring angles of a circle.

Being a modern soul, I couldn't even find a protractor on my desk... but the internets quickly remedied that.  We looked at a picture of a protractor and talked about one circle equaling 360 degrees, and the various subdivisions of that, including the 15 degree increments her script was using.  I then took out a pocket calculator (which doubles as an iPhone) and asked her if she could determine how many of these 15 degree steps would really be required to get to exactly 360 degrees.

This was a tough one.  She knew that the problem was division, but had to work her way through what gets divided by what.  Eventually she sorted out it was 360/15, which provided the answer:  there should be 24 steps.  When we made sure there were exactly 24 steps, sure enough, The Pear spins perfectly.

I thought that was enough for a day, but The Monster wanted to plow forward!  So, sticking with today's lesson of objects, she imported a new sprite, gave it its own behaviors, and we watched the two sprites operate independently on the screen, not interfering with each other, as proper objects shouldn't.  Right now, she's making a brand new sprite, which she says is an enemy.  It'll apparently get its own behaviors too.

Go Monster, go!

No comments:

Post a Comment