Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making a snake game and am having trouble to make the snake go longer, at the moment when I run the program, I just have the snake head eating apples. But I want the snake head(30x30) to spawn body parts(20x20) under himself every second. After I can do that, the snake head has to draw 4 body parts and the computer must "delete" the oldest body part before a new one spawns. It is a 2d game, and the snake can curve instead of 90 degree turns. I hope you will all understand.

share|improve this question

migrated from stackoverflow.com Jul 31 '12 at 2:28

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 0 down vote accepted

Without knowing the structure of the program I'd suggest making a Queue<BodyPart> to track the current body parts. Each second dequeue the tail or the snake, spawn the part at the head's location and add it to the queue, and then move the head. To draw the snake you just iterate over the queue and draw the body parts it contains.

share|improve this answer
    
Yes I've been thinking of doing that but I don't know how use time to do it every second. And the head never stops moving. –  Joachim Velzel Jul 30 '12 at 1:48
    
I've never used XNA but my understanding is that time is a fundamental part of the XNA programming model. I'd suggest lookingup some XNA tutorials or examples and see what they do. –  Andrew Cooper Jul 30 '12 at 2:21
    
I've had a look at some examples, and my understanding is that GameTime is mainly for your Frames Per Second, so I don't know how this will help me. maybe I could use timespan? –  Joachim Velzel Jul 30 '12 at 2:41
    
See the tutorial at msdn.microsoft.com/en-us/library/…. Looks to me like you can either track GameTime in your Update method and update the queue when a second has elapsed. You should also be able to set Game.TargetElapsedTime to 1 second, which will give you a 1 second update cycle in your game. This won't allow you to update the display more than once a second, though. –  Andrew Cooper Jul 30 '12 at 3:16
    
I'm pretty sure that setting Game.TargetElapsedTime to 1 frame-per-second is a bad idea. You need to implement your own time accumulator based on GameTime.ElapsedGameTime.TotalSeconds (or similar) in the Update method. –  Andrew Russell Jul 31 '12 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.