Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Random function updated to Co-ordinates  (Read 6783 times)

0 Members and 1 Guest are viewing this topic.

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« 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
Code: [Select]
Sprite.SetPosition(200.f, 100.f);

And I want to set in any where

So I was tring put

Code: [Select]
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?

Mr. X

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Random function updated to Co-ordinates
« Reply #1 on: May 30, 2010, 02:07:34 pm »
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?

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« Reply #2 on: May 31, 2010, 02:18:47 pm »
I know C++ 's random Function

But

Code: [Select]
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?

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Random function updated to Co-ordinates
« Reply #3 on: May 31, 2010, 02:48:23 pm »
Quote from: "Documentation"
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 :
Code: [Select]
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.
Mindiell
----

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Random function updated to Co-ordinates
« Reply #4 on: May 31, 2010, 03:01:38 pm »
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.
Laurent Gomila - SFML developer

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« Reply #5 on: May 31, 2010, 04:41:10 pm »
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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Random function updated to Co-ordinates
« Reply #6 on: May 31, 2010, 04:49:42 pm »
Quote from: "foodman"
But I am not aware how to Destroy that apple Once ate
I'm certain that some websites can help you (maybe java, but it's the algorithm which is important). At least, use ethicle ;)
Mindiell
----

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« Reply #7 on: May 31, 2010, 08:41:13 pm »
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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Random function updated to Co-ordinates
« Reply #8 on: May 31, 2010, 08:47:15 pm »
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 !
Mindiell
----

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« Reply #9 on: June 01, 2010, 07:05:35 am »
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

dunce

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Random function updated to Co-ordinates
« Reply #10 on: June 01, 2010, 09:16:37 am »
If you create a sprite dynamically:
Code: [Select]

// Create...
sf::Sprite* sprite = new sf::Sprite();

// Use...
....

// Destroy...
delete sprite;


If you create a sprite statically (on the stack):
Quote
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.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Random function updated to Co-ordinates
« Reply #11 on: June 01, 2010, 09:39:47 am »
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!)
SFML / OS X developer

foodman

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 570393595
    • View Profile
Random function updated to Co-ordinates
« Reply #12 on: June 03, 2010, 01:24:24 pm »
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

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Random function updated to Co-ordinates
« Reply #13 on: June 03, 2010, 03:45:31 pm »
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.
SFML / OS X developer

 

anything