Using Greenfoot to create your own driving game

Using Greenfoot to create your own driving game Worksheet 4 – Checking for collisions Game so far You now have a playable game, whereby you control your car in order to dodge on‐coming traffic. There are also white lines going down the road to give the illusion of movement. Step 1 – Check for a collision Skill – Using the getOneIntersectingObject() method You will need to create a new method in the actor class to check for collisions, and then the act method should call this method each time it is called. This will ensure any collision is detected • Edit your car class • Create a checkCollision() method We will use the getOneIntersectingObject() method. This requires us to specify what class we are looking for, and if an intersection (or collision) is detected, it will return the object it intersects with. We need to set up a variable to store this information so: • To store the details of any intersecting objects, in your checkCollision() method, add the line: Actor collided; • To put the details of any colliding vehicles in the collided variable, add the line: collided = getOneIntersectingObject(Vehicle.class); Step 2 – Add an action to perform if the collision is detected Skill– Using the IF statement, Adding sound If a collision is detected, we should make our program play a sound, so it sounds as though a crash has occurred. We can use the Greenfoot method, playSound() for this purpose • We will first check to see if a collision has occurred by seeing whether any data has been stored in our collided variable. We can check to see if this variable is not empty by using this command: if (collided != null) { The operator != means is } not equal • We now need to add the command to tell our game to play a sound when this collision is detected, add this line to your IF statement: Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 4 [email protected] Using Greenfoot to create your own driving game Greenfoot.playSound("crash.wav"); Step 3 – Stopping the game when we crash New skill – Ending a simulation Our game is very nearly complete. All we need do now is stop the game when the collision is detected. We just need to add a line to our IF statement to be run after the crash sound has been played. •
Add the line: Greenfoot.stopSimulation(); •
To ensure this is executed every time the program is run, ensure you added the command checkCollision() to your act method Step 4 – Finishing Touches Skill – Practicing all skills learnt so far The game is now functional, but clearly there are extras that could be added to it. At the moment, when new cars are added to the world, they are often added on top of each other. •
Find a way to prevent the cars appearing on top of each other – use the getIntersectingObject() method to do this Also, you don’t get any chances – when you crash, the game ends. •
Find a way of adjusting the “damage” status of your car, so that crashing just makes the damage value increase. You then only end the game when the damage status becomes 100. To make the game more realistic, we could add houses and trees to the sides of the road. •
Make houses and trees appear at random times along the road side We could also add different types of traffic use the road, say bikes and different types of car. •
Make other types of vehicle use the road too, by randomising the images used to the vehicle class. You may be able to think of many other things to add – be adventurous and try adding other new features to your game! Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 4 [email protected]