|
|||||||||||||||||||||||||||||||||||||||||
| ISBN: 3897214482 ISBN: 3897214482 ISBN: 3897214482 ISBN: 3897214482 | |||||||||||||||||||||||||||||||||||||||||
|
|
Wir empfehlen: | ||||||||||||||||||||||||||||||||||||||||
LandscapegeneratorIn 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. The problemWe 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: Idea and outline of the algorithmTo 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.
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:
The "final" algorithm
{
plus = 1; // initialize variable factor which decides if + or - value of plus faktor = 1; // Initializing start value of the surface start = Math.abs(300 + (rnd.nextInt() % 50)); // Store start value on the first position in the array map [0] = start; // Initializing start values for the colors int greenvalue = 200; int redvalue = Math.abs(rnd.nextInt() % 200); int bluevalue = Math.abs(rnd.nextInt() % 201); // Storing first RGB value in the Color array colors [0] = new Color (redvalue, greenvalue, bluevalue); // Loop to initialize all array positions for (int i = 1; i < mapsize; i ++) {
last = map [i - 1]; // Decision if changing direction or not change = Math.abs(rnd.nextInt() % 10); // changing direction and possibly plus if (change > 8) {
faktor = - (faktor); // new plus (value 1 or 2) plus = 1 + Math.abs(rnd.nextInt() % 2); /* Make sure that surface values stay in a certain range */ if (last > 350 || last < 120) {
faktor = - (faktor); // Make sure that color values stay in a certain range if (greenvalue > 240) {
greenvalue -= 10; else if (greenvalue < 100) {
greenvalue += 10; // Calculate and store surface value on position i map [i] = last + (faktor * plus); // Calculate and store color value on position i greenvalue = greenvalue + (-faktor * plus); colors [i] = new Color (redvalue, greenvalue, bluevalue);
Take a look at the applet Next chapterA Leveleditor in Java |
|
||||||||||||||||||||||||||||||||||||||||
|
Back to the topic sites: StudyPaper.com/Startseite/Computer/Informatik/Programmieren/Java StudyPaper.com/Startseite/Computer/Spiele StudyPaper.com/Startseite/Computer External Links to this site are permitted without prior consent. | |||||||||||||||||||||||||||||||||||||||||
| Home | deutsch | Set bookmark | Send a friend a link | Copyright © | Impressum | |||||||||||||||||||||||||||||||||||||||||