0% found this document useful (0 votes)
2 views

Part 4 - Add More Flexibility to the Game

This document provides a tutorial on enhancing a car racing game using Greenfoot, focusing on adding flexibility through the use of the Date class and modifying the MyWorld class. Key concepts include improving car control, implementing checkpoints for lap completion, and tracking lap times. The tutorial is designed for intermediate users and takes approximately 30 minutes to complete.

Uploaded by

Tegar Wijaya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Part 4 - Add More Flexibility to the Game

This document provides a tutorial on enhancing a car racing game using Greenfoot, focusing on adding flexibility through the use of the Date class and modifying the MyWorld class. Key concepts include improving car control, implementing checkpoints for lap completion, and tracking lap times. The tutorial is designed for intermediate users and takes approximately 30 minutes to complete.

Uploaded by

Tegar Wijaya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Create a Racing Game with Greenfoot

Part 4 – Add More Flexibility to the Game


Intermediate

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

Where can I find Java?


From laptops to datacenters, game consoles to scientific supercomputers,
cell phones to the Internet, Java is everywhere!

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.

Greenfoot is visual and interactive. Visualization and interaction tools are


built into the environment.

The actors are programmed in standard textual Java code, providing a


combination of programming experience in a traditional text-based
language with visual execution.

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

Part 4 – Add More Flexibility to the Game

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.

Key Concepts • Add more flexibility to a class


• Use the Date class
• Add a method to the MyWorld class

Difficulty Intermediate

Duration About 30 minutes

Notes This tutorial was built using Greenfoot 3.5.2

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.

We will make use of this flexibility later.


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.

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.

5 Add 2 new properties to Car


checkpoint1triggered and checkpoint2triggered
If both of these are not true, then we know someone never completed a full lap.

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

9 Edit the MyWorld class to add these 2 instances

10 Open up the editor and modify the Checkpoint class.


Add a property called checkpointnumber
Add a constructor that sets this property.

11 Modify the construtor in MyWorld to change the calls to Checkpoint to select a number for each.

To help identify the cars we are going to give them a number.

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

13 Modify the constructor of Car to set player number.

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.

17 Edit the Car class to import the date class.

18 Add a method in Car calld checkPoints.


This will do a number of things:
It will check if we have gone through both checkpoints.
It will also get the time gone though the checkpoints and get the laptimes.
The basic idea is that when we go over a check point, we test if none are triggered. If this is the case, then it must be the
start of a lap.
If both are triggered, then we must be at the end of a lap.
If it is the end of a lap, then add one to the lapnumber.
Reset the check points.
Calculate the time taken for the lap.
Display the laptime.

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.

Test this and check that it works.

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.

You might also like