Part 4 - Add More Flexibility to the Game
Part 4 - Add More Flexibility to the Game
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
What is Java?
Java is a programming language and computing platform developed by
Sun Microsystems in 1995. Java runs on more than 850 million personal
computers worldwide, and on billions of devices worldwide, including
mobile and TV devices.
What can I do with Java?
Java allows you to:
• Play online games
• Chat with people around the world
• Use interactive maps
• View images in 3D
What is Greenfoot?
Greenfoot teaches object orientation with Java. With Greenfoot you create
'actors' which live in 'worlds' to build games, simulations, and other
graphical programs.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
2
Create a Car Racing Game with Greenfoot
Level - Intermediate
Topic Details
Overview In this tutorial, you will add more flexibility to the game by using
Dates, and adding a method to the MyWorld class.
Difficulty Intermediate
Project Specification
Create a top down car racer where you will control a car around the confines of a track, with the aim of gaining the fastest lap time.
There will also be the option of a two-player game, where you will race against an opponent as well as the clock. Additional obstacles
will be on the track such as oil spillage and dirt. Powerups will be randomly added to the track where if a car drives over them, then the
car will be improved by either increased top speed, increased acceleration, better breaking or better turning.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
3
We are going to bring in more control over the cars. This will give us the ability to change the way they behave
e.g. Faster acceleration, faster top speed, better cornering or better breaks.
We will set up 4 constants that will set the limits of our cars.
1 Open the code editor for Car and add the following constants.
2 Add 3 more properties to Car to give greater control. These are acceleration, breaking and turnspeed.
3 Modify checkMovement(). This will add checks so that we can not go faster than the max speed allowed, and will turn at the
current turning speed.
4
We will now add a check to test if a lap is completed.
We could just check for a collision on the StartLine to start a lap, and another collision when they cross it again. But what if
someone crosses the line, then reversers over it again to register the fastest lap ever? Let us add some code to keep the
player honest…
4 Our approach here will be to add some transparent actors that are basically lines that will be triggered as a car passes over
them. To complete a lap a car will have to trigger all of these.
6 Create a new subclass of Actor and call it CheckPoint. Select the image CheckpointBlack, at the moment.
7 Place one of these checkpoints at the Start Line and another just over half way on the map
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
5
8 Right click on each and inspect. Take a note of both their x and y coordinates
11 Modify the construtor in MyWorld to change the calls to Checkpoint to select a number for each.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
6
12 Add a property to Car called playerno
14 Edit the MyWorld class to include player 1 and 2 in the create Car 1, Car 2.
15 Add a method to MyWorld that will display laptimes. We will make this look a lot better later, but in the meantime we will
show only player1
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
7
16 We shall create a few more properties in Car to hold the lapnumber, the laptime, the best laptime and the start time of a lap.
We are going to time the laps and to help us do this we are going to make use of the Date class. We will have to import this.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
8
19 Add a call to this method in act() of car.
Once you are happy that it is working, then we can change the image of the CheckPoint so that it can not be seen.
20 Right+click on CheckPoint and click set image. Select the image called checkpoint. This is a transparent image and will not
be seen by the end user.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.