# eating food function in snake game

Page 1 of 1

## 2 Replies - 5525 Views - Last Post: 27 November 2008 - 06:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=73622&amp;s=60eb28cfbe3c8ec62b17413085c70ad5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 raulin

Reputation: 0
• Posts: 2
• Joined: 26-November 08

# eating food function in snake game

Posted 26 November 2008 - 09:00 AM

[font=Trebuchet Ms][size=3]
Hye there..I do the snake game for my c++ project course, using Quincy
I employ the use of vectors as the main functions in moving the snake. I already make the snake head to move around. My problem now is to set the random number function for the food. I already did something but it seems that it doesnt work out. here is the piece of codings for this particular function...
```
Fl_Box* box_array[50][50]; //array values correspond to the 2-D array for the snake(i use the FLTK application
to represent the snake)
Fl_Box* food_array[50][50];
vector<int> food_x;
vector<int> food_y;
void move_food(Fl_Widget* w, void* v)
{
//setting the food at a fix position of (300,50) initially
food_x.insert ( food_x.begin(), 300);
food_y.insert ( food_y.begin(), 50);

srand(time(0));
for  (int i=0; i < 20; i++)  //count set as 20 corresponding to the 20 number of times in which the food would change the position
{
food_array[ food_x[i] ][ food_y[i] ]->box(FL_NO_BOX);
food_array[ food_x[i] ][ food_y[i] ]->image(chickenimg); //the image i attach in the box
int fx = rand() % 50;  //setting the chicken to move randomnly in the game area
int fy = rand() % 50;
}
food_array[ food_x[i] ][ food_y[i] ]->position(fx, fy);
snake(); //function of the snake movement which is already successfully compiled
gamewindow->redraw();
}

```

When I compiled this program with the rest of other headers and functions, it says that the compiler do not recognize this:
food_array[ food_x[i] ][ food_y[i] ]->position(fx, fy);
also, it seems that this random function for the food position didnt work at all.
I learnt a lot about this random function, but still i cant get this method work. all i want is to only move the food randomly, so that I can implement the food function quickly..i really need help on this..plzzz guys help me!

Is This A Good Question/Topic? 0

## Replies To: eating food function in snake game

### #2 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

## Re: eating food function in snake game

Posted 26 November 2008 - 01:32 PM

First of all, do you realize that the line in question is not actually in your for loop? You closed off the for loop prior to the line that is erroring out. So it has no idea what "i" is for your arrays etc. Plus it won't know what fx and fy is because you defined those inside your loop and they only have local scope to the for loop.

So my guess is that you need to move it into your for loop as a first step. See if that does anything for you.

### #3 raulin

Reputation: 0
• Posts: 2
• Joined: 26-November 08

## Re: eating food function in snake game

Posted 27 November 2008 - 06:36 AM

Martyr2, on 26 Nov, 2008 - 12:32 PM, said:

First of all, do you realize that the line in question is not actually in your for loop? You closed off the for loop prior to the line that is erroring out. So it has no idea what "i" is for your arrays etc. Plus it won't know what fx and fy is because you defined those inside your loop and they only have local scope to the for loop.

So my guess is that you need to move it into your for loop as a first step. See if that does anything for you.

[size=3]Hi Martyr2..
I did as what u said, yeh brilliant its compiled, but now another problem arose.
My main window is appeared, but when I try to click the New Game button, (corresponding to the New game window),
it says these:
Terminate called after throwing an instance of 'std::out_of_range'
what < >:vector::_M_range_check
This application has requested the Runtime to terminate it in an unsual way.

What's all these? Ohh it seems weird...Pls help me how to overcome this...thanks in advance...
I showed u the alteration of codings i made for the food function.
```void move_food(Fl_Widget* w, void* v)
{
//setting the food at a fix position of (300,50) initially
food_x.insert ( food_x.begin(), 300);
food_y.insert ( food_y.begin(), 50);

srand(time(0));
for  (int i=0; i < 20; i++)  //count set as 20 corresponding to the 20 number of times in which the food would
change the position
{
food_array[ food_x[i] ][ food_y[i] ]->box(FL_NO_BOX);
food_array[ food_x[i] ][ food_y[i] ]->image(chickenimg); //the image i attach in the box
int fx = rand() % 50;  //setting the chicken to move randomnly in the game area
int fy = rand() % 50;
food_array[ food_x[i] ][ food_y[i] ]->position(fx, fy);
}

snake();	//function of the snake movement which is already successfully compiled before
gamewindow->redraw();
}[/size]

```