We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › New and in need of some help.
Page Index Toggle Pages: 1
New and in need of some help. (Read 728 times)
New and in need of some help.
Mar 23rd, 2009, 7:49pm
 
First off, I would like to say Hi to everyone here and look forward to working/collaborating with everyone in the future.  Anyways, I am rather new to the processing language, but so far have been making some good progress with my work and am enjoying it thoroughly.  Lately I have been working on creating various games, and so far have made some basic ones like pong, a simple space invader, etc..., but am stuck on my Snake game.  I figure once I get the basics down I can move onto more complex games.  Anyways, at the moment I have a running Snake game that has the initial snake and it moves and depending on if you push d(down), u(up), l(left),r(right), it will move that direction.  However I am stuck in the whole make a random piece of food on the map, and how, once that piece of food is eaten, it adds one more ellipse to the very back of the snake.  I will include the code I have so far, and if someone could show me how I could accomplish what I am having trouble doing it would be greatly appreciated.  My code is as follows...

int len=20;  // length of worm
int sz = 10; // size of worm segment in pixels
int[] x = new int[len];
int[] y = new int[len];
int dx=1;
int dy=0;

void setup() {
 size(500,500);
 for (int i=0; i < len; i++) { x[i] = width/(2*sz)-i; y[i] = height/(2*sz); }
 frameRate(5);
}

void draw() {
 background(100);
 if (key == 'd') { dx = 0; dy = 1;}
 if (key == 'r') { dx = 1; dy = 0;}
 if (key == 'u') { dx = 0; dy = -1;}
 if (key == 'l') { dx = -1; dy = 0; }

 for (int i=0; i < len; i++) ellipse(sz*x[i],sz*y[i],sz,sz);
 for (int i=len-1; i > 0; i--) { x[i] = x[i-1]; y[i] = y[i-1]; }
 x[0] = x[1] + dx;
 y[0] = y[1] + dy;
 
}

//void make_food(){
//  
//  fill(255);
//  rect(random(501),random(501),10,10);
}
 

I believe that this is good code and would like to not have to change what I currently have too greatly because I understand it well, but am open to new methods to accomplish what I am trying to achieve.  That last part is unneccesary and was just me messing around with random placement.  If this is the wrong section to put this in or not how this forum works I apologize.
Re: New and in need of some help.
Reply #1 - Mar 23rd, 2009, 9:06pm
 
>  believe that this is good code and would like to not have to change what I currently have too greatly because I understand it well, but am open to new methods to accomplish what I am trying to achieve.

While I totally understand that sentiment, it's not a good sentiment to hold, generally, when working with code.  I think that to be a successful programmer, you must always be open to the idea that you may need to scrap what you've got and start over.

Your issue with the food is probably best dealt with in the following manner:

* store the food explicitly within the code.  If you only ever want to have one piece of food, you can just make global "foodX" and "foodY" variables in your program to hold the present location of the food.  Right now your makeFood() function just draws the food and then forgets about it.  Also keep a global "foodPresent" boolean flag that says whether or not the food is on the screen.  You'll set it to true when you create the food, and false when the snake eats it.
* when the snake moves, check to see whether the location of the snake's head is the same as the location of the food.  "if(x[0] == foodX && y[0] == foodY)"
* in your draw() function, you'll draw the snake but also draw the food if the foodPresent variable is true.

As far as making the snake grow, your issue right now is that you're tracking the segments of the snake with a pair of correlated int arrays.  Arrays are great for lots of things, but have some down-sides for applications like this: once the array is full, you can't stuff any more items in there.  Also, you need to explicitly track how many of the items in the array you're actually using.

Much better is the ArrayList type, which can be accessed by numeric index just like an array (although in Java's implementation, you say "list.get(index)" rather than "list[index]"), but which also tracks the length for you and takes care of all the nasty details of making the array longer if you ever need it.  You just use the add() method to add new things to the end of the list.

Other suggestions: don't put the keypress checking in the draw() routine.  Use the keyboard event handlers (keyPressed(), keyReleased(), keyTyped()) instead.  Within those, you'll do whatever processing you need to do to determine what direction the snake is going.  Store that direction in another global variable so your code more clearly expresses what you're trying to do: one part of the code figures out the direction, another part of the code uses that direction to do something with the snake.
Re: New and in need of some help.
Reply #2 - Mar 24th, 2009, 4:59am
 
Thank you cloister that was helpful and allowed me to progress a little further.  Everything you said made sense, but Im still having trouble getting the the food to dissapear when eaten and add onto the back of the snake.  Do not know why this is giving me so much trouble, but am getting frustrated staring at this.  What should my make_food, and foodPresent look like?  I have fooled around with quite a few things, and they make the food, but it will not dissapear or add onto the snake.  Again sorry if these are basic and previously covered, I just know I wont feel good with myself till I figure out how it is done, maybe an OCD thing Smiley.
Re: New and in need of some help.
Reply #3 - Mar 24th, 2009, 5:52pm
 
As I said:

> Also keep a global "foodPresent" boolean flag that says whether or not the food is on the screen.

When the snake eats the food, you grow the snake and set foodPresent = false.  In your draw() routine, you only draw the food if the foodPresent flag is true:

if(foodPresent)
{
 ellipse(foodX, foodY, 3, 3); // or whatever.
}

The basic idea is this: you want to create simple boolean flags that track important pieces of high-level information about your game.  Such as "is there food on the screen right now, or not?"  Then it becomes very easy, anywhere else, to simply check the flags and do the right thing.  This is a general strategy that works well for any program that has more than one "independent" entity it needs to track.  In your case, the independent entities are the snake and the food.  The behavior of the snake is independent of the food.  The snake does its thing, as directed by the player.  The food's behavior (boring though it is), is independent of the snake.  Food appears, and then just sits there.

The snake has its own pieces of high level information associated with it: how long is it?  Where is the snake's head?  What direction is it going?  In these cases, the data isn't a simple boolean yes/no value, but the concept is the same:  Taken all together, they represent the state of the snake at any point in time.  You should track them with appropriate variables, as such.
Page Index Toggle Pages: 1