Tower Defence games

Blitz3D Forums/Blitz3D Beginners Area/Tower Defence games

Destroyer(Posted 2009) [#1]
Ok ive been playing a few tower defence games on my ipod and would like to know how easy or hard a TD game is to make.

Also what sort of routines do i need to be looking into.

Most of them use a 2D/3D grid to place the towers on the main play area.

Path finding routine for the enemy is used by most (tho Field Runners i think just uses some kind of avoidence routine).

A routine to select icons (towers) and drag 'n' drop them onto the main play area.

Upgrade / Sell routine for the towers

Thats all i can think of at the mo(my brains gone to sleep lol)

So does any body have ideas on whats needed, or maybe you have done a simple Tower Defence game yourelf and don't mind throwing up a few routines.

Thanks

Destroyer


Warner(Posted 2009) [#2]
That first question can't be answered. It depends on your skills.
It seems to me that you allready have a good idea about what you'll need to do. Best is, depending on your experience, to start off with several smaller experiments in which you focus on a single element of the game, pretty much the things you've summed up allready. Then, if you know how you want to setup each element, you can bring everything together in the final endproject.

Firstly, I would look into types (Type). I suppose the field runners could best be types, as well as the bullets and the towers. Each type could have fields, such as these:
creeps
-x/y location
-type/sort/kind
-energy
bullets:
-x/y location
-dx/dy direction
towers
-x/y location
-type/sort/kind
-range
First goal would be to write the creeps, as a Type. They should be able to rotate, and walk from the left to the right of the screen. For rotating images, look for prerendered rotation, since b3d can't rotate 2d images realtime.
If you don't want to prerender, use 3d to simulate 2d. 3d objects can rotate realtime.

Collision detection might be an issue to look into. RectsOverlap should be sufficient, since you don't need pixel-perfect collisions.
So, alter the program so that you can delete enemies with the mouse.

Next would be, create bullets. You could write a program in which you can fire bullets with and from the mouse. If a bullet hits the enemy, it should be deleted.

It would be good to use Functions to create/update/draw each element. CreateBullet, UpdateAndDrawBullets, CreateEnemy, UpdateAndDrawEnemy. This will result in a more flexible and more readable program.

For the grid, you could use an array (Dim). The array can be used to check if a certain position is allready filled with something, and you can use it to let the field runners determine their route. Best is to look at the way to store Types into arrays. That way, you can immediately access the type instance that is on a certain x,y location.

To convert from mouse coords to grid coords, divide by the tilesize:
gx = mousex / 32
gy = mousey / 32
Due to the fact that integers are truncated (rounded to below) by default, the fractional part will dissapear, leaving the tile location.
The other way around, from tile to x,y:
screenx = gx * 32
screeny = gy * 32

For determining their route, I think that each enemy takes the shortest path to the endpoint, with the least danger possible.
You could create a 2d grid (array), and use a number that determines that danger level for each location. If a tower is placed, all grid tiles that are in it's firing range should increase their danger value. The stronger to tower, the bigger the increase should be.
By using that system, if two towers are near each other, all tiles that are in their overlapping firing range will automatically have a bigger danger level that tiles that are in the firing range of only one tower.
If tiles are not in any towers firing range, their danger level is zero.
A creep can than scan each vertical colomn and look for the tiles with the lowest danger value. If you combine that with the distance that such a tile has from the creeps position in the row on the left side of the row you are checking, it should be able to decide which route it should take. It would most likely come round to calculating a few possible routes, sorting them on their danger and length, and then choosing the first one. (The shortest and least dangerous)
        0 0 0 0 1 0 0
        0 0 0 1 2 1 0
start-> 0 1 0 0 1 0 0 ->finish
        0 0 0 0 0 0 1
        0 0 0 0 0 1 2


Upgrading, selling and dragging seem to me less relevant to the gameplay than bullets/enemies/towers. Same goes for the game menu.
In that sence, I'd suggest to start off with the basics, and upgrade the game when the basic layer is solid enough to build upon. You should prob. best not attempt to create the game in a linear way, I mean, in terms of the creation process the starting point is not the game menu, and the endpoint is not the gameover screen. The creation process should in my opinion start with the simplest form of the game, with one type of tower, one type of enemy and one type of bullet. Also it is a good idea to save each subversion under a different name as you go along.

Here is an example program that might be helpful:



Destroyer(Posted 2009) [#3]
Good Point Warner about the upgrading.

Ive got a few routines done(just simple tho), But like you said keep it simple to start with then build on it.

Ill keep it 2D for now using circles and rectangles for enemy and towers(just 1 of each) then try it with 3D

Thanks for taking the time to read my post and giving some good pointers.

Oh and ill take a look at your code :)

Thanks

Destroyer


Nate the Great(Posted 2009) [#4]
for selecting towers and selling and placing etc it is best to use a combo of arrays and types but its kind of hard to explain in words so its best if you figure it out for yourself.

thats my 3 cents ;)


Destroyer(Posted 2009) [#5]
Thanks for the tip Nate the Great :)

I know a lil about types and arrays so will look into using them.

I also see that you are making a tower defence game. looking good so far :)

Thanks
Destroyer


Nate the Great(Posted 2009) [#6]
I think I was kind of vague. here is what I was trying to say...

use an array to store where all of your towers are so the array might look like this

0,0,0,1,0,2,0
0,0,2,0,0,0,0
0,0,0,0,0,0,1
0,0,0,0,0,0,0
0,0,1,0,0,0,0

.. thats just a random array for the purpose of an example the 1s are one type of tower and the 2s are another type of tower..

anyway for each tower in the tower type, the x and y values where the tower existed on the array would be variables in the type.

I then have another array for the selected squares where only one square at a time could be selected

0,0,0,0,0,0,0
0,0,1,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0

so now that I have thouroughly confused you with seemingly meaningless stuff here is how it all works together

the 2nd array starts out blank.. thus no towers are selected
the 1st array then stores all the towers.. here is what happens every loop

1. if the player clicks then check to see if he clicked on a tower by cycling through all the tower types
2. if the player clicked on a tower then set a var in that type to true and get that tower's x and y on the 2nd array, erase the second array and set a single square in the array where the tower is located to true thus there is never more than 1 thing selected
3. as the towers are updating every loop, check if the towers select var is true if so then check if it is true on the 2nd array.. if not then set the tower's selected status to false
4. now there will only be one tower selected at any one time!

if you dont understand then post and I will try to get some example code up soon it is really way simpler in coding.. you can also adapt this system for anything that requires the user to select one thing at a time good luck


Nate the Great(Posted 2009) [#7]
here is some example code.. sorry it is so messy but it is simple and should be self explanitory with the above explanation. It has a bare minimum structure.




Destroyer(Posted 2009) [#8]
Thanks for the example Nate ill look through it.

I also like the idea of using the 2 arrays for tower placments and checks


Thanks

Destroyer


Ross C(Posted 2009) [#9]
You could always just have a path the enemies take. They will always follow that path. Maybe have two paths even, but keep the attack pattern the same. Each enemy will go exactly the same way as last time.

I haven't played a great deal of tower defense games, but the ones i have played (and enjoyed) the enemy paths were static.

It helps in the harder stages, as you can predict better and plan better.


Destroyer(Posted 2009) [#10]
Yes that is true Ross C

Field Runners seem to run from A-B, and only change direction if they collide with a tower.

Geo-Defence use a path to get from A-B.

Im thinking of using way-points for my enemy to follow(not sure if this is the best way).

But for now while im putting some routines together, Ill try the Field Runners method(i think its the simplest).

Thanks

Destroyer


Warner(Posted 2009) [#11]
Maybe it's an idea to try out a number of methods/models and see which one is the best.


Blitzplotter(Posted 2009) [#12]
An interesting thread, my son has played a number of tower defence games and is quite taken with them, might have a play with this myself in B3D. Nate the great's sample code looks interesting


Destroyer(Posted 2009) [#13]
@Blitzplotter

Yeah i love them myself lol

Im gona keep it 2d for now then try a 3d version.

Thanks

Destroyer


Destroyer(Posted 2009) [#14]
Ok not much to show. Ive cludged a few routines together lol



The tower tracks the mouse pointer, and when the mouse pointer gets in range of the tower it shoots at the mouse pointer(like i said im keeping it simple lol).

Im working on the grid and tower placement next.

*EDIT* ive just added 1 enemy, so tower tracks and shoots enemy instead of the mouse.

Thanks

Destroyer


grindalf(Posted 2009) [#15]
Ive not looked through the code but i ran it and it looks intresting
Ive never played a tower defence game before but i can see how this could be really fun.

looking good :D


Destroyer(Posted 2009) [#16]
@grindalf

They are very addictive, you must try one :)

Thanks

Destroyer


srcoder(Posted 2009) [#17]
Tower Defence games are very addictive and not too hard in Blitz3d.

I gave it a go and the end result might give you an hour of distraction or so.......

I can release the code if you are interested.
Here is the link and look up Sheep Defence in the forums

http://webkiss.100webspace.net

Have fun!


Destroyer(Posted 2009) [#18]
@srcoder

Ive downloaded and played your game before lol

I feel sorry for the poor sheep but ya have to shoot at something :)

By all means upload your code (can't hurt to look through code that works)

Im keeping mine 2D for the moment but will convert it to 3D at some point.

I find it easier to code in 3D for some reason, but ive started it now so ill see how far i get.

Thanks
Destroyer


Ross C(Posted 2009) [#19]
The idea of shooting with the mouse, was something i thought about too. All towers will automactially track and shoot targets, but in some cases it will be better to manually aim :o) Looking good man! Agreed about 3d.


Destroyer(Posted 2009) [#20]
@Ross C

Yeah you could have a Boss Creep every so many levels that needs you to target it.

I wish some one would start a Tower Defence community project. You can learn alot from this type of game.

Thanks

Destroyer


Ross C(Posted 2009) [#21]
Community project eh... I'm a sucker for them, as i'm curently working on a tower defense, 3D game. So far, my designs have:

Set paths (no path finding, as i'm starting out with something i can control. Pathfinding may cause enemies to get stuck if not done properly :o) It just one less hassle in my book and i've seen games work and play very well with a fixed path(s)

Make manual targetting an optional upgrade. Perhaps give a first person view from the tower?

Tower shields: To protect from the enemies that attack towers. Damaged towers are less effective. Shields are auto-regerative. Can only be used when you purchase the upgrade.

Standard Upgrades: Rate of fire, Power and Range. Appearance alters with each upgrade.

Each tower made from modules in a 3d sense. That way you only need to create a 2 or 3 gun meshes for different levels of upgrades, then you can remove and parent a new gun, or radar module for increased range... etc etc

Each level made of a blitz mesh, created by either a landscape editor, or a heightmap with textures. I have a function for that purpose.

Do you have an email address i might contact you with?


Destroyer(Posted 2009) [#22]
Sent you an e-mail Ross :)


srcoder(Posted 2009) [#23]
Sheep Defence contained a "zoom to turret" function where you could click on any turret and manually control it.
Turned out too hard to shoot slow moving bombs and too easy with lasers.
Biggest challenge was the mathemetics of "Leading the target". Let me know if you get stuck with that one!!
I used L3DT to create and texture the terrain.
I wrote a custom tool to create the path/s.

I would be interested in helping out if needed. Some of my existing code could be useful.


Nate the Great(Posted 2009) [#24]
did I read that right? is this a community project? if so I can help since I have some experience in that field although I cant become a major part of it because I have too much else going on


Destroyer(Posted 2009) [#25]
@Nate

lol it was just my wishfull thinking that some one would start a community project.

Thanks

Destroyer


Ross C(Posted 2009) [#26]
Well, my thoughts on that were, the cannons were only good on slow moving enemies. Eliminates the problem, and adds some stategy. To kill faster moving ones, you either build another type of tower, or take manual control :o)