M
Monster Ahhhhh
Guest
At http://javaboutique.internet.com/tutorials/Java_Game_Programming/LandscapeEng.html
i have found something i want to use in a game im making but dont get how its
done i have been poking at it for a while and im kinda struggling in arrays. I will show the code changed a little because i don't need most of the color stuff. If someone can breakdown how this works it would be appreciated.
i have found something i want to use in a game im making but dont get how its
done i have been poking at it for a while and im kinda struggling in arrays. I will show the code changed a little because i don't need most of the color stuff. If someone can breakdown how this works it would be appreciated.
Code:
public void generateLandscape ()
{
/* initialize plus, this variable tells you which value will be added or subtracted from the last value. */
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 = 0;
int bluevalue = 0;
// 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 ++)
{
// get the value before the actual position and store it in last
last = map [i - 1];
// Decision if changing direction or not
change = Math.abs(rnd.nextInt() % 10);
// changing direction and possibly plus
if (change > 8)
{
// Ändern der Richtung
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)
{
// Ändern der Richtung
faktor = - (faktor);
}
// 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);
}
}

