Hey everyone
I am making a Snake Game in C# with XNA.
I've gotten to the point where I have a snakehead that can collect apples and thereby getting points.
Now I need to add new parts to the snake, but I have no clue how I should do this.
Can you give me a hint?
XNA Snake Game - help
Started by Thomas Morgan, Mar 26 2010 03:37 AM
6 replies to this topic
#1
Posted 26 March 2010 - 03:37 AM
|
|
|
#2
Posted 27 March 2010 - 04:35 PM
I'm about to work on a snake remake myself and what i'm going to do is just increase the scale of the snake to make it appear larger after every apple
#3
Posted 28 March 2010 - 08:40 AM
Yeah, that's a good idea, but then how would you make it bend?
And also, I'm planning on making som better graphics for the game so that solution wouldn't really be suitable for my game, thanks anyway :)
And also, I'm planning on making som better graphics for the game so that solution wouldn't really be suitable for my game, thanks anyway :)
#4
Posted 28 March 2010 - 03:30 PM
Without going into too much detail...
You could create an array that stores the position of each segment. That way, when the snake moves forward, you just have to set the position of each segment to positionArray[previousPosition+1]. Then changing direction could just change the position of the head and all the other segments would automatically follow suit.
I that makes sense lol.
You could create an array that stores the position of each segment. That way, when the snake moves forward, you just have to set the position of each segment to positionArray[previousPosition+1]. Then changing direction could just change the position of the head and all the other segments would automatically follow suit.
I that makes sense lol.
#5
Posted 29 March 2010 - 10:31 AM
Yeah, I guess I'm gonna do it like that, thanks.
I just need to set a length of the array which annoys me a little.
I just need to set a length of the array which annoys me a little.
#6
Posted 29 March 2010 - 02:53 PM
Use the List class. It's in the 'System.Collections.Generic' namespace.
if you use a list, assuming that the item at index 0 is the segment closest to the head of the snake, you can just use the 'Add' method of List everytime a new segment appears. 'Add' appends an element to the end of the List.
This way, you don't have to dynamically reassign an array to a new size every time you add a segment.
On the other hand, if you're dead-set on using arrays, just make sure you initially allocate an array large enough to hold the maximum number of segments and then keep an int nearby to store the current size of the array. Be careful of OutOfBounds type exceptions though.
if you use a list, assuming that the item at index 0 is the segment closest to the head of the snake, you can just use the 'Add' method of List everytime a new segment appears. 'Add' appends an element to the end of the List.
This way, you don't have to dynamically reassign an array to a new size every time you add a segment.
On the other hand, if you're dead-set on using arrays, just make sure you initially allocate an array large enough to hold the maximum number of segments and then keep an int nearby to store the current size of the array. Be careful of OutOfBounds type exceptions though.
#7
Posted 31 March 2010 - 04:22 AM
Alright, thanks. The list is awesome, and I have made it add new parts now, but I need to make a list for each direction of the parts, too, and some "turnpoints" where the direction changes, but how do I refer to an element in the list by number? Fx if I got a for-loop with an int i and I want the list[i] to change to the new direction.
Nvm, that was too simple... D'oh.. I think I've been sitting with this for too long ;)
Okay, so I got it moving now, and the parts are added, but the turnpoints are not removed when the last part hits it, and the parts do not hit the turnpoints if the speed is increased. Any ideas? The Snakepart positions must be at the exact same position as the turnpoint, and if then the snakepart jumps over it it doesn't turn.
Got it all fixed now.
Nvm, that was too simple... D'oh.. I think I've been sitting with this for too long ;)
Okay, so I got it moving now, and the parts are added, but the turnpoints are not removed when the last part hits it, and the parts do not hit the turnpoints if the speed is increased. Any ideas? The Snakepart positions must be at the exact same position as the turnpoint, and if then the snakepart jumps over it it doesn't turn.
Got it all fixed now.
Edited by Thomas Morgan, 02 April 2010 - 02:32 PM.


Sign In
Create Account


Back to top









