Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

snake algorithm

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
Many of you have probably seen the game "Nibbles" or "RattlerRace". The idea is that a snake moves around the screen eating numbers or apples (respectively) and getting longer with each one- if the snake gets all the apples without (a) running into a wall or (b) running into itself or (c) another snake (2 player in Nibbles or the enemy snake in RattlerRace), it advances to the next round, which either is faster or has more walls to run into, or both depending on the way it was programmed.

I'm rewriting this in Java, but having a hard time figuring out what the algorithm to draw the snake would be. Say if the snake were going left:


<-------

and then turned up, it would look like this:

^
|
-----

if it turned left again, it would look like this:

<--
|
---

I need to handle these turns in Java. Right now, I just have a rectangle that moves up and down, left and right. However, a snake could be the combination of many rectangles (I hope this makes sense)... so it would have to be a recursive algorithm. I was also thinking of storing the values in a Vector, but that wouldn't be very efficient.

Any ideas? Thanks all... [sig]<p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."[/sig]
 
You 've to conceptually split the snake into slices...


Say the snake has 12 slices

Goes west ... at pt (x,y) user presses upward arrow

erase the 4 slices at the tail..
start piling slices towards North..
continue loop

You need to have the following attributes in Snake class

- snakeLength // increases with time if I am right

- numSegments
// say the user pressed up & left arrows at an
// instant where len of snake is X no of slices
// the numSegments will be three (ref u'r fig)

// You need not put the screen loc of each slice
// in a vector ...

// Imagine.. the S takes a loop (down n right wrt
// screen - now travelling in HORIZONTAL)
// then you 've to check for the boundary
// conditions in the VERTICAL direction

// if reaches BOUNDARY the snake dies
// Boundary can be any sanake body(if >1 snake)
// or any wall

Now if we analyse there are two boundaries

// HORIZONTAL and VERTICAL

// you can make 2 Vectors each for H & V
// they will already have wall coordinates

// If any snake changes direction ... it creates
// a H or V boundary (dynamiclly) which should
// be entered to the corresp vector(dynamically)

// if a snake moves in Horizontal(left)direction
// then you've to check for boundary reach
// condition in Vertical-Vector....

// that tooo... only those vertical boundaries
// that lies left to the head of the snake-head

.... am I confusing ??? .... I'll catchup later
:) [sig]<a href=mailto:pondyprem@yahoo.com>pondyprem@yahoo.com</a><br>[/sig]
 
I would say, split the snake up into &quot;square&quot; segments, with each segment occupying a coordinate on the map. When the snake moves, all you really need to do is add a new &quot;head&quot; segment 1 coordinate north, south, east, or west of the current &quot;head&quot; and remove the tail segment. The snake would logically be a queue, but perhaps a Vector would be an adequate Java class to use?

If you are implementing a graphically pretty version, you'll obviously need to do more than what's described above, such as drawing a body segment where the old head was and drawing a tail graphic where the new tail is (which used to be a body segment). And to give the impression the whole snake is moving, perhaps have two body versions of each body segment graphic (assuming you'll need an east-west and north-south straight images and north-east, north-west, south-east, and south-west turning images) since snakes tend to have repeating patterns, you could then just toggle the images on each segment to give the illusion of motion.

I guess I got off on a tangent and am not sure I answered your question. Hope some of that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top