3

Click here to load reader

Using Greenfoot to create your own driving game · Using Greenfoot to create your own driving game • Now program the line class to ensure the lines are removed when they hit the

Embed Size (px)

Citation preview

Page 1: Using Greenfoot to create your own driving game · Using Greenfoot to create your own driving game • Now program the line class to ensure the lines are removed when they hit the

Using Greenfoot to create your own driving game 

Using Greenfoot to create your own driving game    Worksheet 3 By Helen Jenson  Rainham Mark Grammar School  [email protected] 

Worksheet 3 – Creating the white lines on the road 

Game so far You should by now have added code to make your car move when the appropriate cursor key is pressed.  You should also have created a sub class of Actor, called Vehicle that is programmed to make the vehicles get added at random times along the road, and then move down the game screen to give the illusion of a moving road. 

We will now create the white lines on the road, to add to the effect of making it look like the red car is moving along the road. 

Step 1 – Create the white lines 

Skill – Practicing previous skills – adding a new sub class and programming it to move down the screen You have already created new classes and programmed them to move down the screen (The vehicle sub class does this).   

• Create a sub class of Actor called Line • Create a moveDown() method that moves 2 pixels down every time 

• Make it run the moveDown() method when the scenario is run 

Step 2 – Making the lines appear at the right position when the game starts 

New skill – Adding new instances at run­time We need to add the lines we created to the road when the game is first run.  We can do this using the CarWorld() constructor of the CarWorld class.  If you look at the code already entered here, you should be able to see the code that added the red car to the world when the game starts.  The line looks like this:         

addObject(new Car(),300,550); 

This means a new instance of the class Car will be added at coordinates (300,550) which is in the bottom middle of the game screen. 

To add the lines, we write: 

• addObject(new Line(),300,0); addObject(new Line(),300,120); and then at 120 pixel gaps throughout the world 

We also need to program the lines to disappear when they reach the bottom of the screen.  You did this for the Vehicle class already 

Page 2: Using Greenfoot to create your own driving game · Using Greenfoot to create your own driving game • Now program the line class to ensure the lines are removed when they hit the

Using Greenfoot to create your own driving game 

• Now program the line class to ensure the lines are removed when they hit the bottom of the screen 

Your scenario should now look like the screenshot below, but when the game is run, no new lines will appear to take the place of the ones that have disappeared.  Adding this feature is our next step 

 

Step 3 – Making new lines appear at regular intervals 

New skill – Setting up a counter, setPaintOrder We set our lines to appear every 120 pixels.  Every time the act() method is run the lines move down 2 pixels.  This means that every 60 times when the act() method is run, we need to add a new white line. 

To set up a counter we need to declare a new variable.  This is just like creating a box that will hold the current value of our counter.  It would make sense to give the variable a meaningful name, so we will call it counter.  We will also set the data type to be int (short for integer) which means a whole number (without a decimal point) 

We will add 1 to the counter every time the act() method is run and when the counter reaches 60, we know it is time to add a new line object to the game.  We also reset the counter at this point so it can start all over again. 

To declare the counter variable, we need to edit the code for the CarWorld class. 

• Type private int counter; above the line public CarWorld() 

• In the act() method, add a line to add 1 to the counter.  The code for this is counter++; 

Using Greenfoot to create your own driving game    Worksheet 3 By Helen Jenson  Rainham Mark Grammar School  [email protected] 

Page 3: Using Greenfoot to create your own driving game · Using Greenfoot to create your own driving game • Now program the line class to ensure the lines are removed when they hit the

Using Greenfoot to create your own driving game 

Using Greenfoot to create your own driving game    Worksheet 3 By Helen Jenson  Rainham Mark Grammar School  [email protected] 

• Using an if statement, write a command to add a new instance of the line class when the counter reaches 60 (counter == 60) (You did something similar when you added the on‐coming traffic).  Don’t forget to write counter=0; after a new line has been added, or the counter wont ever reach 60 again. 

We now have another problem.  When the new lines are added to the world, they appear on top of the on‐coming traffic.  We can easily fix this by setting the order for drawing our objects.  This is in the CarWorld class.  Look for the line that reads 

 setPaintOrder (Car.class); 

This tells Greenfoot to always draw objects belonging to the Car Class on top of anything else.  We also want to draw the other vehicles before the lines. 

• Change this line to read setPaintOrder (Car.class, Vehicle.class); 

All that we have left to do now is to detect the collisions between the red car and the on‐coming traffic.  Worksheet 4 will tell you how to do that, but perhaps you can work it out for yourself now you have a basic familiarity with the language.