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

Part 2 - Add Objects and Collision Detection

This document provides a tutorial on creating a car racing game using Greenfoot, focusing on adding objects and collision detection. It covers key concepts such as adding a start and finish line, bridges, and oil spills, along with modifying instance properties to enhance gameplay. The tutorial is aimed at intermediate users and is designed to be completed in about 30 minutes.

Uploaded by

Tegar Wijaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Part 2 - Add Objects and Collision Detection

This document provides a tutorial on creating a car racing game using Greenfoot, focusing on adding objects and collision detection. It covers key concepts such as adding a start and finish line, bridges, and oil spills, along with modifying instance properties to enhance gameplay. The tutorial is aimed at intermediate users and is designed to be completed in about 30 minutes.

Uploaded by

Tegar Wijaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

academy.oracle.

com

Create a Racing Game with Greenfoot


Part 2 – Add Objects and Collision Detection
Intermediate

Copyright © 2019, 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

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 © 2019, 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

Create a Car Racing Game with Greenfoot


Part 2 - Add Objects and Collision Detection
Level – Intermediate

Topic Details

Overview In this tutorial, you will add more objects, and detect collision in
the Arcade Game in Greenfoot.

Key Concepts • Add objects in order


• Collisions with Objects
• Modify Instance Properties

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 © 2019, 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

Adding a start and finish line to the game.


Add a bridge to the track.
Adding Oil spillage to the track

1 Before we add our start and finish line let us improve our off track detection and handling. You may have noticed you can
reverse off the track and drive around. Let us detect and handle this better.

2 Modify the checkOnTrack() method so that if we are moving forward when leaving the track we reverse, otherwise we move
forward

3 We will now add a start and finish line.

3a Create a new subclass of actor by right+clicking actor and selecting New subclass…

3b Call this StartLine and select the image “CheckedStart”

3c Right click on StartLine and select new StartLine

3d Place this in front of the car

Copyright © 2019, 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

3e Run your game, and you should see a problem. The car goes under the StartLine! In Greenfoot, the order of image creation
determines which actor appears in front. So the second image created appears on top of the first and so on.

3f Right click on the StartLine and select inspect.

3g In this screen take a note of the x and y value to where you have placed the StartLine.

X value

Y value

3h Edit the MyWorld Class to add an instance of the StartLine in code.

3i

Copyright © 2019, 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

Note: We have added the StartLine first so that it appears under the car.

4 Adding objects in different orders give us the ability to add objects like tunnels, and bridges where the car would go under the
graphic.

We are now going to add a bridge. This bridge is from (chabull(CC BY 3.0)).- https://opengameart.org/content/bridges

4a Right+click on Actor, and then select New Subclass.

4b Give this the name “Bridge”, and select the image called “Bridge”.

4c Add a line to your MyWolrd constructor to add the bridge to your track after the car is created.

4d

4e Test that your car drives under the bridge.

5 We are now going to add an oil spill to the track. This will effect the performance of the car if driven over.

5a Right+click on Actor and select New Subclass

5b Call this OilSpill and select the image with the same name.

5c You can place this anywhere. We have placed it before the first corner. Do the same and take a note of its x and y position.

5d Add a line in the MyWorld constuctor that adds the oil in your selected position and place it before the car is created.

You can run your program and drive over the oil spill.

6 There should be a consequence of driving over oil. We could remove the breaks, accelerate the car, or turn the car randomly
or anything we want.

We are going to add a property called inoil to the Car class. This will be true if the car is in oil, and will be used to stop the
breaks working while moving the vehicle randomly!

6a Edit the Car code. Add the property inoil.

Copyright © 2019, 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

6b Create a method called checkInOil under act().

6c Modify the checkMovement method to test that the car is not in oil while trying to break

6d Add a call to checkInOil() to the act() method

Copyright © 2019, 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

Run your program and drive over the oil. You will see it is now difficult and unpredictable to control the car.

That is the end of Part 2.

Copyright © 2019, 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