So I have started learning Java for my senior project at school and I would really like to make a tower defense game as my project. I have been looking for tutorials that would help but I am having trouble finding some. I have been using www.javacoffeebreak.com and I've been using their online text book to help me learn the basics. I'm pretty sure I will be told to try learning flash to do this but I have tried using flash and I really don't enjoy using it, but with Java I have a BLAST coding! If anyone has any tips or tutorials they could give me that would be great, I am not very far with the online textbook, I have just started learning about animation and drawing very basic graphics to the screen but at least its a start! I would greatly appreciate any tips and tutorials!

Yeah, I agree. I also know java but would have trouble trying to build a tower defense game. Any tutorials that you guys could link us to would be greatly appreciated.

I have never played a tower defence game, but why dont you just make it like you would make any game? You would learn alot more from following a tutorial that covers the basics of a game loop and drawing etc etc but actually making a different game, makes you think more. Its what I did!

I only had a breif look but this looks quite good:
http://www3.ntu.edu.sg/home/ehchua/programming/java/J8d_Game_Framework.html

Just out of curiousity, what's a tower defense game?

I was wondering the same thing, Ive seen loads of games described as such but never played any. From the sounds of wikipedia its like a real time stratergy but you can only build towers and you have to stop enemies getting to certain postions?

yip pretty much hanvyj. check out Flash Element TD. That was one of the first and demonstrates pretty much everything a TD game is supposed to be. there are more things that some TD games have incorporated like upgrades, A good example here is Ant Buster. together these two games encompass what TD games can be pretty much completely. And thanks for the game tutorial hanvyj, looks good.

A tower defense game is a game where there is (usually) a linear path for the enemies to follow and you have to build towers (also referred to as turrets) along the borders of that path to shoot the enemies before they reach the end of the path and take your health down. There are also a different type of tower(turret) defense game where there is no linear path but instead you make the path they follow by building your towers in the path you would like them to follow, for example Madness Combat Defense is a non linear, open path tower defense while Bloons Tower Defense 4 is a linear path based tower defense.

So the towers are being built in real time, while the enemy advances? Or are you setting up the defense, and then watching it play out?

The latter would be an interesting game - abstract strategy, resource management, visualization - and probably a bit easier to program. Still not easy, you'd have a fair bit of work to do, but easier. The former, you're interacting with the user in real time while calculating the computer's moves. That seems to me a more difficult proposition.

There's an old unix game called "robots" which might seem unrelated at first, but it would probably be a good starting exercise for you. The problems are simpler, but they seem to lead towards some of what's required for your tower defense. Just a suggestion - have fun!

Okay so i have read through the source code on this website and put them all into the appropriate classes and i wanted to try running it but i get this "Reference to list is ambiguous, both class java.util.list in java.util and class java.awt.list in java.awt match." Any ideas?

Yeah, you'll see that if you're importing both java.util.* and java.awt.*, and then trying to declare something like this:

List l = new List()

Both of those libraries have classes called List, so your compiler is getting confused about which one you mean. Since you didn't write the code, you're going to have to go in and figure out which one is meant and either use a fully qualified reference or else use explicit imports of each class you want:

import java.util.List;
import java.awt.Foo;
etc.

I prefer to use explicit imports, if you're willing to keep them up to date. It serves as a level of documentation, telling the next guy "I really meant to use these paticular classes" rather than "I thought I might need everything in javax.swing".

In this case, I would bet that the util class is more likely to be the one you want, but you should look at the documentation for both classes, and then examine the code to see which one might be meant.

What exactly is the difference between the two?
Whichever one is needed is for a game of snake and I believe it holds the snake segments, I don't have the exact line in front of me because I'm on my phone at the moment.

This is a good chance for you to do some research. Look at Sun's documentation for the two classes - java.awt.List and java.util.List - and see if you can work it out.
You should already be able to guess that one deals with windowing and GUI display, while the other is likely to be a more generically useful utility class, probably a data structure of some sort. You've given me enough information that I'm willing to bet on which it is, but have a look at the APIs and the code and see if you can work it out.
One thing you can be sure of, they won't overlap in their function at all. awt and util cover quite different domains of functionality, so looking at the code should tell you straight away whether you're looking for an awt or a util class.

Hmm, well I still seem to be confusing myself, I'm also having a tough time completely comprehending what the author is doing here. I read through the source code and I even type my own copy with comments into my compiler so that I'm actually reading the material but what he is doing i feel isn't explained in enough detail.

I feel as though I should ask something a little more related to my original topic and maybe getting help with this would help me get a little more on tack.

I have finally started understanding how to draw things to the screen but I'm still not sure how to handle mouse and keyboard events, as far as a tower defense goes I shouldn't be too worried about keyboard events right now but rather drawing something to the screen where the user has clicked.

Say I draw a route to the screen, a green square with a zigzagged black route through it, what would I need to understand and what would I need to do to make it so the user can only click on the green and so that a red square is placed only on the green where the user clicked?

To make things easier I would have grid like lines displayed over the green to show where the user could spawn a square.

Are you making this game with custom rendering, ie you will be drawing the gridlines etc?

If so, one way to do it would be to have your "tiles" stored in a 2D array. Have a method for finding out which co-ordinate a screen-point is in (using the width of the tiles on the screen)

