Jun 04 2006
Magic Mushrooms!
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.

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.
- 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 itlump_rotation, as it will hold information about the current rotation of the object. - Increment the field
For the moment, most of the interesting stuff happens in the Actor’s
actmethod. You see, Greenfoot constantly is poking at our Lump, saying “Hey! Act! Do something!” When you hit the “Act” button, theactmethod gets invoked once; when you hit the “Play” button, it does this over and over and over. However, if there’s nothing in theactmethod, nothing will happen. For now, I’m going to do two things in theactmethod, and the first one is to increment the value oflump_rotation. Specifically, I’m going to add one to the value oflump_rotation, and overwrite the existing value oflump_rotationwith this new value. This is given by the code
lump_rotation = lump_rotation + 1;
- 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.

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.