If this is your first visit, be sure to check out the FAQ by clicking the link above. You have to register before you can post.

If you have recently signed up, please make sure you log in so that your activity is registered. This is required for automatic promotions to work and to ensure your account is not deleted during the registration process.

Recent Tutorials

Artillery Game Tutorial - Part 2: The Dirt

Hey guys, now that the intro and all the dull stuff is over with, we can get right into the goods! Playing in the dirt.

In this first lesson I'm going to show you how to generate a nice random 2D battle ground for your tanks to kill each other in. You'll be able to then display it on top of your sky background that will either be a single color or the nice bitmap I've included or one of your own making.

Okay enough with the explanations, lets get right into the code!

Some More Files

I know that I've already given you some files to start with in the intro, but these ones will be the beef of our game code now. Here is what they are for;

GameConstantsUnit.pas -- All the default settings for our game.GameObjectUnit.pas -- Houses all our game objects. As I mentioned before in the intro we'll be using OOP.

Game Constants

Lets get this one out of the way shall we? I'll be supplying this file at the end of this tutorial, but if you wish you make simply copy and paste the following code into the file named GameConstantsUnit.pas.

Init() will create the battlefield object!GenerateLand() will do the initial generation of the shape of the land.SmoothenLand() will help make the land more usable in the game.DrawSky() will draw our sky background.DrawLand() will draw the land we generate.

Initializing The Battlefield

I don't see a need to go into great depth here so here is the function straight out.

ScreenWidth & ScreenHeight are the dimensions of the game screen you'll be using.Land & Sky are the colors of the land and sky.useBGImage is the switch for using a bitmap instead of a solid single color for the sky background.BGImageFile is the filename within the images directory of the bitmap to load into the Background surface.

Initial Generation

To generate our land we're going to go through it in a couple of passes. Once to get the general shape and then again afterward to make it a little more practical for the game's use. But for now lets look at how we'll get the basic 'shape' of the land we want.

GenerateLand() uses 2 values to keep the surface of the dirt with a high and a low range. There are Highest and Lowest.

Have a look at this example to see what effect this has...

Notice how the peeks will never go above the Highest value and the valleys will never go below the Lowest value?

Now we have to figure out the rate of which we the land will vary as we go along the surface. We could simply use a random value between Lowest and Highest but that wouldn't produce a very nice shape for our land.

Instead what we'll do is have a Variation value that will govern the change in the heights of each segment of land from left to right. Each segment will be 1 pixel wide for the most precise effect!

Here you see we cycle through each segment along the x-axis and calculate a height for the land based on the previous calculated height.

If the land travels higher than the Highest range, it will recalculate another random height until it fits under the required range. Also when a height value is created lower than Lowest it will, instead of recalculating the height, level the height to the Lowest value.

Both high and low values are treated this way to simulate peeks and valleys in a more realistic way.

So now we have our land, but it's very jagged, ugly and unusable. Worry not! This can be done quite easily with a 2nd pass on the land height values.

The technique that I will be using to create more usable land will involve passing through a set of the initial generated land. It'll take a number of segment values from the left and right sides of the current segment and calculate an average which it will be changed to. The value SmoothSize will determine the amount of samples on each side.

You will also notice that we must stay within the bounds of the screen so we will be checking that the code that gathers the average from other segments does not drift off the screen.

Mass is the total added height of all of the segments within the SmoothSize area including it's own height.NumOfMassSamples is the count of how many actual samples were taken regardless of the value feed into SmoothSize.

This should give you a relatively nicer result and land that you can actually use to place tanks on and have them accurately aim and fire at each other.

Drawing It

Okay so now that we've got our nice land generating functions, I'm sure that you'll want to actually see it in action. So here is our drawing code...

So now we have our randomly generated dirt to place tanks on and plow shells into. In the next part I'll be showing you how to place our tanks randomly about the land we've generated and do so in a smart manner.

Until then, play with the code a little bit and see what else you can do with it. There is lots of room for innovation here.