How To Approach 360 Degree Snake

Posted by Austin Brunkhorst on Game Development See other posts from Game Development or by Austin Brunkhorst
Published on 2012-06-10T20:17:11Z Indexed on 2012/06/10 22:48 UTC
Read the original article Hit count: 487

Filed under:
|
|

I've recently gotten into XNA and must say I love it. As sort of a hello world game I decided to create the classic game "Snake". The 90 degree version was very simple and easy to implement. But as I try to make a version of it that allows 360 degree rotation using left and right arrows, I've come into sort of a problem.

What i'm doing now stems from the 90 degree version: Iterating through each snake body part beginning at the tail, and ending right before the head. This works great when moving every 100 milliseconds. The problem with this is that it makes for a choppy style of gameplay as technically the game progresses at only 6 fps rather than it's potential 60.

I would like to move the snake every game loop. But unfortunately because the snake moves at the rate of it's head's size it goes way too fast. This would mean that the head would need to move at a much smaller increment such as (2, 2) in it's direction rather than what I have now (32, 32). Because I've been working on this game off and on for a couple of weeks while managing school I think that I've been thinking too hard on how to accomplish this. It's probably a simple solution, i'm just not catching it.

Here's some pseudo code for what I've tried based off of what makes sense to me. I can't really think of another way to do it.

for(int i = SnakeLength - 1; i > 0; i--){
    current = SnakePart[i], next = SnakePart[i - 1];

    current.x = next.x - (current.width * cos(next.angle));
    current.y = next.y - (current.height * sin(next.angle));

    current.angle = next.angle;
} 

SnakeHead.x += cos(SnakeAngle) * SnakeSpeed;
SnakeHead.y += sin(SnakeAngle) * SnakeSpeed;

This produces something like this: Code in Action. As you can see each part always stays behind the head and doesn't make a "Trail" effect.

A perfect example of what i'm going for can be found here: Data Worm. Not the viewport rotation but the trailing effect of the triangles.

Thanks for any help!

© Game Development or respective owner

Related posts about xna-4.0

Related posts about game-mechanics