I'm quite a beginner on Java and have taken to creating a simple game based on what I've read, watched, listened to online. Just a simple game that'll help me cement techniques and syntax whilst going along.
One of the features it has is random (sort of) terrain generation. It's a 2d flying game and what I've done is create an int[][] which draws a line of land. It uses a for loop to cycle through the array and then a random number generator to determine whether the next 'piece' of land is a pixel higher, lower, or on the same level.
int a = lengthOfLand;
int b = startHeightOfLand;
int[][] land = new int[a][500];
land[0][b] = 1;
int y = b;
for(x = 1; x < a; x++){
switch(getRandomNumber(3)){
case 0: land[x][y - 1] = 1;
case 1: land[x][y] = 1;
case 2: land[x][y + 1] = 1;
(I'm sure there are a million better ways for random terrain generation, but I'm just getting to grips with everything and learning new techniques, so this way works for me).
The above is all called in my initializer class. In the rendering area of the game, we take the array and say if the value = 1, draw this image at the x, y values provided. Obviously, all other values in the array = 0, so these aren't rendered. So what I see is a nice line with some ups, downs, flats, all generated (semi-)randomly, and (most importantly) differently, every time I run the game. Basically, I use this multi-dim array to plot every pixel on the screen. The values that = 1 are rendered as the top of the land. Perfect.
However, what I'd like to do is start up a separate array, to carry the land on where this one left off. So I need to be able to draw from the land array, the height at which we end (this is different every time the game is run due to the random element). Basically, I need to find out the y value, where x = a. So searching for and then returning a specific value in a certain 'column' of a multi-dim array.
How can I do this? I assume I can use a method from the Array Class, but as a beginner, I don't really have a clue where to look and what to do with it.
Any/all help massively appreciated. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire