Near enough everyone has heard of Space Invaders, since its first arrival 30 years ago it's been remade and rewritten an almost uncountable number of times. And now it's come to Greenfoot. Well – almost. It's been half written, but it's up to you to finish it off and get it all working.
Due to the problems we have been experiencing in Bell 340 with the keyboard and Greenfoot, we are asking that all students follow these directions for starting Greenfoot until further notice. The same problems occur if you are working in Bell 101 and you should use these instructions there as well.
If you are working in the lab, you will need to copy the scenario directory to your home directory. The scenario is located in: /eng/class/notes/cse113/space-invaders. If you do not remember how to do this, refer back to Lab 1.
If you are working from home, you can get the space-invaders scenario from here.
Once you have the scenario loaded into Greenfoot, compile all on the scenario to see the skeletal version of the game.
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. 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. Your worksheet is this website.
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. After doing this, you should see that apart from the aliens shooting you every so often, not a lot happens! You can't shoot aliens by any means, the aliens don't move, you can't move, the score stays at 0 and the game never ends. We'll have to do something about that, so let's get started!
To start with, let's get the ship doing what we want. Open up the source code to the ship.
So let's start off by looking at the first bit of code we need to modify:
if(Greenfoot.isKeyDown("left")) { //Insert code here }
This code is essentially saying, if the left key is held down, then execute the code in the curly braces. We clearly need to put some code in here, but what? Well to start with, in plain English, think of what you want the Ship to do. Think simple. When the left key is held down, the ship should – move left? Sounds about right. So what code do we use to get the ship to do this?
If you poked around before in the documentation view of MotherShip, you might have some idea. If not, open up MotherShip in the editor and select documentation view if you haven't already. Now scroll down until you see something like this:
See anything useful there? Try and look for something that matches the English description of what you came up with earlier... moveLeft() seems to fit nicely. So make the appropriate method call to move the ship left when the user holds down the left arrow. Go back to the scenario window and test to ensure that the code you just entered behaves as you would want it to.
However, as you have probably noticed, we're far from finished yet. So open up the Ship class again, then look at the other points where you need to write code. Work through them the same way as before, looking for a method that may be suitable in the MotherShip class, before implementing it in your code. All the conditional “if” statements have been coded already, you just need to write the code inside these blocks. Make sure you compile and test your code at each stage!
When you have finished your coding, scroll back up to the top of the class and enter your name and the date in the provided spaces. Congratulations, assuming everything works as planned you have implemented all the ship's functionality!
Now that the ship works as it should, let's move on to getting the aliens to do what they should. The first and most obvious thing is – they need to move. So, fire up the editor as before (but this time for the Alien class) and MasterAlien in documentation view and have a look at what methods you can use.
You may notice that, this time, there's no straightforward moveLeft()moveUp() methods and suchlike, instead there's just a single one called move(). However, if you click on the method to view it's more detailed description, you can see it takes two parameters. These are values that are passed into a method, and the method can do whatever it likes with them. In this case, the method moves the aliens x units horizontally and y units vertically. For example, move(2, 5); would move the block of aliens 2 units horizontally and 5 vertically.
Implement the movement of the aliens.
You're almost there! If you play the game now, it should look almost finished, apart from one thing – it still never seems to actually end!
In the Alien class, you may notice a final comment at the bottom saying to add checks to end the game. This time, the if statement isn't constructed for you, you have to implement it yourself! Have a look at the MasterAlien class again, all the methods you need will be in here.
Now comes the fun part, you get to test your code and make sure the game works as it should, which means – you play it! If you notice anything wrong, this is how you go back and fix it, by testing.
Some things to implement on your own:
See the following page for instructions for submission.
Remember that you need to first zip up your scenario directory before trying to upload.
Your lab submission is due no later than 11:59:59 pm on October 23rd. Remember, no late labs will be accepted.
Lab adapted for use in CSE 113 by Adrienne Decker
Page maintained by Adrienne Decker
Contact: adrienne@buffalo.edu | 130 Bell Hall | (716)645-3184