Guys I have successfully implemented my first basic level Allegro5 c++ game.
It has a player that keeps moving continuously in right direction. From the right edge of the screen player faces obstacles like triangles and squares. These obstacles come alive at right edge of screen and die at left edge of screen.
I randomized the occurrence of both square and triangle individually.
player jumps over them, if hits triangle loses life, if falls on squares rolls over it.
But then, I thought a lot about how to make them randomly appear in combinations.
For example: If suppose
X represents triangle
O represents squares
and . (Fullstop) represents empty space.
.........x.....xo.....oxxo......
......oo.....x....oxx......xo...
and similarly they should appear in many randomized patterns.
But I after having tried a lot, I still am empty-handed (No solution).
Apart from rendering I have used below code:
[Note: once in a while, like rarely, triangle and square DO overlap.Dunno how!]
I request you all to guide me until I solve the problem.
{"name":"Capture.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea61732842592cbc05dfedb8fccbeca9.jpg","w":1279,"h":524,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea61732842592cbc05dfedb8fccbeca9"}
I have only skimmed the code briefly, but what exactly is not working?
The randomeness? Tri and Squares not actually appearing? Did you seed (with srand() ) the random function?
if(rand()%200 == 1)
That means if a random number between 0 and 199 is 1 !!! Not much of a range!!!
If I understand you correctly, you want to generate something like
.........x.....xo.....oxxo......
but your code only generates something like
...x...o...o...x...x...o......x
sometimes with a triangle and a square sharing the same coordinate?
First off: They are ovelapping, because you're checking if a square or triangle should be generated individually instead of checking if an obstacle is to be generated in general and then deciding, whether the obstacle should be a square or a triange. In other words: If the first rand()%200 generates a zero, you create a triangle, but the second rand()%200 can generate a 1 right after the zero, in which case you create a new square at the same coordinates as the riangle.
If you want to avoid overlapping, rewrite your code to
If you want your code to genarate a random group of obstacles like
..xooxoxxoxxxo..
you could use do something like this:
This code would generate a line from 2 to 17 obstacles off screen.Take note that the code also eventually will generate overlapping object in the line, as it doesn't check if there already is an object at 800 + Cameraposition[0]
Never mind, apparently you're checking for overlaps already, though I don't understand what your logic is behind that. What exactly are the square and triangle object and how and where are they defined?
What exactly are the square and triangle object and how and where are they defined?
That is mainly why I asked what exactly does not work.
Besides not entirely grasping this logic (due to too little code shown I guess?) to check overlapping, the rest seems to be plausible to generate a random pattern of sqaure+triangles.
When you add an obstacle (or series), you should compute the width of what you've created. Don't create anything more until the screen has scrolled by this width + the minimum distance you want between two series.
I would suggest you to create some simple patterns, like: {x} {xo} {xoox} {oxxo} {oxx}... And some variations(random maybe) then you proceed to randomize that. I think your map will be a little more organized.
LUPUSS caught my problem. And KILLOCAN what you said is perfectly what i want to do.
I am going back on computer to bang my head against code wall. Lets see if i make it to the other side of problem. But KILLOCAN how do i make those combinations , because to render my gameobjects i used a list. So what you suggested is all blurry. But I will give it many tries too.
UPDATED:------------------------------------------------------------
Guys look I tried this thing to get the combination of obstacles like KILLOCAN said, using what Lupuss suggested.
Now also i am facing a problem.:-[
I think there is some conflict between creation of triangles and that of squares. No triangle overlaps other triangle. No Square overlaps other square. But either triangle overlap a square which is already drawn, OR a square overlaps an already drawn triangle. Can you see any error in my triangle/square creation code?
When they overlap, somehow both get drawn on same place such that their x & y coordinates (or Their centre points) are same.
*(also thanks to Dizzy for making my rand()% range smaller )
I think there is some conflict between creation of triangles and that of squares. No triangle overlaps other triangle. No Square overlaps other square. But either triangle overlap a square which is already drawn, OR a square overlaps an already drawn triangle. Can you see any error in my triangle/square creation code?
When they overlap, somehow both get drawn on same place such that their x & y coordinates (or Their centre points) are same.
What exactly are the square and triangle object and how and where are they defined?
I think you should getrid of
if(!(triangle->GetX() - 15 < t->GetX() + 15 && triangle->GetX() + 15 > t->GetX() - 15) && !(square->GetX() - 40 < t->GetX() + 15 && square->GetX() + 40 > t->GetX() - 15) ) continue;
altoether and do what Audric said
When you add an obstacle (or series), you should compute the width of what you've created. Don't create anything more until the screen has scrolled by this width + the minimum distance you want between two series.
P.S.:
Guys look I tried this thing to get the combination of obstacles like KILLOCAN said, using what Lupuss suggested.
Sorry, but I recognize neither his, nor my approach...
Lupuss if you didnt notice I tried to club the idea of creating a combination of random size (Between 1-4) by improvising on my previous code where you implemented rand()%2 ==0 inside rand()%200==0 ... I just implemented those rand() statements seperately, one in void createObstacles() and another inside the main function where void createobstacles() is being called.
Dizzy, haha...#oops Thanks to you too.
But hey guyz, hello! I am still facing the issue. I am still working on it,meanwhile if you get any idea , I'll be waiting.
You need to implement code for each combination of obstacle you want to do.
Or make an array of rand()%3 (empty / triangle / square) to determine what is coming up next and then add those objects to your game.
Edgar, Thanx man...
I was making things complex for no reason.
Classic methods always work :Lesson learned
Thanx everyone for looking into my confusing codes
UPDATE: Now where did this 'give credits section' disappear along with that 'Problem Solved' tab.
Actually I would go with the second method. Use an array / list / vector to store a sequence of randomly generated objects with possible empty spaces between them. The first method means largely writing more code than you should, since its easier to work on a whole array instead of all the specific use cases.
The 'Give Credits' option is only available if you make the new thread classification 'a question with a specific answer'.
Edgar, I am newbie. I dont have exposure with things. The first method was easy to go for since I have been doing it in ever since i started coding.
But second method, I never used an array to randomize things. And, if i have to store sequences in array how does it decrease the amount of code that i have to right.
I would like to learn this method too. If you have a little time, can you give me a quick brief about second method?
What i suggest can be expressed(in pseudo correct code hehehe) as something like this:
You can make it an std::vector(if you like) (instead of array) and load it with random shapes during the stage setup, then use it like i did.
First , determine a random size for the number of obstacles in this object.
Then determine which obstacles is which - ie which is a triangle / square / empty space.
For each obstacle, push it back onto the vector of objects.