major assignment
The major assignment involves producing a Pacman style game in Clara Online.
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 Claras 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.
Figure 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 one file, this time you will have to edit Ghost for all the logic related to the Ghosts as well as MyClara 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.
Important note 3
For this assignment you should no longer have a main loop (for or while) inside act(). Instead you should rely on the act() method being executed repetitively (similar to how you did in Practical 2). The stop(); command MUST NOT be used in this assignment.
Claras vocabulary
For this assignment, the following commands are available for Clara right from the start:
Name Description Input 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() Checks if there is a ghost wall in front of Clara. Nothing.
true if there is a ghost wall in front of Clara.
false if there is not a ghost wall in front of Clara.
getDirection() Gets which direction Clara is facing. Nothing. 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.
left if Clara is facing to the left 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.
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.
Nothing.
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.
For more information, see the isScared() function in the Ghosts vocabulary. Also see the section Part 10 Eating Mushrooms and Making Ghosts Eatable
Nothing. Nothing.
isScared() Clara checks whether the Ghosts are still scared of her. Nothing.
true if the Ghosts are still scared of her.
false if the Ghosts are no longer scared of her.
animate()
Clara continues her animation by one frame.
For more information, see the section Part 2 – Animating Claras Movement.
Nothing. Nothing.
animateDead()
Clara visually shows that she is dead.
For more information, see the section Part 9 – Ghosts Colliding With Clara and Losing
Nothing. Nothing.
onLeaf() Checks if Clara is on top of a leaf. Nothing.
true if Clara is on top of a leaf.
false if Clara is not on top of a leaf.
removeLeaf() Clara removes a leaf that she is standing on. Nothing.
Nothing.
Will show an error if she tries to remove a leaf that isnt there.
onMushroom() Checks if Clara is on top of a mushroom. Nothing.
true if Clara is on top of a mushroom.
false if Clara is not on top of a mushroom.
removeMushroom() Clara removes a mushroom that she is standing on. Nothing.
Nothing.
Will show an error if she tries to remove a mushroom that isnt there.
allLeavesEaten() Checks if Clara has eaten all of the leaves in the level. Nothing.
true if there are no more leaves for Clara to eat.
false if there are still leaves for Clara to eat.
isClaraDead()
Checks if Clara should be dead.
For more information, see the function makeClaraDead() in the Ghosts vocabulary and the section Part 9 Ghosts Colliding with Clara and Losing
Nothing.
true if Clara should be dead.
false if Clara should not be dead.
playClaraDieSound()
Plays Claras death sound.
For more information see the section Part 9 Ghosts Colliding with Clara and Losing
Nothing Nothing
isClaraDieSoundStillPlaying()
Checks if Claras death sound is still playing.
For more information see the section Part 9 Ghosts Colliding with Clara and Losing
Nothing. Nothing.
playLeafEatenSound() Plays the sound of Clara eating a single leaf. Nothing. Nothing.
playPacmanIntro() Plays the pacman intro sound. Nothing. Nothing.
isPacmanIntroStillPlaying()
Checks if the pacman intro sound is still playing.
For more information see the section Part 4 Eating Leaves and Winning
Nothing. Nothing.
wrapAroundWorld() Enables Clara to automatically move off one side of the game, and end up on the other. Nothing. Nothing.
getCurrentLevelNumber() Gets the number of the current level. Nothing. An integer representing the current level number. E.g. If the current level is 1, then it will return 1.
advanceToLevel(char[][])
Changes the current game level to the one specified. If it is sent null, then it will automatically load the first level.
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
A level as per Part 12 Making and Adding Levels, or null. Nothing.
Ghosts 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 Claras, some are slightly different, and some completely unique to the Ghosts. Be sure to read the following list of commands carefully:
Name Description Input 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.
false if there is not a tree in front of the Ghost.
treeAbove()
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.
Nothing.
true if there is a tree above the Ghost.
false if there is not a tree above the Ghost.
treeBelow()
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.
Nothing.
true if there is a tree below the Ghost.
false if there is not a tree below the Ghost.
treeToLeft()
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.
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.
treeToRight()
Checks if there is a tree to the right 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 right of it, will always be to the right it.
Nothing.
true if there is a tree to the right of the Ghost.
false if there is not a tree to the right of the Ghost.
getDirection() Gets which direction the Ghost is facing. 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.
left if the Ghost is facing to the left 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.
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.
Nothing.
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 An integer, which specifies how fast you want the Ghost to move. Nothing.
isScared() The Ghost checks whether it should be scared of Clara. Nothing.
true if the Ghosts should still be afraid of Clara.
false if the Ghosts should no longer be scared of Clara.
animate()
The Ghost continues its animation by one frame.
For more information, see the section Part 6 – Animating Ghosts Movement.
Nothing. Nothing.
animateDead()
The Ghosts continues its death animation by one frame.
For more information, see the section Part 10 Eating Mushrooms and Making Ghosts Eatable
Nothing. Nothing.
animateScared()
The Ghosts continues its scared animation by one frame.
For more information, see the section Part 10 Eating Mushrooms and Making Ghosts Eatable
Nothing. Nothing.
getClara()
Gets Clara.
Is to be used with the isAboveMe, isBelowMe, isToMyLeft and isToMyRight commands, explained below .
Nothing. Clara.
getGhostHealer()
Gets the Ghost Healer.
Is to be used with the isAboveMe, isBelowMe, isToMyLeft and isToMyRight commands, explained below.
Nothing. Ghost Healer.
isAboveMe(Actor) Checks whether the specified Actor is above the Ghost. Clara or Ghost Healer.
true if specified Actor is above the Ghost.
false if the specified Actor is not above the Ghost.
isBelowMe(Actor) Checks whether the specified Actor is below the Ghost. Clara or Ghost Healer.
true if specified Actor is below the Ghost.
false if the specified Actor is not below the Ghost.
isToMyLeft(Actor) Checks whether the specified Actor is to the left of the Ghost. Clara or Ghost Healer.
true if specified Actor is to the left of the Ghost.
false if the specified Actor is not to the left of the Ghost.
isToMyRight(Actor) Checks whether the specified Actor is to the right of the Ghost. Clara or Ghost Healer.
true if specified Actor is to the right of the Ghost.
false if the specified Actor is not to the right of the Ghost.
intersects(Actor) Checks whether the specified Actor is currently intersecting the Ghost. Clara or Ghost Healer.
true if the specified Actor is currently intersecting the Ghost.
false if the specified Actor is not currently intersecting the Ghost.
makeClaraDead()
Makes Claras isClaraDead() method return true.
For more information, see the section Part 9 Ghosts Colliding with Clara and Losing
Nothing. Nothing.
playGhostEatenSound() Plays the sound of a Ghost being eaten. Nothing. Nothing.
isPacmanIntroStillPlaying()
Checks if the pacman intro sound is still playing.
For more information see the section Part 5 Moving Ghosts
Nothing. Nothing.
wrapAroundWorld() Enables the Ghost to automatically move off one side of the game, and end up on the other side. Nothing. Nothing.
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 Keyboard.isKeyDown(String) method. Try passing the movement constants defined inside MyClara.java as input for Keyboard.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 Claras 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 Claras 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 youre able to process keyboard input and make Clara respond to it by turning in the right direction now its 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.
Part 2 – Animating Claras 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 Claras animation very quickly. This is because every time the method is called, it advances Claras 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 shouldnt 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 youve got Clara eating leaves, youll need to play the right sound. To do so, youll need to call the method playLeafEatenSound() at an appropriate point in your code.
When you have Clara happily eating leaves, youll 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, its 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 youre ready to submit.
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().
Dont 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 youve 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.
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 Claras 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 Claras 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 Claras death. Note that Claras 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. In order to do so, you will need to use methods onMushroom() and removeMushroom().
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 dont 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 Ghosts 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.
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.
Part 12 Making and Adding Levels
Its time to understand how Claras 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 1
You will need to rename the level from LEVEL_1 to LEVEL_2.
Youll 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 Claras 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 (},).
Now you should have a rough understanding of how Claras world is constructed.
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.
Then write your own level, in between the curly braces that are left after you delete what was highlighted. After youre done, youll 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 youve coded. Youre ready to hand in your assignment. Life is good!
Demonstrate the game to your tutor in the last tutorial class
Submit all files to your tutor by 2