SFML community forums
Help => Graphics => Topic started by: foodman on May 30, 2010, 01:56:24 pm
-
I have a tiny question
I am making a snake Game :P
And My snake is ready and moving very good
But now I have A very big question How can I generate apples or dots randomly any where
I tried many things I even tried some old c++ tricks
Like if defined position of a sprite is
Sprite.SetPosition(200.f, 100.f);
And I want to set in any where
So I was tring put
Sprite.SetPosition(A.f, A.f);
and then define A as an int
after that the random function of SFML given in tutorials so that there will be a random number at point of A
Well my logic was a bit right But I saw that , the program of sprite dosnt accept anything like int or anything it only accepts class/struct/union
So now my big question is how to get that random value can anybody please help?
-
sf::Randomizer::Random(lowest, highest)
or
std::rand()%highest;
They return both a random value.
But what about learning a bit more C++ before using SFML?
-
I know C++ 's random Function
But
Sprite.SetPosition(A.f, A.f);
and then define A as an int
But int nor float is not acceptable there so what should I put in SFML
Are you getting what is my question?
-
void sf::Drawable::SetPosition (float X, float Y)
This method uses float numbers.
the ".f" thing is used to say "this is a float" to the compiler.
If you are really using "A.f", this should not work (IHMO). Use A instead :
int randX = sf::Randomizer::Random(lowest, highest);
int randY = sf::Randomizer::Random(lowest, highest);
Sprite.SetPosition(randX, randY);
If all this is nonsense or not answeringto your question, please post the part of your code which is not working.
PS: By the way, using SetPosition(A, A) is a bad idea, all your sprites will appear with the same top and left position, on a diagonal.
-
The ".f" suffix is meant only for number litterals (like 5.3f), using it on a variable name won't convert it to float, it will produce a compiler error.
By the way, sf::Randomizer can give you float numbers directly, not only integers.
-
Thank you Mindiell , Laurent
Well Mindiell It did the work , At least I guess It did.
Well now I am trying How can I destroy The apple once eaten And how to grow the snake
But growing the snake will be on if condition and For loop At least this is my Idea
But I am not aware how to Destroy that apple Once ate
-
But I am not aware how to Destroy that apple Once ate
I'm certain that some websites can help you (http://www.gamedeveloperskills.com/?p=138) (maybe java, but it's the algorithm which is important). At least, use ethicle (http://www.ethicle.com/) ;)
-
Yes thanks But Can u tell me how to destroy a created sprit
Like if Some thing == something
{
destroy spirit
}
Like that something
Is there anything That Will destroy a drawn sprit
-
If I were you, I won't destroy the Sprite. Since an other object is appearing just after the first one is eaten, just change the Image and place it somewhere !
-
OK this is for snake game But what if I want to destroy a particular sprit
Say Like there is villein and player killed him how will he vanish?
We need to destroy it
-
If you create a sprite dynamically:
// Create...
sf::Sprite* sprite = new sf::Sprite();
// Use...
....
// Destroy...
delete sprite;
If you create a sprite statically (on the stack):
sf::Sprite sprite;
it will be destroyed automatically when it goes out of scope. i.e. if sprite is a local variable inside some function, it goes out of scope and dies when the function returns.
-
Or you can use bool to hide/show the sprite which doesn't require alloc/dealloc "all the time" (maybe it's negligible here but beginners should not use new/delete IMO).
PS : I consider myself still as a noob even after more than 4 years practicing C++. C++ is soo biiig!)
-
Can I get Co-ordinates of my snake when moved
Is there any function or something in SFML
Like If Co-ordinates of snake == Co-ordinates of apple
{
apple.move(randx,randy)
score++
}
Is my basic Idea So can we get actuall snake Co-ordinates and give it to a letter?
like
co=Co-ordinates of snake
how to get that
-
Well, first it depends on how you implement your world (discrete or not, ...).
You have to think about it before coding anything. Develop how you want your world to be ("paper & pen" part of the application development).
Hint : how do you manage the snake position ? (answer this question to yourself, if you're not satisfied with your conception start from beginning of the conception of your application.)
PS : SFML don't provides everything to build games (or more generally any application). It's a low level library. Thus you have to do mostly everything yourself.