Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!


I was trying to create a snake program in c++ (i'm using devC++ environment). I had already initialized the snake. But I have no idea as to how to move the snake. I'm using the graphics.h header from borland...

I was wondering if you could help me out on the snake movement algorithm??? thanks!

[Edit]Code from OP, removed from comment.

Thanks!for ur interest in helping out :) i've created this trial class and have this snippet...
C++
void snake::createSnake() 
{ 
  while (cnt==0){ 
    for (i;i<=800;i++){ 
      cnt++; 
      rectangle(10+i,300,i,310); 
      rectangle(20+i,300,10+i,310); 
      rectangle(30+i,300,20+i,310); 
      rectangle(40+i,300,30+i,310); 
      rectangle(50+i,300,40+i,310); 
      rectangle(60+i,300,50+i,310); 
      rectangle(70+i,300,60+i,310); 
      Sleep(10); 
      cleardevice(); 
    } 
  } 
} 

i'm trying to manipulate this code snippet...this is just a trial and not the final codes yet. i just want to try it out if i could move the rectangles. thanks... i'm really starting from scratch... and it isn't easy... lol... but thanks a lot!
[/Edit]
Posted
Updated 23-Aug-11 20:35pm
v2
Comments
walterhevedeich 24-Aug-11 2:16am    
Post the relevant code.

 
Share this answer
 
v2
Comments
walterhevedeich 24-Aug-11 2:14am    
Comment from OP: Deleted from answers:

wow! thanks! nice suggestion... but anyone in here who could help me out on my codes? someone i can talk to? thanks a bunch! Smile | :) )
Some issues:

1. I don't see any variable declaration for either cnt or i (or anything else). Even though they may be declared outside, somewhere, that is bad design. C++ is all about encapsulation, and that means you should declare variables as close to the scope they're used in as possible. Put the declarations right in your Create function.

2. Neither cnt nor i were initialized - it is not clear what values they're starting at and therefore it cannot be determined how your Create function will behave. Always initialize your variables, preferably right at the point of declaration, and possibly (again) right before they're actually used. Initialization is cheap. Forgetting initialization and spending hours to chase errors is expensive!

3. You're creating your snakes as a series of rectangles (or, in fact, squares), but if your snake game is supposed to be that kind of game I have in mind then the total number of rectangles will vary over time! Hardcoding the drawing as a fixed series of rectangle commands therefore is a bad idea. Instead you should make a loop that is controlled by the current shape (and length) of the snake.

4. The function states 'Create', but it does much more than that as it iterates over i and thus 'moves' the snake over the screen. Do seperate your functions cleanly! Otherwise you're just confusing yourself and create unmaintainable code.

5. You put in a Sleep command right in there, to control the speed of movement! That is a very, very bad idea! Controlling the flow of your game should happen in just one central location and not in every single utility function independently!

6. Even worse is your call to cleardevice! The function states that you 'create' something, but the very last thing is to destroy it again? And not only does it destroy itself, it also destroys anything else that might be visible on the screen! If you want to erase your last drawing of the snake, then erase that and that only, not the entire screen. And make a separate function for it! And don't call it in your 'create' function except maybe right at the beginning to clean up any previously exxisting snake(s). In fact, it isn't even neccessary to erase the entire snake - if you want it to 'move' it's much more efficient to just erase the last bit of the tail and the 'redraw' part then just has to append a new head.

One more thing: your 'create' function shouldn't even draw anything! Drawing your snake is one functionality that should go in a separate function, and it should be able to draw the snake in it's current shape. For that to work you of course need to somehow store the current shape of your snake - and that, exactly is what your 'create' function also should do: store the shape of your snake. And nothing else.
 
Share this answer
 
For the snake you have to store the snakes body position. i.e: every rect the snake occupies. the snake has a head and a trail - to move the snake you can add a new random position around the snakes head and remove the trail position. the old positions (+) new head (-) old trail is the new snake body you have to store.
Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900