A few weeks ago we had a Cool Stuff in Computer Science session dedicated to Greenfoot. Cool Stuff is an extra-curricular “course” open to students at the University of Kent where we explore… well, things they think are cool. Sometimes, we focus on things we think they’ll think are cool. (Did I get that right?)

Anyway, what I like about using Greenfoot in the classroom is the unpredictability of what students will do. I started things out by showing them “termites”, which is a world I created based on a world in StarLogo/NetLogo.

Termitessmall-1

Termites! (~400KB MP4, click to play)

Then, I introduced them to the (mostly empty) Hippo world.

Hippoworld

A hippo world

Now, I think the introduction matters. I sold them on hippos on the (false) belief that hippos are kind, friendly creatures that love nothing more than to wallow about in the water. Now, in truth, hippos are cantankerous creatures that are not to be trifled with. And, I think (but am not sure) that they are omnivores. They eat meat. I think.

Regardless of whether it is true, in my world, hippos eat sheep. Poor, defenseless, dumb-as-rocks sheep.

And that was it. Some introduction to how Greenfoot works, and how you actually add behavior to an Actor, and they were off. “Do something cool” was the extent of our instruction that evening.

The first world I’d like to share with you is Rage Lemons (hippos-ragelemon.zip). Below is a 2MB movie (MP4 encoded as well) of the hippos in action.

Ragelemons

Rage Lemons! (click to play)

You see, in this world, Hippos and Sheep live together, peacefully. Until, of course, a Hippo eats a Rage Lemon! At that point, the Hippo goes bezerk, eating any and all Sheep it comes across. Eventually, the powerful effect of the Rage Lemon wears off, and the Hippo resumes its normally peaceful life.

There’s a lot of microworlds out there. The original microworld was probably LOGO (Wikipedia), with its iconic turtle. Today, there are countless environments in which students can create characters, agents, worlds, universes… all programmable, all creative, all quite cool. Alice comes to mind (a 3D environment in a novel language intended for beginners), as does NetLogo (a massively parallel LOGO). And if you’re open-minded, things like the LEGO Mindstorms is a physical manifestation of these agent-based microworlds… a real, robot agent that kids of all ages can program and play with.

Why, on the Greenfoot weblog, would I be pointing to… the competitors? Humor aside, each of these environments provide a very different way of approaching the same ideas, and as users and educators, we stand to learn a great deal by looking at a broad spectrum of tools. At one level, all of these programmable microworlds provide opportunities for beginners to program in an interesting and engaging environment. At another, they challenge students to think about an interesting class of problems: software agents interacting with each-other, dynamically and (in some cases) unpredictably. This is critical in the real world: programs that interact with databases, webservers, and the like operate in fundamentally the same (unpredictable, agent-based) space.

In the case of Greenfoot, there’s an added benefit that the language encounter Java. Java is widely used in the world today, and the object is a reasonably natural way to represent an individual agent wandering free in the world. What I particularly like is that the notion of an object (an abstract concept that is often difficult to explain to a novice programmer) gains a visible analogue in Greenfoot: “that (ant, wombat, thing on the screen there) is represented by an object.” Students can inspect those objects (just like in BlueJ) and see how their agent’s state changes as it wanders about in the world. That, I think, is quite cool.

seagull-beach

Since I’m actually at the beach, I’m going to stop my musings on “why I like Greenfoot”… as I should be out enjoying the sun! However, I’ll continue these thoughts from time-to-time. If nothing else pulls me away, I have to make sure that the fish stew I’ve planned for this evening continues as planned. We have a nice, long-grain brown rice, 2lbs of fresh shrimp, 3.5lbs of assorted, firm white fish (all fresh off the boat!), lots of fresh tomatoes, and good garlic bread and salad to go with. Yummy.

The Marine Biology Case Study

Given that I’m on the shore, it’s a perfect time to mention the Marine Biology Case Study, which is now available as a Greenfoot microworld. This was an example project used by the US College Board for the AP Computer Science course; now, it’s far more interactive and interesting to play with. If I can’t wrangle a guest blogger here to walk us through the case study, I’ll dive in and do my own exploration. Marine biology and beaches: two great things that go great together. :)

