Greenfoot Gravitron tutorial -- student assignment
Find other posts in the same category: | | |

Where do you go to find it? (Put URL if it is a website)

Long description

download the tutorial here
http://www.greenfoot.org/greenroom/images/d/d5/Graviton_Tutorial.zip

Here is the student assignment for this lesson
I pasted the text in here so that it can be found by search engines such as Google. I've made minor edits to make it easier to understand and I've corrected errors.

Greenfoot Tutorial – Graviton

Graviton is a game that's a few years old. The idea is basically to guide a rocket to a target whilst avoiding obstacles along the way. However, what makes this game unique is the way the rocket moves – instead of using the arrow keys to move it up, down, left and right, you have to fire a booster and tilt the rocket to counteract gravity.
You're provided with a bare bones outline of the graviton game, and your job is to implement all the missing bits to make the game complete. When finished, the game will use two human players playing against each other, and will cycle through levels building up a score for each player.

Setting Up

If you haven't already, download the latest version of Greenfoot at www.greenfoot.org/download and install it (at the time of writing, the latest version is 1.4.1). When it's installed, open up Greenfoot, then locate and open the Gravitron scenario. If you haven't been given a location for the scenario you can download it at
http://www.greenfoot.org/greenroom/images/d/d5/Graviton_Tutorial.zip

There will be a single folder inside this zip file, extract this folder and then locate and open it with Greenfoot.

When you've done the above, hit the “Compile all” button in the bottom right hand corner, and you should have a screen similar to the following:

When you see this screen you're ready to start!
Stage 1 – Playing around

Before you dive into anything, spend a while familiarising yourself with the Greenfoot environment and the scenario. With any scenario, it's often a good idea to look at the information that's provided – so click on the “Scenario Information” button and see what comes up:
Here it should tell you the name of the project, the date it was written, the author, and it also tells you you need to follow a worksheet. That's this one!

You might also want to open up some of the classes and have a look at them. Don't be put off if you don't understand the code, but it might be helpful for later to poke around in the Documentation view of some of the classes. To switch from the source code view to the documentation view, look at the drop down box in the top right of the editor:
Next, let's take a look at what the scenario does already. To do this, hit the run button. Nothing happens? Try controlling the rocket with the arrow keys. Still nothing? If nothing happens, then that's just about right – you need to implement it all yourself!

Stage 2 – Assigning keys

The first thing is to actually get the rockets doing what they should. Open up the rocket class by double clicking on it, then have a look at the source code. You'll notice that there's lots of gaps where you need to add code.

To start with, we'll assign the player's keys. Have a look towards the top of the method, you'll see the constructor:

This is the piece of code that's run whenever a new rocket is created. The int player is the parameter passed to the constructor. This tells the constructor whether the player is number 1 or number 2.

We then have one block of code that's executed if the player==1, and another if player==2. The comment above the constructor tells us that this assigns the keys to the player, so this is what we need to do!
Put the Rocket class to one side and open up the RocketMaster class. Make sure documentation view is selected, and you should see a table titled “Method Summary.” It's this table we're interested in. For setting the keys, we're interested in the last four methods – setDownKey, setLeftKey, setRightKey and setUpKey. These are the methods that we need to use to set the keys.
You can set the keys to anything you want. If you want to set a key to a letter, a for example, you'd type setUpKey(“a”); . If you wanted to use an arrow key, you simply use the direction of the key instead, so it'd be setUpKey(“up”); . I'd recommend using w, s, a and d for player 1's keys, and the arrow keys for player 2. After assigning player 2 to the arrow keys, your code should look something like the following:

Player 1 will still need their keys assigned the same way of course.

Once you've assigned the keys, test your code compiles OK by hitting the compile button. If you get an error, make sure the code is entered in exactly the same way as in the example above. If you hit the run button however, you should notice that nothing else actually happens. While you've assigned the keys, you're still not actually telling the rocket to do anything when those keys are pressed, and that's what we need to tackle next.

Stage 3 – Getting the rocket to move, fire and rotate

