Session 8 Finishing Touches to the Air Raid Game Authored by Brian Cullen (bcullen@rossettschool.co.uk/@mrbcullen) (c) Copyright 2011 Computing At School. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-nc-sa/3.0/ for details.
Growth in Computing Power If you think about how fast computers have evolved over the last few decades it is truly amazing. To demonstrate how much computers have changed I have a challenge for you. Most people have a smart phone of some sort today so I want you to find out the processor speed and memory available either in your phone or a phone you would like to have! Done that? Well compare that to the Apollo 11 (the spacecraft that landed on the moon) computers which ran at 2.048 MHz (not GHz) and about 2KB (not MB or GB) of memory. There is a table below to help you compare these values. All I can say is wow it does make me wonder if you could do all that with such simple machines why do people always complain about their computers running too slow? Do you have any ideas? Unit Conversion 1 KiloByte 1024 Bytes 1 MegaByte 1024 KiloBytes (1,048,576 Bytes) 1 GigaByte 1045 MegaBytes (1,073,741,824 Bytes) Replacing the Plane and Making it Harder One of the biggest issues with the game at the moment is that if you shoot the plane it isn t replaced and so there is nothing left for you to do. At the same time the game never really gets harder and it would seem to be a good idea if we add more and more planes the higher the score gets. As both these problems relate to adding planes to the world we are going to try and deal with both of them at the same time. To start with we are going to add two more methods to our AirRaidWorld class. The first method will be called addplane and will be used to add a new plane to the world. The second will be called getnumberofplanes and will tell you how many planes are currently in the world. You should already know how to write the method to add a new plane to the world as you have already done it in the constructor. So write the method and copy the code into the new method. It should look like the code below once you are finished. Note that once you have written this method you could replace the line where you add the plane in the constructor with addplane; public void addplane () addobject(new Plane(), 100, 30); The second method is a little more tricky but we know that we want to return the number of planes in the game so the method definition is going to look something like this.
public int getnumberofplanes () // Code to count the number of planes goes here. return 0; We can get a list of all the planes in the game by calling the getobjects method and passing it the Plane class as a parameter. Luckily for us the List returned by this has a size method which will tell us how many planes are in the list. This means that the final code will be as shown. public int getnumberofplanes () List planelist = getobjects(plane.class); return planelist.size(); There is one final complication that means that this code won t compile at the moment. The problem is that unless you tell it Java won t know where to find the List class we are using in this code. To sort this out we need to go to the top of the file and import the List class. At the very top of the file you will see a line importing the Greenfoot classes and right underneath it simply write the following to import the list class. import java.util.list; Now that we have both a way of checking how many planes are left and creating a new one we need to go back to our Rocket class and find the code where we remove a plane that is hit by the a rocket. After the code that removes the plane we need to check if there are any planes that are left (using an if statement) and if there isn t add a new one. Once you have added these new lines of code test your program and make sure it works. if (airworld.getnumberofplanes() == 0) airworld.addpane(); The final thing to do is consider when we need to add more planes to the game to make it harder. For example we could add a new plane to the game every time a player scores 5 points but where would we put that code? You may have guessed that the increasescore method might be a good place to do this and that is what we are going to do. We are going to check if the score is evenly divisible by 5 and if it is then we will add a new plane. The easiest way to find if a number is evenly divisible by 5 is to use the modulo operator (written as %). This will give you the remainder of a division so, for example, 11 % 5 would give you 1 as an answer because when you divide 11 by 5 you get a remainder of 1. The code you need to add to the increasescore method is shown below. Make sure you add it AFTER you increase the score otherwise it won t work properly! Once you have added the code try the
if (score % 5 == 0) addplane(); Game Over Screen The easiest way to make a game over screen is to open up a graphics program (paint/fireworks/etc) and draw one! I would suggest that you make your image 300 by 200 pixels to ensure it fits properly on the screen. Once you have made you image create a new subclass of Actor called GameOver and choose your picture for it to use. Add the following code to the lostlife method just before it stops the program to display your game over actor in the middle of the screen. addobject (new GameOver(), 300, 200); Other Refinements At this stage it is over to you! You need to decide what if any improvements that can be made. One thing that you might want to consider is changing the graphics used in the game. You can change the picture used by any of the objects simply by right clicking on the class and choosing the Set Image option. Get someone else in the group to play your game and give you feedback on what they think. It s your game so customise it! Publishing Your Game Want to get others feedback on the game? Then why not publish it to the Greenfoot site. You can do this by clicking the Share button on the right hand side of the window. Publishing a game like this can be a great way to get feedback and you can always use the Greenfoot forums to ask for more help if you need it!