Using Greenfoot to create your own driving game

Using Greenfoot to create your own driving game Worksheet 2 – Adding on­coming traffic Game so far You have a red convertible on the road that is able to move left and right. You may even have managed to make it move up and down to give the illusion of increasing and decreasing speed. We will now add on‐coming traffic to your game, to give you something to avoid. Before continuing with this worksheet, ensure you have Greenfoot open and have programmed the car class to respond to left and right key presses Step 1 – Creating the Vehicle class New skill – Creating a new sub class The vehicle class will store the details of all on‐coming traffic. We first have to create this class and decide on the image to use. •
Right click on the Actor class and choose the option to create a new subclass Name the new class “Vehicle” and choose a suitable image. The screenshot to the left shows the blue car has been chosen for this. Step 2 – Making the vehicles move down the road Skill – Practicing setLocation(), getX() and getY() methods In order to create the illusion of moving along the road, we need to program our on‐coming traffic to move down the screen. We have already used the setLocation(), getX() and getY() methods in order Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 2 [email protected] Using Greenfoot to create your own driving game to make our car move when we press one of the arrow keys. We will use the same methods to make the on‐coming traffic move too. We first need to look at the code for the vehicle class, so: •
•
Right click, and choose Open editor. We need to create a new method called moveDown(). The code for this should make the vehicle move down the screen. Use the setLocation() method to adjust the Y coordinate of the vehicle to increase by 3 pixels each time the moveDown() method is called. This will be done in exactly the same way as your moveDown() method created for the car class. You now need to tell the vehicle to run the moveDown() method each time the act() method is called. • To do this, add the line moveDown(); to the add method Compile your code and then test it by adding some vehicles to the top of your road, as shown in the screenshot below. When the scenario is run, you should be able to move your car around the on‐coming traffic. Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 2 [email protected] Using Greenfoot to create your own driving game Step 3 – Removing the vehicles when they hit the bottom of the screen New Skill – Learning about the If Function, Removing Objects, Accessing methods from the objects world If you look at the bottom of your game screen after testing out your last bit of code, you will notice that the vehicles do not disappear when they reach the end of the road. In time you will have a build up of vehicles and your game will not look very refined. We can easily solve this problem by checking when each instance of vehicle has reached the bottom of the screen and then destroying the instance if it has. We can solve the problem by adding to the act method of the vehicle class. • Open the editor to see the code for the vehicle class We need to check the y coordinate of the vehicle, and compare it to the y coordinate of the bottom of the game screen. If the vehicle’s Y coordinate is more than or equal to the bottom of the game screen, we must destroy the vehicle instance. We use the if statement to check this. This statement allows us to only run a command if a particular condition has been met. In this case if the vehicle has reached the bottom of the screen. It should be used like this: if (test condition) { Statement(s) to run if condition is met } To find out the Y coordinate of the bottom of the game screen, we can use the getHeight() method of the World class. To access World class methods, we use the getWorld() method on the vehicle. The test condition to be entered in the act() method, after the instruction to moveDown() is: • getY()>= getWorld().getHeight()‐1 Gets the Y coordinate of the vehicle Access the methods of the world that the vehicle is in Gets the height of the world, then subtracts 1 to find the lowest pixel the car can be at Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 2 [email protected] Using Greenfoot to create your own driving game If the condition is met, we need to use the removeObject() method of the vehicle’s world. The removeObject() method needs one argument (extra piece of information) to be entered into the brackets when the method is called. It needs to know what object to remove. We can use the key work this to mean we want to remove the object that is calling the method. The statement to run if the condition is met is: • getWorld().removeObject(this); Step 4 – Making the on­coming traffic appear automatically New Skill ­ Generating Random Numbers, Adding objects at run­time To add these vehicles to our world automatically, we need to randomize things. It should add a new car at random time intervals, and also at random positions along the road at the top of the screen. We can use a method available through the Greenfoot environment called getRandomNumber() to help us here. This method works by giving us a random number between 0 and what ever number is entered in brackets when the method is called. So, if we used the command getRandomNumber(100), we would get a random number between 0 and 99 – that’s 100 different numbers. If we wanted a 10% chance of a car being add to the world, we could check to see if the random number between 0 and 99 is less than 10 – that would happen 10% of the time. To check whether the random number we are after has been generated, we can use the if function again. This will let us only add a new vehicle if our 3% chance condition has been met. When we add our vehicle, we want to place it on our road. The road takes up about a 3rd of the screen, which is 600 pixels across, so we want our vehicles to be placed at an X coordinate between 200 and 400, and a Y coordinate of 0 (the top of the screen). When we instruct the CarWorld class to create a new instance of a class, we have to specify where we want the instance placed. We can use the getRandomNumber() method to get us a coordinate between 200 and 400 by adding 200 to a random number between 0 and 200. • Open the editor for the CarWorld class • In the act() method, add the following to add a new vehicle 3% of the time This condition will be met 3% of the time
if (Greenfoot.getRandomNumber(100) < 3) { addObject(new Vehicle(), Greenfoot.getRandomNumber(200)+200, 0); } Method to add a new We need to add a new We need the x coordinate to be between 200 and 400. object to the world vehicle object The Y coordinate should be 0 Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 2 [email protected] Using Greenfoot to create your own driving game • Now try your game. You should find that new cars keep appearing at the top of your road, and the method you programmed to make them move down the screen make it look as though the red car is driving along the road towards the on‐coming traffic. The vehicles should disappear when they reach the bottom of the screen. Using Greenfoot to create your own driving game By Helen Jenson Rainham Mark Grammar School Worksheet 2 [email protected]