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;
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
// 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