Lab session 8

The final assignment for the course is to develop a simple Tower Defence game. You can find many examples of such games on the internet, for example http://www.flasharcade.com/tower-defence-games/play/azgard-tower-defense.html

Tip: Don’t spend all your time playing these games instead of working on the assignment!

Our game is not very graphically advanced, the end result should look something like this:

Of course, you are welcome to add as many improvements to the graphics as you like.

Description of the game

In our tower defence game, a “monster” follows a fixed path from one side of the screen to the other. Along this path, the player can place a number of defensive towers to protect against the monsters.

Each tower has a certain reach and fire power. When the monster enters the reach of the tower, it will fire projectiles at the monster. Each projectile has a 50% chance to hit the monster, dealing damage equal to the tower’s fire power. Any damage dealt is subtracted from the monster’s health value.

The monster starts with a positive value for its health. When its health reaches zero or below, the monster dies and is removed from the game. On the other hand, if the monster manages to reach the end of the path without dying, the player loses the game.

Step 1: Implementing the model

Start by implementing a model of the game without thinking too much about the graphical interface. This model should consist of several classes that represent the different parts of the game, as well as a main class TowerDefenceLevel that represents the overall state of the game.

In this phase, you should employ a bottom-up strategy similar to Lab Session 6: pick one class that only depends on other classes you have implemented already, implement it, and then continue with the next class until you have implemented all classes.

You should determine yourself what methods and attributes you need for each class. Here are a few suggestions for methods that might be useful:

For each method (except simple getters and setters) you should write a comment describing what the method does, as well as any pre- and postconditions it has.

Tip: when you are finished with implementing a class, write some simple tests for it before moving on to the next class.

Monster movement. The monster should automatically move one step each time the game advances. If the monster can go in multiple directions, it should choose one direction at random (monsters are not very smart). However, it should remember its previous position so it does not go back in the direction it came from (monsters are not that dumb).

Step 2: Implementing the GUI

In the second phase you can add the graphical interface for the game. For the GUI, the main class is GUITowerDefence, but you can also add more classes if you need them. In this phase you should follow a ‘top-down design’: start by implementing the top-level window and then gradually move down to each of its components until everything is working.

Some details:

The file GUITowerDefence already contains some code to help you to get started. Try to understand the code and feel free to change anything you like!

The main logic to update the game to the next state is located in the actionPerformed method of the EventLoop inner class. This method is called every step of the game (by default once per second).

In the code, there are some example methods that show how to create components that display a tower or a monster. You can use them as they are or change them however you like.

Tip: You can remove a component from a JPanel by calling the method remove(Component c). You can remove all JLabels from a JPanel with the following code:

The player should be able to click on a green space to build a new tower at that position (in a real tower defence game, there would also be a cost preventing the player from building infinitely many towers, but it is not required to implement that for this assignment).

Tip: One way to allow the player to ‘click’ on a green space is by adding an invisible button to the location. Here is an example of how you could create an invisible button:

More levels and extensions

In the TowerDefenceLevel class, there are some more sample levels you can try out once you are finished. Here are some screenshots of the various levels:

For some of these levels, you may want to improve the intelligence of the monster, or else it may wander around through the maze forever!

If you want, you can also extend the game with more features, for example:

Have fun!

Menu