×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Programming Snake

Programming Snake

Programming Snake

(OP)
I can't program a snake to move in a proper manner. Does anyone know the logic behind moving the snake on the screen?

RE: Programming Snake

Can we see some code?

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."

RE: Programming Snake

... or maybe a plan how you would solve the problem?

Designing the program should be done by you; we can help transforming the design to code (although in Pascal, if you're design is entirely correct, it's only a matter of literally translating it).

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
http://student.vub.ac.be/~bvingerh/
Don't worry what people think about you. They're too busy wondering what you think about them.

RE: Programming Snake

Snakes are usually controlled by a bloke sitting crosslegged with a flute rather than using pascal. However, if I'm right in assuming that you mean a virtual snake in the game where a wiggly thing moves around on the (often text-based) screen, and dies if it bumps into itself, then there are various possibilities. e.g.

Moving:
control the head as though it were a pacman or any other similar thing.
maintain a list of "occupied squares"
each time the snake moves, add the new square occupied by the head to one end of the list, and delete the last square of the list (which is the end of the tail).
Depending on graphics you probably only need to draw the tail-square (to blank out where the snake has just been), the head square, and possibly the square behind the head if you have different graphics for the head.

Collisions:
either test the screen to see if the head square is now occupied by snake-tail (this can deal with collisions with walls and other snake, too. Test if head square is empty), or (probably slower, depending on graphics again) check through the snake square list to see if the head coordinates are already present.

RE: Programming Snake

I guess you're using *something* loosely based on this:

REPEAT
  move snake in chosen direction
UNTIL (snake hits a wall) OR (direction is changed)

Yes?

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."

RE: Programming Snake

I tend to do
while not snake_dead do.....
begin
  (delay, preferably based on screen flyback)
  snake moves to snake + snake_increment
  if snake_collides then snake_dead := true
  if keypressed then snake_increment := appropriate_value
end

(pseudocode!!)

RE: Programming Snake

      You can avoid the list of occupied squares if you read back off the screen what's there.  I wrote a snake long ago.  The initial version had a 10,000 element queue for the snake.  At 1024x768 that turned out to be inadequate.  How long would it take to collision-test 10,000 elements?!  I just dug out my old version.  The top entry in the high score table is over 13,000.

RE: Programming Snake

Since you've written the program yourself, there's no way of checking wether a score of 13000 is either high or low .

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
http://student.vub.ac.be/~bvingerh/
Don't worry what people think about you. They're too busy wondering what you think about them.

RE: Programming Snake

In suggesting the "keep a list" approach I didn't assume 1-pixel length graphic-mode snakes. For a text mode I would keep a list of squares because it's short, or look at the screen, cos that's easy too.
In a high-res graphic mode I personally wouldn't read the screen, because it's often very slow to read screen memory compared to real memory. But I wouldn't keep a list of pixels either. I would keep a list of corners in the snake, because it is unlikely it wiggles more than 20 times anyway. Then you look for collisions by seeing if the head lies on a line between two corners. Update the final corner (i.e. tail end) and first corner (head end) each move, and make a new corner every time the player changes direction.

RE: Programming Snake

Bertv100:
     The scoring in Snake is very simple:  It's the length of the snake!  13,000 means there were 13,000 pixels in the snake.

Lionelhill:
     Reading the screen, however slow, is still faster than any sort of search based on the snake.  Besides, your 20 is way off.  It can get folded a *LOT* more than that on a high res screen.

RE: Programming Snake

If your snake is one pixel wide, I agree. Personally I found the text-based version of snake that came with Q-basic very playable, and from a game-play point of view I'd have been inclined to emulate that in prettier graphics. But we're all different (thank goodness!)

RE: Programming Snake

Hmm... 5218 are you even paying attention?

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."

RE: Programming Snake

(OP)
Sorta

RE: Programming Snake

have you found any of this useful?

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."

RE: Programming Snake

(OP)
yup
it helped me a lot
thanks for everything

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close