I really want to apologize; I just started the weblog, and promptly went silent for too long! My intent is to post weekly. The last few weeks, however, were difficult. I was busy trotting the globe, obtaining work permits (so as to remain in the UK legally), and this was really quite stressful.

But, that’s over now… I’ve decided to become a refugee in the UK. Just kidding! Now that we’re getting back on track, it’s important to say the important things up front: Greenfoot is 1.0! This is excellent news; Poul, Davin, and Michael, and Bruce all put in some hard work to make Greenfoot ready for use sooner rather than later. Therefore, I need to step up my explorations.

Magic Mushrooms

In my last post, I built LumpWorld, which was a world with mushrooms that didn’t do anything. This wasn’t very exciting, but it demonstrated that we could do something in Greenfoot without ever writing a single line of Java. That’s kinda cool. However, I’m not content to produce LumpWorlds over and over… my Lumps need to move around, interact, and ultimately, take over the world.

We’ll start small, though. :)

My next steps will carry on from where I was, so you can create your own LumpWorld, and pick up where I’m at. If all goes to plan, I’ll have a bunch of mushrooms dancing around the world. To start, lets see what kind of code I’ve got in my Lump Actor.


walkabout-mm-002


Every Actor comes with a template of code for you to work with.

What does this stuff mean? When you write programs in Greenfoot, you’re writing code in the Java programming language. Now I’m not gonna lie to you: the Java programming language is large and complex. That said, if I can write code in Java, I’m confident you can make your way around it as well; besides, we’re going to take small steps to tackle the language. Also, I will say this (and will say it more than once): if you’re new to programming, grab a copy of Studying Programming. Yes, I helped write it, but so did a lot of other excellent people at the University of Kent. It’s intended for people just starting out writing programs, and I think there’s a lot of good stuff to help you learn how to read code and make sense of programming environments like Greenfoot.

Editing the code

Now that I’ve attempted to scare you off programming, and encouraged to buy the book that I helped write, let’s get back to the task at hand: making the Mushrooms in the world spin like crazy fungal tops.

  1. Add a field

    First, I’m going to add a field to my Actor; this is a place where we store information about the Actor. Technically, information about your Actors is referred to as the Actor’s state. The field will hold a number (without any fractional parts), so it will be of type int. And since I get to choose the name of the field, I’ll call it lump_rotation, as it will hold information about the current rotation of the object.

  2. Increment the field

    For the moment, most of the interesting stuff happens in the Actor’s act method. You see, Greenfoot constantly is poking at our Lump, saying “Hey! Act! Do something!” When you hit the “Act” button, the act method gets invoked once; when you hit the “Play” button, it does this over and over and over. However, if there’s nothing in the act method, nothing will happen. For now, I’m going to do two things in the act method, and the first one is to increment the value of lump_rotation. Specifically, I’m going to add one to the value of lump_rotation, and overwrite the existing value of lump_rotation with this new value. This is given by the code


    lump_rotation = lump_rotation + 1;
  3. Set the Actor’s rotation

    I cheated; I looked at the Wombat tutorial to see what Michael had done there. I thought rotating my mushrooms would be a good start. So, every time we’re asked to act, we increment our rotation, and then set our Actor’s rotation to that value. This is accomplished by the code


    setRotation(lump_rotation);

    Of course, there’s some details here that might be worth mentioning… but I’m going to gloss over them for the moment.

You can see all three of these changes below.

walkabout-mm-005

And that’s it! I’ve added three lines of code to my Lump, and now they rotate in a most excellent way. :) (Don’t forget; you need to compile your program before you can add Actors to the world and have them do anything interesting.) I was so pleased with the success of my experiment, I created a small movie of a spinning mushroom (right-click and “Save As”). It’s 7.8MB, and in in the Quicktime MP4 format. As I get settled in with making screencapture movies of Greenfoot, I’ll work out how to quickly and easily generate files that I know for certain will play on both Mac and Windows. For the moment, just drop me an email if things don’t work for you, and I’ll see what I can do.

