Session 6 – Collision Detection and Keeping Score Authored by Brian Cullen ([email protected]/@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. Commenting our Methods In the last session we looked at the API for classes written by other people but we need to provide this type of information for our classes as well. To do this we must write comments into our code to explain the purpose of each method. By default Greenfoot provides the following comment for the act method of each class. /** * Act - do whatever the Actor wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() Change this comment to say what your act method does in each of the classes you have made. You can then view what the documentation looks like by choosing documentation from the drop down list in the top right hand corner of the screen. Continuing with the Air Raid Game Collision Detection We have already done simple collision detection so checking if the rocket is hitting a crate or plane should be something you are familiar with. However because we now have more than one condition that can cause a rocket to be removed from the game we have to make sure we don’t try and remove it twice as this will cause our game to crash! In general the code to check if the rocket has hit another object and remove them both from the game will look like this (for this example I have used the Crate class). To make sure we don’t remove the rocket twice the rest of the code will be put in an else block so it is only executed if you haven’t hit a crate. Actor actor = getOneIntersectingObject (Crate.class); if (actor != null) { World airWorld = getWorld(); airWorld.removeObject (actor); // remove the crate airWorld.removeObject (this); // remove the rocket // play sounds or anything else } else { // Code to check if you have hit the plane or the top of // the screen goes here. } If you don’t get this right it is likely that you game will crash and you will get a nasty looking error message. While you should see if you can construct this code on your own the final version of the act method in my version of the Rocket class is shown below. You will notice that I’m playing a sound called explosion.mp3 every time I blow something up so you can add this too if you want. Deal with hitting a crate. Else deal with hitting a plane. Else deal with hitting the top of the screen. Automatic Setup So far we have been putting the plane and launcher into the world manually which is a bit of a pain. To make things easier we are going to edit the constructor of the AirRaidWorld class so that it puts both of these things on automatically (if you haven’t done it already). Double click on the AirRaidWorld class and add the following two lines of code to the constructor. addObject (new Launcher(), 300, 375); addObject (new Plane (), 100, 30); Keeping Score One thing we haven’t looked at yet is how to keep score – something that you need to do in almost every game! Probably the best place to store this information is in the AirRaidWorld class as the score is a piece of information that applies to the entire game. To do scores properly we will need to provide a variable to store the score in, a way to increase the score and a way to check what the current score is. So lets get started… 1. In the AirRaidWorld class add a new private property called score. It is private because we want other classes to have to ask us to change the score rather than then changing it themselves. public class AirRaidWorld extends World { private int score = 0; … rest of your class 2. We are now going to write a method that can be used to increase the score. Note that this method must be public because it is to be used by other classes. public void increaseScore () { score = score + 1; // could just write score++; which does the same thing. } 3. The final thing we need to do is provide other classes with a way of seeing what the current score is. To do this we are going to add another public method. Notice that instead of saying void like last time we say int to show that the method is going to return an integer. We use the return statement to say we want to give this value to the class using the method. public int getScore () { return score; } After you have finished making these changes the final code for your AirRaidWorldClass should look like this. Now that we have a way of keeping score we need to increase it when we hit a crate or plane. To do this we must go back into the Rocket class and change our collision detection code a little. At the moment our collision detection code looks like the example below. Actor act = getOneIntersectingObject (Crate.class); if (actor != null) { World airWorld = getWorld(); airWorld.removeObject (actor); // remove the crate airWorld.removeObject (this); // remove the rocket // play sounds or anything else } What we need to do is call the increaseScore method that we have just made when we are removing the objects from the game. Unfortunately it is not quite as simple as writing the following line of code – although it is very close. airWorld.increaseScore(); The problem is that we have said that airWorld is made from the World class and the increaseScore method is only in the AirRaidWorld class. So what we need to do is tell the computer that the world we are in is actually made from the AirRaidWorld class. We do that by putting (AirRaidWorld) in front of the getWorld method. An example of the complete code is shown in the box below. You will need to change this is two places in the Rocket class – once for when you are checking for crates and once for when you are checking for planes. Actor act = getOneIntersectingObject (Crate.class); if (actor != null) { AirRaidWorld airWorld = (AirRaidWorld)getWorld(); airWorld.removeObject (actor); // remove the crate airWorld.removeObject (this); // remove the rocket airWorld.increaseScore(); // play sounds or anything else } As we haven’t hooked up our score board yet the easiest way to check the score is working is to play the game for a while and then pause it. You can then right click on the background and choose getScore, as shown in the screenshot below, to see what your current score is. Try it! Further Documenting Your Code Having written comments explaining what each of your act methods do you should now add comments for each of your other methods. Once finished swap with a partner and see if their comments make sense – do they give enough or too much detail? Are the comments helpful?
© Copyright 2026 Paperzz