Take a look at MouseListeners, they provide a method (mouseClicked) that give you the point of the click, you can convert this to array co-ordinates. If you array holds say a value of 2, then its "green" and place a tower there (set that coordinate to 3 meaning "tower" etc etc

Okay lets say I'm going to draw a checkerboard to the screen then using a 2D array I'm going to assign a variable to each square.

public void gameDraw(Graphics g) { 
        int row;
        int col;
        int x,y;
        int board [][] = new int[8][8];
        int BLANK = 0;  // location empty
        int WHITE = 1;  // white piece
        int BLUE = 2;   // blue piece
        
        for (row = 0; row < 8; row++){
            for(col=0; col < 8; col++){
                board[row][col] = BLANK;
                x = col*80;
                y = row*80;
                if ((row%2)==(col%2))
                    g.setColor(Color.red);
                else 
                    g.setColor(Color.black);
                g.fillRect(x,y,80,80);
            }
        }
       
    
    }

That's what I'm using to draw the checkerboard. Lets say i want to assign the BLANK int to every red square and then the top 3 rows assigned to BLUE and have it draw a blue circle on the top 3 black lines and have WHITE assigned to the bottom 3 rows and have it draw white circles on all 3 bottom row black squares, what would I need to do?

Okay lets say I'm going to draw a checkerboard to the screen then using a 2D array I'm going to assign a variable to each square.

public void gameDraw(Graphics g) { 
        int row;
        int col;
        int x,y;
        int board [][] = new int[8][8];
        int BLANK = 0;  // location empty
        int WHITE = 1;  // white piece
        int BLUE = 2;   // blue piece
        
        for (row = 0; row < 8; row++){
            for(col=0; col < 8; col++){
                board[row][col] = BLANK;
                x = col*80;
                y = row*80;
                if ((row%2)==(col%2))
                    g.setColor(Color.red);
                else 
                    g.setColor(Color.black);
                g.fillRect(x,y,80,80);
            }
        }
       
    
    }

That's what I'm using to draw the checkerboard. Lets say i want to assign the BLANK int to every red square and then the top 3 rows assigned to BLUE and have it draw a blue circle on the top 3 black lines and have WHITE assigned to the bottom 3 rows and have it draw white circles on all 3 bottom row black squares, what would I need to do?

You are mixing up your logic and your rendering here. I assume you are calling draw() from a loop?

I'll go through some of the code with words:

1) we are going tro draw what is going on. (call the draw method)

2) we make a new board object.

3) we loop through every item on the board and set each item to be BLANK;

4) we draw a checkerboard

5) we throw away the board object

If you think about it, you dont want to be making a new board object every time you want to draw it on the screen, or set everything to blank in the panting, or throw it away afterwards.


What you need to do to make it work better is a few things

1) you board object needs to be a global variable (or a field of the class), this way any method can use it (modify it/read it) at any time - not just the one that created it.

2) you need to set up your board object before doing any drawing, a simple way to do this would be to do it in the constructor or a "initializeGame" method - in this you can loop through it and set the appropriate tiles to BLANK or WHITE or BLUE (and have loading from file options later etc etc) and is only called once before the game loop.

3) your drawing object should really only be reading things. It shouldn't ever need to modify an object related object (such as where the towers/pieces are ie the board) its simply to read whats going on, and draw it - eg:

for (int row = 0; row < 8; row++){ // declairing row/col here will make things clearer
  for(int col=0; col < 8; col++){  // in the long run
     if (board[row][col] == BLUE)
     {
         g.setColor(Color.red);
     }
     else if (board[row][col] == WHITE)
     {
         g.setColor(Color.black);
     }
     if (board[row][col] != BLANK)
     {
        drawCounter(row,col);
     }
}

It's probably worth doing to build a few strategy games in the command line before you try to combine strategy and GUI programming.

Othello isn't too hard to implement, and you can even make the computer play a respectable game. Try playing that on the command line (drawing a new board for each move) and writing it so it can be extended to a GUI. (ie, separating the rendering from the logic). Then, when you have it working on the command line, figure out how to extend it to a GUI.

This would require you to think of the user interface as an object. One way to do this would be to think about what sorts of things the logic of the program is going to need the interface to do for it. For example, you might want it to get a move from the player. You don't want "get a move" to put that move on the screen, because it might not be a valid move, so maybe you want it to "display a move" (for Othello, that would be a pair of coordinates and a player ID, white or black). "Display a move" in othello might involve flipping other pieces - in fact, it'll have to. Probably you want a way to tell the interface to redraw the board to reflect flipped pieces. It might be easiest to do this by just passing a new grid to your interface, you'll have to think about it.
You'll also maybe want to be able to reject a move - if someone tries a play that doesn't flip an opposing piece, for example. This would tell the UI to prompt the player for a better move.

Notice how none of this tells the interface how to display the board or what sort of input it should accept - you can implement this on a CLI, or on a GUI, or on some speaking device for the visually not-as-able, it doesn't matter. It also keeps the interface from ever thinking about the rules of the game - the interface doesn't care, and it shouldn't (any more than you want a chess board to know the rules of chess).

You'll probably find your tower game easier to program if you try a few simpler things first, as I said before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.