Major Assignment: Pacman Game

Transcription

Major Assignment: Pacman Game
Major Assignment:
Pacman Game
300580
Programming Fundamentals
Week 10
Assignment
The major assignment involves producing a Pacman style game with Clara using the Greenfoot files that are given to you.
The essence of the game – is to make Clara eat all available leaves, while avoiding collisions with ghosts chasing her. Clara is to
be controlled by a player through keyboard. Apart from leaves, there are also mushrooms available in Clara’s world, but unlike
the practicals, Clara is unable to push them around, but instead she can eat those mushrooms and gain superpowers (for a short
period of time she will be able to eat ghosts). Ghosts always start at their home base (also known as Ghost Healer) that is always
marked by a think red dot. Somewhere close to a Ghost Healer there is always a ghost wall (represented by a square surrounded
by dotted red line). The ghost wall can be penetrated by ghosts, but not by Clara, making the ghost base protected from invasion.
When Clara eats a mushroom, ghosts change their colour and would start running away from Clara. This mode when Clara is
able to eat ghosts is called “scared” mode. In the “scared” mode ghosts run away from Clara rather than chasing her and if a
collision between a ghost and Clara occurs while in this mode – the ghost would change into an almost invisible creature that
moves from the collision location to the base and then resurrects once the base is reached. If a collision happens between Clara
and a ghost in any other mode – then Clara looses and the game is over.
In solving the major assignment you will not be entirely on your own, as the steps involved are broken up in this assignment
brief. During practical classes your tutors will explain you the basics in completing these steps. Before we start, there will be an
explanation of all commands available in both Clara and the Ghosts vocabularies.
Major Assignment: Pacman Game
1
Important Requirements
For your convenience, all important requirements specified in this brief are re-listed here:
-
Clara and the Ghosts must be able to move from one side of the game to the other with the “wrapAroundWorld()”
method
-
Clara must be capable of being controlled with at least the arrow keys on the keyboard
-
Clara must not move in the game until after an arrow key pressed
-
Clara must stop before hitting trees or the ghost wall
-
Clara must be able to eat leaves, and play the appropriate sound when doing so
-
When all the leaves are eaten, the game must progress to the next level
-
When each level starts the intro sound must be played once for each level and Clara and the Ghosts must not be able to
move until after the intro sound has completed playing
-
Ghosts must move slower than Clara when moving normally and when scared
-
Ghosts must stop before hitting trees
-
Ghosts must randomly decide on a direction to go in when they find themselves in an intersection
-
When Clara and a Ghost collide, and the Ghost is neither dead nor scared, she must die, and play the appropriate sound
-
When Clara is dead the player must not be able to continue controlling her
-
Clara must be able to eat mushrooms, and this must make the Ghosts scared
-
When the Ghosts are scared, they must run the appropriate animation
-
When Clara and a Ghost collide, and the Ghost is scared, the Ghost must die, play the appropriate sound and play the
appropriate animation
-
When Ghosts are dead they must attempt to return to the Ghost Healer
-
When Ghosts collide with the Ghost Healer they must no longer be dead
-
You must make, at a minimum, one extra level
-
All files must compile and the game must run.
Important note 1
While in the past you only had to work with MyClara.java, this time you will also have to edit Ghost.java for all the logic related
to the Ghosts as well as MyClara.java for all the Clara functionality.
Important note 2
Do not let the following list of commands scare you, as they are further explained in the assignment brief and your tutors will
help you to select those commands that you need for each task.
Major Assignment: Pacman Game
2
Important note 3
For this assignment you should no longer have a main loop (for or while) inside act(). Instead you should rely on Greenfoot
executing act() repetitively (similar to how you did in Practical 2). The stop(); command MUST NOT be used in this assignment.
Clara’s vocabulary
For this assignment, the following commands are available for Clara right from the start:
Command Name
Command Description
Command Input
Command Output
treeFront()
Checks if there is a tree in
front of Clara.
Nothing.
true if there is a tree in
front of Clara.
false if there is not a tree in
front of Clara.
ghostWallFront()
getDirection()
Checks if there is a ghost
wall in front of Clara.
Gets which direction Clara
is facing.
Nothing.
Nothing.
true if there is a ghost wall
in front of Clara.
false if there is not a ghost
wall in front of Clara.
A string which represents
the direction Clara is
facing.
“up” if Clara is facing up
toward the top of the
screen.
“down” if Clara is facing
toward the bottom of the
screen.
“right” if Clara is facing to
the right side of the screen.
setDirection(string)
Makes Clara face a
specific direction.
It will not change her
direction if there is a tree
in the way.
A String, which specifies
which direction you want
Clara to face.
“left” if Clara is facing to
the left side of the screen.
Nothing.
“up” - face toward the top
of the screen.
“down” - face toward the
bottom of the screen.
“right” - face toward the
right side of the screen.
“left” - face toward the left
side of the screen.
Major Assignment: Pacman Game
3
Clara’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
move(int)
Moves Clara at the
specified speed.
An integer, which specifies
how fast you want Clara
to move.
Nothing.
makeScared()
Clara lets the Ghosts know
that she is ready to eat,
and makes them scared.
Nothing.
Nothing.
Nothing.
true if the Ghosts are still
scared of her.
isScared()
animate()
animateDead()
onLeaf()
removeLeaf()
For more information, see
the “isScared() function in
the Ghost’s vocabulary.
Also see the section “Part
10 – Eating Mushrooms
and Making Ghosts
Eatable”
Clara checks whether the
Ghosts are still scared of
her.
Clara continues her
animation by one frame.
For more information, see
the section “Part 2 Animating Clara’s
Movement”.
Clara visually shows that
she is dead.
For more information, see
the section “Part 9 Ghosts Colliding With
Clara and Losing”
Checks if Clara is on top
of a leaf.
Clara removes a leaf that
she is standing on.
Nothing.
false if the Ghosts are no
longer scared of her.
Nothing.
Nothing.
Nothing.
Nothing.
true if Clara is on top of a
leaf.
Nothing.
false if Clara is not on top
of a leaf.
Nothing.
Will show an error if she
tries to remove a leaf that
isn’t there.
Major Assignment: Pacman Game
4
Clara’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
onMushroom()
Checks if Clara is on top of
a mushroom.
Nothing.
true if Clara is on top of a
mushroom.
removeMushroom()
allLeavesEaten()
isClaraDead()
playClaraDieSound()
isClaraDieSoundStillPlaying()
playLeafEatenSound()
playPacmanIntro()
Major Assignment: Pacman Game
Clara removes a mushroom
that she is standing on.
Checks if Clara has eaten
all of the leaves in the level.
Checks if Clara should be
dead.
For more information, see
the function
“makeClaraDead()” in the
Ghost’s vocabulary and the
section “Part 9 – Ghosts
Colliding with Clara and
Losing”
Plays Clara’s death sound.
For more information see
the section “Part 9 – Ghosts
Colliding with Clara and
Losing”
Checks if Clara’s death
sound is still playing.
For more information see
the section “Part 9 – Ghosts
Colliding with Clara and
Losing”
Plays the sound of Clara
eating a single leaf.
Plays the pacman intro
sound.
Nothing.
Nothing.
Nothing.
false if Clara is not on top of a
mushroom.
Nothing.
Will show an error if she tries
to remove a mushroom that
isn’t there.
true if there are no more leaves
for Clara to eat.
false if there are still leaves for
Clara to eat.
true if Clara should be dead.
false if Clara should not be
dead.
Nothing
Nothing
Nothing.
Nothing.
Nothing.
Nothing.
Nothing.
Nothing.
5
Clara’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
isPacmanIntroStillPlaying()
Checks if the pacman intro
sound is still playing.
Nothing.
Nothing.
Nothing.
Nothing.
Nothing.
An integer representing the
current level number.
wrapAroundWorld()
getCurrentLevelNumber()
advanceToLevel(char[][])
For more information see
the section “Part 4 – Eating
Leaves and Winning”
Enables Clara to
automatically move off one
side of the game, and end
up on the other.
Gets the number of the
current level.
Changes the current game
level to the one specified. If
it is sent null, then it will
automatically load the first
level.
A level as per
“Part 12 –
Making and
Adding Levels”,
or null.
E.g. If the current level is 1,
then it will return 1.
Nothing.
To see more information on
how to use it, see “Part 4 –
Eating Leaves and
Winning”
For more information on
making levels to send the
method, see “Part 12 –
Making and Adding
Levels”
Major Assignment: Pacman Game
6
Ghost’s vocabulary
For this assignment, you will also be required to code new characters - Ghosts. The Ghosts pose a threat to Clara as she tries to
eat all of her leaves and try to prevent her from accomplishing her mission.
The behaviour of each Ghost is controlled by the act() method inside Ghost.java. Each of the Ghosts in the game is essentially an
instance of the Ghost class (we will learn about classes soon enough in the lectures).
In order to assist you in programming Ghosts, just like for Clara, there are Ghost commands already made for you. Some of
these commands are exactly the same as Clara’s, some are slightly different, and some completely unique to the Ghosts. Be sure
to read the following list of commands carefully:
Command Name
Command Description
Command Input
Command Output
treeFront()
Checks if there is a tree in
front of the Ghost.
Nothing.
true if there is a tree in
front of the Ghost.
treeAbove()
treeBelow()
treeToLeft()
Checks if there is a tree
above the Ghost.
Note that this method is
relative to the screen, not
the Ghost. That is,
regardless of the direction
of the Ghost, a tree that is
above it, will always be
above it.
Checks if there is a tree
below the Ghost.
Note that this method is
relative to the screen, not
the Ghost. That is,
regardless of the direction
of the Ghost, a tree that is
below it, will always be
below it.
Checks if there is a tree to
the left of the Ghost.
Note that this method is
relative to the screen, not
the Ghost. That is,
regardless of the direction
of the Ghost, a tree that is
to the left of it, will always
be to the left it.
Major Assignment: Pacman Game
Nothing.
false if there is not a tree in
front of the Ghost.
true if there is a tree above
the Ghost.
false if there is not a tree
above the Ghost.
Nothing.
true if there is a tree below
the Ghost.
false if there is not a tree
below the Ghost.
Nothing.
true if there is a tree to the
left of the Ghost.
false if there is not a tree to
the left of the Ghost.
7
Ghost’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
treeToRight()
Checks if there is a tree to
the right of the Ghost.
Nothing.
true if there is a tree to the
right of the Ghost.
getDirection()
Note that this method is
relative to the screen, not
the Ghost. That is,
regardless of the direction
of the Ghost, a tree that is
to the right of it, will
always be to the right it.
Gets which direction the
Ghost is facing.
false if there is not a tree to
the right of the Ghost.
Nothing.
A string which represents
the direction the Ghost is
facing.
“up” if the Ghost is facing
up toward the top of the
screen.
“down” if the Ghost is
facing toward the bottom
of the screen.
“right” if the Ghost is
facing to the right side of
the screen.
setDirection(string)
Makes the Ghost face a
specific direction.
The Ghost will not change
to a direction that has a
tree in the way.
A string, which specifies
which direction you want
the Ghost to face.
“left” if the Ghost is facing
to the left side of the
screen.
Nothing.
“up” if you want it to face
toward the top of the
screen.
“down” if you want it to
face toward the bottom of
the screen.
“right” if you want it to
face toward the right side
of the screen.
“left” if you want it to face
toward the left side of the
screen.
Major Assignment: Pacman Game
8
Ghost’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
move(int)
Moves the Ghost at the
specified speed. The ghost
will move in the direction
it is currently facing until
it hits an obstacle or until
the direction is changed
The Ghost checks
whether it should be
scared of Clara.
An integer, which
specifies how fast you
want the Ghost to move.
Nothing.
Nothing.
true if the Ghosts should still
be afraid of Clara.
isScared()
animate()
animateDead()
animateScared()
getClara()
The Ghost continues its
animation by one frame.
For more information,
see the section “Part 6 Animating Ghost’s
Movement”.
The Ghosts continues its
death animation by one
frame.
For more information,
see the section “Part 10 –
Eating Mushrooms and
Making Ghosts Eatable”
The Ghosts continues its
scared animation by one
frame.
For more information,
see the section “Part 10 –
Eating Mushrooms and
Making Ghosts Eatable”
Gets Clara.
Nothing.
false if the Ghosts should no
longer be scared of Clara.
Nothing.
Nothing.
Nothing.
Nothing.
Nothing.
Nothing.
Clara.
Is to be used with the
“isAboveMe”,
“isBelowMe”,
“isToMyLeft” and
“isToMyRight”
commands, explained
below.
Major Assignment: Pacman Game
9
Ghost’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
getGhostHealer()
Gets the Ghost Healer.
Nothing.
Ghost Healer.
Clara or Ghost Healer.
true if specified Actor is
above the Ghost.
isAboveMe(Actor)
isBelowMe(Actor)
isToMyLeft(Actor)
isToMyRight(Actor)
intersects(Actor)
makeClaraDead()
playGhostEatenSound()
Major Assignment: Pacman Game
Is to be used with the
“isAboveMe”, “isBelowMe”,
“isToMyLeft” and
“isToMyRight” commands,
explained below.
Checks whether the specified
Actor is above the Ghost.
Checks whether the specified
Actor is below the Ghost.
Checks whether the specified
Actor is to the left of the
Ghost.
Checks whether the specified
Actor is to the right of the
Ghost.
Checks whether the specified
Actor is currently intersecting
the Ghost.
Makes Clara’s
“isClaraDead()” method
return true.
For more information, see
the section “Part 9 – Ghosts
Colliding with Clara and
Losing”
Plays the sound of a Ghost
being eaten.
Clara or Ghost Healer.
Clara or Ghost Healer.
Clara or Ghost Healer.
Clara or Ghost Healer.
false if the specified Actor
is not above the Ghost.
true if specified Actor is
below the Ghost.
false if the specified Actor
is not below the Ghost.
true if specified Actor is to
the left of the Ghost.
false if the specified Actor
is not to the left of the
Ghost.
true if specified Actor is to
the right of the Ghost.
false if the specified Actor
is not to the right of the
Ghost.
true if the specified Actor
is currently intersecting the
Ghost.
Nothing.
false if the specified Actor
is not currently
intersecting the Ghost.
Nothing.
Nothing.
Nothing.
10
Ghost’s vocabulary - continued
Command Name
Command Description
Command Input
Command Output
isPacmanIntroStillPlaying()
Checks if the pacman intro
sound is still playing.
Nothing.
Nothing.
Nothing.
Nothing.
wrapAroundWorld()
For more information see the
section “Part 5 – Moving
Ghosts”
Enables the Ghost to
automatically move off one
side of the game, and end up
on the other side.
Making the Game
Part 1 – Moving Clara and Keyboard Input
One of your first tasks - is to learn how to process keyboard input of the player by using Greenfoot.isKeyDown(String) method.
Try passing the movement constants defined inside MyClara.java as input for Greenfoot.isKeyDown(String) and see what
happens when you press the arrow keys. Try starting with using System.out.println() to print which of the keys was pressed to
make yourself familiar with keyboard input.
Next thing for you to do – is to make sure that Clara can be controlled with the arrow keys. You may add extra functionality,
such as controls for left handed players, but that is not compulsory. You will need to use Clara’s “setDirection(String)” method,
which can be sent one of the following commands; “up”, “down”, left” or “right” (a good idea is to use the same movement
constants defined inside MyClara rather than using those String literals inside your code). Passing the aforementioned String
values to setDirection(String) method will set Clara’s direction to the command you specify.
Note that the “setDirection(String)” method will not change Clara to a specified direction if there is a tree in that direction.
Once you’re able to process keyboard input and make Clara respond to it by turning in the right direction – now it’s time to make
Clara move. In order to make Clara move, you will need to use the “move(int)” method. Note that it can be passed any integer,
and that a bigger integer will make Clara move faster. The integer that it is passed will determine how fast Clara moves. It will be
up to you to determine a suitable speed for Clara.
Please note that it is not a good programming practice to arbitrarily pass numbers to methods, and that it is always a good idea to
instead, make a constant that will hold the value that you wish to pass.
Then you will need to somehow stop Clara from automatically moving at the start of the game, and only start continuously
moving after a single key is pressed. Note that as per the original Pacman game, it is only necessary to stop Clara from moving
before the initial key press, and that after that, it is perfectly acceptable to have Clara continuously move in the direction that was
last selected.
Once you have determined, and tested, a suitable speed, you might notice Clara disappearing off the edge of the world. It is
important that Clara, and the Ghosts, at some point call the method “wrapAroundWorld()” to deal with this issue. This will
enable them to reappear on the opposite side of the world that they disappear off.
Major Assignment: Pacman Game
11
Part 2 – Animating Clara’s Movement
In order to animate Clara, you will need to call the “animate()” method. You may notice, that if you just call the “animate()”
method, that it cycles through Clara’s animation very quickly. This is because every time the method is called, it advances Clara’s
animation to the next frame.
You will need to figure out a way of not calling it every time the act method is run, but every so many times that the act method
is run.
HINT: If you are having trouble, try only calling “animate()” every second time the act method is called. Then try editing that
attempt to work only every third time the act method is called. It will ultimately be up to you to determine what looks best.
Part 3 – Clara Collisions with Trees and Ghost Walls
Obviously, Clara really shouldn’t be floating through the world; she should stop before hitting trees. She will also need to stop
before hitting the Ghost Wall, as only the Ghosts are allowed to pass through it.
It will be up to you to figure out how to stop Clara from passing through either.
HINT: You will need to use the “ghostWallFront()” method.
Part 4 – Eating Leaves and Winning
Clara will need to eat all of the leaves in order to win. First, she will need to be capable of eating leaves. If you are unsure of how
to do this, please revise practicals 1 and 2.
Once you’ve got Clara eating leaves, you’ll need to play the right sound. To do so, you’ll need to call the method
“playLeafEatenSound()” at an appropriate point in your code.
When you have Clara happily eating leaves, you’ll need to check that all leaves have been eaten and progress to the next level.
You will need to use the “allLeavesEaten()”, “advanceToLevel(char[][])” and “getCurrentLevelNumber()” methods. You will
need to somehow check what the current level is, and either advance to the next available level, or, if there are no more available
levels, advance back to the first level again.
Note that if you wish to return back to the first level, all you need to do is send LEVEL_1 or null to the
“advanceToLevel(char[][])” method.
Please note that actually adding levels to the “advanceToNextLevel()” method is covered in “Part 12 – Making and Adding
Levels”.
Once you can advance to new levels, or return to the first level after player completes it, it’s time to play the famous Pacman intro
music at the start of each level. Note that it is a requirement to not allow Clara or the Ghosts to move until after the intro music
has finished playing and the intro music must only play once for each level.
You will need to use the methods “playPacmanIntro()” and “isPacmanIntroStillPlaying()”. For now, just focus on stopping Clara
from moving while it is playing.
Note that after doing the above, you may wish to comment out the code that plays the intro music and prevents movement while
testing the game, but remember to uncomment it and add it back in when you’re ready to submit.
Major Assignment: Pacman Game
12
Part 5 – Moving Ghosts
Making the Ghosts move is very similar to making Clara move, but all your corresponding code must now be placed inside
Ghost.java. You will need to call the “move(int)” method and determine a suitable speed for Ghosts. Note that they should move
slower than Clara at this point.
It is also a good idea to add the code to stop them from moving when the intro is playing. You will need to use the method
“isPacmanIntroStillPlaying()”.
Don’t forget to call the “wrapAroundWorld()” method, so that Ghosts correctly move after reaching the edge of the world.
Before starting to code collisions and AI, there is something important you need to note: unlike Clara the Ghosts do not face the
direction that they are pointing. They will always look like they are facing up, and this will not change.
This is important to know for logic error checking.
Part 6 - Animating Ghosts’ Movement
Making the Ghosts animated works in the similar way as animating Clara. The method you call is exactly the same. Just like
Clara, you will need to figure out a way of calling the “animate()” method every so often that the act method is called, and that it
is up to you to determine how often they should animate.
Part 7 – Ghosts and Tree Collisions
Making the Ghosts detect that a tree is in the way, and stop, is achieved in a similar way as making Clara do so, you will even
need the same methods. Although, unlike Clara, there is no human player to tell the Ghosts where to go after they hit a tree. It is
up to you to decide what they should generally do when they hit one.
Note that telling the Ghosts where to face will involve using the “setDirection(string)” method, just like with Clara.
It may assist your general Ghost logic if you use the “getDirection()” method, which returns a string just like you would send the
“setDirection(string)” method, and that the string will be either “up”, “down”, “left” or “right”. It is not necessary to use the
“getDirection()” method, but you may find it helpful while testing and debugging your code.
Note that, just like Clara, the “setDirection(string)” method will not change the Ghost to a specified direction if there is a tree in
that direction.
Part 8 – AI and Intersections
An important part of making your Ghosts intelligent – is detecting an intersection and deciding which way to go. You should
start with simply selecting a random direction at an intersection by using random numbers covered in the lectures.
You will need to use the methods “treeAbove()”, “treeBelow()”, “treeToLeft()” and “treeToRight()” to detect an intersection.
It should be noted that, unlike most sensor commands that you’ve used before, like “treeFront()” and “treeLeft()”, these
commands are not relative to the direction that the Ghost is facing, they are relative to the screen itself. This means that a tree
that is above the Ghost, will always be above the ghost, regardless of which direction it is facing. This basic logic is true for all
four aforementioned commands.
Major Assignment: Pacman Game
13
Test them out in order to understand them. Drag one of the Ghosts around and see what is the result of calling those methods.
It should be noted that, for the most part, an intersection can be defined as a location where there are three or more directions to
go to.
HINT: You might want to check to make sure the Ghost has passed the intersection it is currently at before running the code to
choose a direction at an intersection. (Otherwise, it might just hang around the same intersection and not go anywhere).
For more advanced students, use the “isAboveMe()”, “isBelowMe()”, “isToMyLeft()”, “isToMyRight()” and “getClara()”
methods to give them a 50% chance of moving in the Clara’s direction at every intersection. Note that the “isAboveMe()”,
“isBelowMe()”, “isToMyLeft()”, “isToMyRight()” methods are further explained in the “Part 11 – Ghosts and Regeneration”.
Part 9 – Ghosts Colliding with Clara and Losing
The Ghosts must pose a threat to Clara, and as such, they must kill her if they catch her. In order to do so, you will need to check
if they have collided with Clara. To do so, you will need to use the methods “intersects(Actor)”, “getClara()” and
“makeClaraDead()” inside the Ghost class. Clara will need to use the method “isClaraDead()” to signal that the game is over.
It is important for the Ghosts to be continually checking whether they have caught Clara, and it is important for Clara to check
whether she is dead to prevent further movement and animation.
Once you have Clara capable of checking whether she is dead, you will need to show the appropriate death image, with Clara’s
method “animateDead()” and play the appropriate sound with the method “playClaraDieSound()”. It is important that the
method “playClaraDieSound()” is only called once.
It is also important that you stop processing keyboard input from the player at the point of Clara’s death.
Note that Clara’s “animateDead()” method only contains one frame, and so it is acceptable to run it every time the act method is
called after Clara is dead.
Part 10 – Eating Mushrooms and Making Ghosts Eatable
Clara will need to able to eat mushrooms and get her super power.
“onMushroom()” and “removeMushroom()”.
In order to do so, you will need to use methods
Then you will need to appropriately place the method “makeScared()” when Clara has eaten a mushroom.
The Ghosts will need to check whether or not they should be scared, and switch to the appropriate animation when they are. You
will need to use the methods “isScared()” and “animateScared()”. Note that the “animateScared()” method will need to be run
every so many times that the act method is called, just like the “animate()” method.
It is acceptable, but not compulsory to increase the Ghosts’ speed when they are scared, but they should be slower than Clara.
You will then need to ensure that the Ghosts don’t kill Clara when they collide with her if they are scared, and instead, die
themselves. It will be up to you to create your own variable that checks if the Ghosts are dead. You will need the
“animateDead()”and “playGhostEatenSound()” methods.
It is important to note that the Ghosts do not stop moving when they are dead, and that the Ghost’s “animateDead()” method
will need to be run every so many times that the act method is called, just like the “animate()” method.
It is also important that the Ghosts should not harm Clara when they are scared or when they are dead.
Also note that the game will automatically decide how long the Ghosts should be scared for.
Major Assignment: Pacman Game
14
Part 11 – Ghosts and Regeneration
When a Ghost is dead, it will need to return to the Ghost Healer, which is the red object near where the Ghosts start. It will be up
to you to decide how they should logically make their way back to the Ghost Healer object.
You will need to use the “isAboveMe(Actor)”, “isBelowMe(Actor)”, “isToMyLeft(Actor)”, “isToMyRight(Actor)” and
“getGhostHealer()” methods to make this happen.
Note that the “isAboveMe(Actor)”, “isBelowMe(Actor)”, “isToMyLeft(Actor)”, “isToMyRight(Actor)” methods need to be sent
an Actor, and that only two are available to you, through the “getGhostHealer()” and “getClara()” methods. You will need to
send those ‘get’ methods to the ‘is’ methods in order to get an appropriate answer.
An example of how to use the above functions is below:
“isAboveMe( getGhostHealer() );”
If the above example returns true, then the Ghost Healer is above the Ghost.
When the Ghost returns to the Ghost Healer object, it will need to make itself no longer dead. You will need the
“intersects(Actor)” and “getGhostHealer()” methods to make this work.
Hint: To avoid ghosts being stuck between 2 intersections - you may want to remember the direction the ghost came form (use
getDirection() for this) before setting a new direction and don't send the ghost to this direction again.
Part 12 – Making and Adding Levels
It’s time to understand how Clara’s world is designed in this game, and how to design and add your own levels.
When you first opened up “MyClara”, you may have noticed the first level at the top of the code. This is the format you will need
to use in order to create your own levels. See the image below for what you should roughly see when first opening up “MyClara”.
Figure 12.1 The first level is done for you
Major Assignment: Pacman Game
15
You will need to rename the level from “LEVEL_1” to “LEVEL_2”.
You’ll notice that a level is simply an array of characters that visually represent the level that they make. The characters used are
as follows:
‘#’ represents a tree, which is an obstacle to both Clara and the Ghosts.
‘.’ represents a leaf, which Clara needs to eat in order to progress to the next level.
‘ ’ represents nothing, which is essentially an empty tile.
‘%’ represents a Ghost. At least one must be present in every level you make.
‘?’ represents a Ghost Healer, which the Ghosts return to when eaten in order to come back to life. At least one must be present in
every level you make.
‘|’ represents a Ghost Wall, which is an obstacle that only Ghosts may pass through, and not Clara. It is connected to the group
of trees that make up the initial room that the Ghosts start in. At least one must be present in every level you make.
‘$’ represents a mushroom, which can be eaten by Clara in order to make the Ghosts scared and capable of being killed by Clara.
‘@’ represents Clara, it is where she will start at the beginning of the level. Only one must be present in every level you make.
The characters that make up the array of a level in Clara’s world are each surrounded by single quotes (‘’) and the characters in a
single row are separated by commas (,).
A single row in a level is surrounded by curly braces({}), and the end of a row of a level, that is not the end of the whole level, is
indicated by a comma after the closing curly brace (},).
This is all indicated in the image below.
Figure 12.2 How a level of Clara’s world is constructed in an array
Now you should have a rough understanding of how Clara’s world is constructed.
Major Assignment: Pacman Game
16
In order to build your own level, all you have to do is delete all of the streets of the level and write your own level in there,
following the above rules in order to present it and write it properly.
Just in case you’re still unsure of what to delete before you write your own level, only delete the section as highlighted in the
following image.
Figure 12.3 The part to delete to write your own level
Then write your own level, in between the curly braces that are left after you delete what was highlighted.
After you’re done, you’ll need to go to where you call the “advanceToLevel(char[][])” method and figure out a way of sending it
“LEVEL_2” when the player has finished “LEVEL_1”.
Now add a new level, and test it.
To add another level, simply copy and paste “LEVEL_2” inside MyClara and change it to “LEVEL_3”, and modify it just like
you did for level 2.
Add as many levels as you want, although technically you are only required to add one more level.
Part 13 – Life is Good
Now tidy up your code, add more levels if you want, but be sure to properly comment what you’ve coded.
You’re ready to hand in your assignment. Life is good!
Demonstrate the game to your tutor in the last tutorial class (Week 14)
Submit all files to your tutor by 11.59PM Friday 31 October.
Late penalties apply for submissions after the due date above (see learning guide).
Major Assignment: Pacman Game
17