In this chapter I want to teach you the method to create 2D landscapes I used in my game Castles. Again, I won't talk about the Main class, which controls the applet in general and I won't talk about the Stars class, that paints the stars in the background. But both classes are pretty simple, so I think it will be no problem for you to learn them by yourself if you want to. Before we start programing, we have to know what our "problem" looks like.
We want to generate a 2D landscape that looks like the "Rocky Mountains" and can be used in a game like "Castles". This landscape shall look different in every game, so it's not possible to use *.gif's or something like that. We want this landscape to be generated at random. Also we want to have the possibility to change it afterwards for example if "bombs" hit the ground. An other important thing is, that the datastructure of our landscape mustn't be too complicated, so that the game is still running fast and the amount of data is not too big. I'm sure, there are many different solutions to this problem one could think of. Let's talk about mine:
To produce as less data as possible, I want to generate my landscape out of many lines and not out of single points / pixels. The basic idea is to draw a vertical line for every pixel of the length of the landscape. Now we choose the lower point of all lines constant. So we only have to store the upper point, the "surface" of the landscape that will have different values for every line, in a array.
First of all one has to recognize, that it doesn't make sense to choose every upper point at random. If you want to generate a "landscape" this way, you wouldn't get a structured surface, instead your "surface" would look like the picture beside this text.
To generate a real surface is a little bit harder. First of all I will show you the basic concepts of the algorithm, then afterwards, you get the method I used in Castles as a Java program. Remember, that we leave the lower point of every line constant and change / store the upper point only!
Beside this text you can see the result of this pretty simple but pretty effective algorithm and it is realy not bad.
There are just a view small problems. First of all it happens pretty often, that "mountains" are higher and "vallies" are lower than the appelt size. Another "problem" is, that this landscape looks a little bit boring, I think. So I will present you an algorithm that solves these problems and adds some additional features to the landscape like changing colors. Here we go:
If one has initialized all array values that way one can paint the landscape. The easiest way to do this is to get all values of the arrays with a for - loop. Then draw a line from the bottom of the applet (constant value) to the surface array value. The x - coordinate is the array position of the value. Draw the line in the corrosponding color out of the color array. You can see the result of this method besides this text. As I mentioned, there are many different possibilitys to generate a landscape (for example one could calculate and store single points instead of lines) but my algorithm is fast and doesn't generate a too large amount of data. And at least the result is not too bad. Ok, that's it, if you have another good method to generate a landscape (maybe even 3D) write me. Here comes the source code and the applet!