2 Replies - 3726 Views - Last Post: 27 November 2008 - 06:36 AM Rate Topic: -----

#1 raulin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#3 raulin  Icon User is offline

  • New D.I.C Head

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

Re: eating food function in snake game

Posted 27 November 2008 - 06:36 AM

View PostMartyr2, 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..
Thank u so much for your reply.
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.
Please contact the application's support team for more information.

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]


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1