One of the nice things about Greenfoot is, once a student has got their head round the basic idea of an object that is walking about, they can make their own new objects or make changes to existing objects fairly easily. A scurrying crab can be given new artwork to become a scurrying rat. It can be made to do different turns at the side, or different random staggers. Very quickly, a student can fill the screen with a dozen objects that look different but have awfully similar internal code.
And that’s okay. Because at this stage, we are doing a lot of learning-by-repetition to reinforce key ideas. If a student is working quickly and gets bored waiting for the rest of the class to catch up with him, he can easily recreate CrabWorld as FishWorld and have randomly moving fish on a blue background. If he’s really ambitious and wants to have the fish pout occasionally, that’s great. If he wants a basic ‘fish’ with extensions of that model (shark, goldfish, etc), great. To a random person walking into the room, it may look like some kid who has gone off on a tangent and who is ignoring the purpose of the class. To me, he’s very much on-track with my objectives – making things move randomly round the screen.
Of course a game that has no interaction, just moving things, gets boring very quickly. We need some sort of interaction. “So, class, back to the crab and create a new object called lobster. Get him moving randomly, just like the crab. I’ll give you five minutes and then we’ll make the Lobster eat crabs.”
“Oooooohhhhhhh”…… they are already letting me know they like this idea.
Five minutes later, we have introduced this to the Lobster:
if (canSee(Crab.class) )
“Ooooh, the lobster eats the turtle…” and pennies begin to drop. They are starting to figure out how objects in Playstation and X-box games interact. They are moving from being end-users of stuff to becoming understanders and creators of stuff.
Back to Greenfoot- it’s a randomly moving bunch of crabs being chased by a lobster. Actually, nothing’s being chased – the lobster is totally blind and just stumbles on a crab now and then. It looks like this.
But a lobster chasing crabs is, on its own, not as much fun as it could be.
So we take out all the code that makes the lobster walk about at random. In its place, we start with:
if ( Greenfoot.isKeyDown("up") )
This checks if the ‘up’ key is pressed and if it is, the Lobster moved forward.
“OK class, make it turn left, right, go forward, go backward, etc.”
…and they did. Some even tinkered with random numbers to make a drunkLobster that responded to what you wanted, but only sometimes. Some even had the lobster randomly to the reverse of what you wanted. They played and played and played. They created little challenges for themselves overcome them. They shared ideas and it was great. This never happens with PowerPoint, I tell you!
At this point, though, most of them had very messy code that they understood but might be confusing to other people. Some were taking the initiative to re-write and tidy their mess. We had no methods. For the uninitiated, methods let us turn a very long program into a lot of smaller, easier-to-maintain programs. Twenty lines inside the lobster’s act() method, dealing with keyboard control, should really be a single line: checkKeys(), which goes off and talks to a checkKeys() method. Just take my word for it, it’s a lot easier to maintain and read.
So, our next challenge: tidy it up, have some procedures. And then, like manna, did arrive Dr Michael Kölling and his fantastic Joy of Code videos. More of that later.