Oooh… and I need to come up with a well-structured way to include/aggregate the source code from all my explorations. For now, here’s a zip file of the Greenfoot project that I ended up with when I was done spinning mushrooms all over the world.

What next?

Spinning mushrooms are all fine and good, but they’re still just lumps. I think it would be fun if they were mobile, and I think it would be great if they interacted with each-other in some way. We’ll see where the next installment takes us.

Questions?

I suspect I have some fancy greenfoot.org email address, but I’m not sure about that. For now, if you have any questions, don’t be afraid to drop me a note at my gmail.com address; that would be ‘jadudm’. Or, leave a comment here on the blog; I think I have moderation turned on (to block weblog SPAM), but that may change as I get the hang of using WordPress. Comments may someday loose to SPAM, mind you.

I want you all to know that programming for all to see like this is kinda like wandering through a crowded farmers market in your pajamas. Everyone can see how many mistakes you make, and that you’ve got a robe that’s just a bit too small, and you still like fuzzy slippers with funny little monsters on them.

My point is, don’t make fun of my first Greenfoot world. I’m proud of it, even if it doesn’t have wombats in it.

I called it LumpWorld.

To start, I fired up my freshly downloaded copy of Greenfoot. You may need to get Java 5 before you can get started; it’s a big download, so you’ll want to start downloading it slightly before you started reading this post.

First things first; I opened up Greenfoot, and I created a New project (File -> New). I called it “LumpWorld”, because… well, I figured my first world really wouldn’t do anything. So, everything in it would kinda be like a lump.


lump-01

Now remember Matt’s Mantra: documentation is for C programmers. I figure that I’ve hung around the BlueJ guys enough that I can just figure out how to use Greenfoot. So, I right-clicked on Actor, and chose New subclass. When given the opportunity, I called it “Lump”.


lump-02

(Question to self: If you’ve never programmed before, does New subclass mean anything whatsoever? Perhaps this should be New Protoge, or New understudy?)

What I was surprised by was the next dialog: I get to choose a picture for my Greenfoot Actor! Sweet. Now, I could go digging around my hard drive for something really lumpish, but for now, I’ll use a mushroom. They’re kinda lumpy.


lump-03

My Lump now has a picture, but it isn’t ready to go on-stage. To do that, I need to hit the Compile All button. This checks my code, and then gets my Lump ready to run around in the Greenfoot World.


lump-04

I have to admit, I was getting excited at this point. I right-clicked on Lump, and said New Lump(). I thought “Sweet. Mushrooms everywhere!” Turns out, though, I was wrong.


lump-05

I can’t put my mushroom down! You see, I’ve just created a new Lump, but not a new world. This may not be obvious, but I apparently need to have both a World and an Actor to play in it. And here I thought “… all the world’s a stage,” or something like that…


lump-06

If I right-click on World, I can select New Subclass again. Here, I’ve called my new World “LumpWorld”. To use it, I have to Compile All again.

Now, things change a bit! This was nice: I now have an empty world in which to place my lumpy mushrooms. Pretty cool.


lump-08

I know it seems silly, but this struck me as a really nice behavior. So far, I’ve written absolutely no code, but the environment is doing reasonable things for me… like giving me a titled, blank canvas to work on. Oh, the universes I will conjure!

(*cough* OK. It’s getting late. I get a bit loopy when I’m tired.)

At this point, I can right-click on Lump, and place one or more mushrooms in the world. Keep in mind, they’re not really mushrooms—they’re Lumps, but they happen to have an icon that looks like a mushroom. (In the future, I’ll try and use icons that look like the thing I’m creating. At least for a little while.)


lump-10

Bling! A world full of mushrooms! If you’re keen, you can run your world now:


lump-11

And nothing happens! Why? Because we haven’t made the Lump actor do anything. But making our Lump objects do nifty and exciting things is going to have to be for another time… because right now, I need to catch a bus to Chicago.