First, let's concentrate on getting the rocket to fire if the up key has been pressed. Have a look at the ignite method in the MasterRocket class. We need to call this method whether the up key has been pressed or not, the difference comes with the parameter that's passed to the ignite method. Read the comments on the method to see what value you need to pass if the up key is pressed, and what value if it isn't. You should then be able to call the ignite method properly in both cases, and when you test your code you should be able to ignite the booster to cancel gravity! You still can't move anywhere other than up or down... but it's a start.

If you've managed to do the above correctly, getting the rocket to fire shouldn't be any problem at all! You know where to put the code because of the commented line. Have a look through the MasterRocket class in documentation view again and have a look at a method that looks appropriate. It should be pretty obvious! Once you've found this, it's just a case of typing it in.

Compile and test your code again, and you should be able to shoot using the down key. If this isn't what happens, go back and check your code thoroughly. If it is, then well done, just rotation to go!

This is the most challenging part of getting the rocket to work as it should. All you're given is an integer which gives you the rocket's current angle of rotation. All of the if statements and actually getting the rocket to rotate is down to you! The speed at which the rocket rotates is given at the top of the class.

When you get to a problem like this that may seem difficult at first, it's best to break it down into stages. This is the same with any programming – breaking one problem down into several smaller problems makes things much easier. In this situation, we've got 2 main parts, the key handling with the if statements, and then the actual code to get the rocket to rotate.

Let's start with the easier problem, the if statements. If you look at one of the other if blocks, such as the one for fire, the code you need for the block is very similar to this. You need one block for the right key being pressed, and one for the left key.

Now that you've done this, check that you're code is still OK by compiling. You still won't be able to rotate the rocket, but if you've done everything correctly then the code should still compile.

Now let's look at actually getting the rocket to rotate. You need another method for this that's not in the MasterRocket class, this method being setRotation. (If you want to have a look at it, it's in the Actor class.) This method takes one parameter, and this is the angle of rotation that you want to set the rocket at. Since we want to set it relative to the angle it's at already, we need to get the current rotation of the rocket. However, that's the bit of code that's already there, saving us one less thing to do!

Hint: To rotate the rocket right, you need to set the rotation as the current rotation + the rotation speed, stored in the ROTATESPEED variable. Going left is the opposite.

Stage 4 – Testing and adding more levels

Now you've added all the functionality to make the game work, grab the nearest willing person next to you and have a play around. Make sure things are working as they should be, and try and fix any problems you notice. There should be 3 levels built in for you, and these should cycle in a loop while you build up a score.

If everything's working then why not try adding some more levels? Doing this is simple, the most time consuming part is working out where you want the blocks to go. If you want to try this, simply open up the GravitonWorld class in editor mode and scroll down to the bit where you see:

This shows you what a typical level (level 3) looks like. To add another level, simply put case 4: after the case 3: block and then put your code there. The only thing to bear in mind is that you must put a break statement at the end of the case.

Stage 5 – Adding new bits

While you've got the game to a usable state, this doesn't mean you have to push it aside and leave it. See what else you can add! Here's some ideas of possible things you could try to do:

Make the game stop after one player or the other reaches a certain score (first to 10 for example)
Create a different kind of block, one that is indestructible (it doesn't disappear when a bullet hits it)
Edit the goal class so the goal moves back and forth to make it harder to hit. (Hint: Add an act method to the Goal class to do this. Methods you may find useful are getX(), getY() and setLocation(). )
Instead of dying when the rocket hits the edge of the screen, try making the rocket emerge from the opposite size. (You will need to use some of the methods in the World class for doing this.)
Add a stronger type of bullet that doesn't stop until the edge of the screen and destroys everything in its path. Give each rocket a set number of them, say 3
Add items that increase or decrease a player's score when they collect them.

Of course, while those are suggestions they're not exhaustive by any means, play around and see what you come up with! Just remember to follow the directions as above, frequently saving and running your work to check it does what you think!


To see articles on the same topic, click the links below the name of the author at the top of this page.
Authored by learn.Greenfoot on Dec 17, 2008.

I found this to be a lot to do. I asked the author if he can make a scenario that only teaches how to have 2 players, one controlled with arrow keys, and 1 controlled with the keys asdw.

Authored by guest on Dec 19, 2008.

A simpler version is now available on the greenroom (www.greenfoot.org/greenroom). Hope this helps!

--mjrb4