Hi there,
I am currently doing the snake game for my c++ project.
For your information, I am not majoring in computing or any programming course, but am majoring in Physics at Univ of Birmingham, UK.
About my task, I employ the use of vectors as the main function to make my snake moves. For the design, I employ the FLTK application which I've just learned about a month ago.
I already did the keypad directions for up,down,left and right
for the movement of the snake (by means of an array of [50,50]
I already did the codings regarding the tail (using pop back, push back, insert functions).
My problem now is regarding the food.
How to set up a random position function of the food
in my game window. I don't have any clue on how to link the random function to the box I chose to be the food (it contains a JPEG image of a chicken, mybox in my case)
This is the coding for this particular function I roughly did in the aim of moving the food at different position at different times in the game window. For your information, I do this codings in Quincy 2005 as this is the default IDE learnt in Univ.
For this particular function, I also include:

#include <ctime> and all other headers essentially needed in running the whole program.

void rand_food(Fl_Widget* w, void* v) //setting the chicken to move randomnly at different times (but i don't add timeout yet)
{
int m, n; //m and n simply the integer for the m value (x axes) and n value (y axes) of the food
int c = 0;
srand(time(0));
while (c < 20) //number of loops
{
m = rand()/50; //as random number normally big,I just divide it to make it smaller number, am not sure it is true or not
n = rand()/80;
mybox->position(m, n); //new position of the food at (m,n) axes
c++; //running the loops
}
mybox->redraw();
gamewindow->redraw();
}
// the end of this particular function

I know this little codings are indeed messy and needs a lot of alteration as it has a lot of errors when i compile the whole program.
Therefore, I am glad to ask your help or any suggestion on how to improve this particular function in setting up the snake food at random positions, also at different times as this involved standard c++ and FLTK.

Thank you so much for your help.

God bless you guys. Thanks again

m = rand()/50; //as random number normally big,I just divide it to make it smaller number, am not sure it is true or not
n = rand()/80;

You're wrong :)

If you want a number in the range 0-49 (an array of 50) you should use something like this:

m = rand() % 50;
n = rand() % 80;

That way you're sure that you'll stay within the arrays bounds.

Next time when posting code, please use tags:

[code=cplusplus]
your code goes here
[/code]

That way you'll have code-formatting and syntax highlighting.

Also you should really use meaningful variable names. If you ever make a large program and only use names such as m,n,x,i,p etc, it will become a chaos in a very short while :)

Thanks niek for your help. I've learnt the random number function tutorial in this forum, and altered my code a bit.

void rand_food(Fl_Widget* w, void* v) //function for the food
//it's position changes randomnly every minute
{
int foodx; //x-axes for food
int foody; //y-axes for food
srand((unsigned)time(0));
for  (int count=0; count < 20 ; count++)
{
	foodx = rand() % 50;
	foody = rand() % 70;
	cout << foodx << foody << endl;
}
food->position(foodx, foody); //food is the name of box, which contained a JPEG image of a chicken
Fl::repeat_timeout(1.0, rand_food);
food->redraw();
gamewindow->redraw();
}

However, it seems that the timeout added doesnt do anything (I already add the timeout below the main functions for the snake to move)
Also, the box i set for the food also gone, but weirdly, the whole program compiled and my snake indeed moved around.
Just that the food is missing after I included this piece of function regarding the food.
I really need help on how actually to set up a food, just locate it at positions on the game area, change in a random positions at say, 1 minute? just like the normal snake game...? i don't think i should set up another array for the food since it will not move like the snakes..just it changes the positions in times..but i also not very sure about this..uhh...:(
Many Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.