• 14
• 14
• 9
• 10
• 9

2D sprite game assitance.

This topic is 2488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi,

This is my first post here and I just wanted to get some opinions on what would be best to use in a couple of situations. The game I will be making is snake with a few variations to the original, using windows GDI. Basically I was going to have two difficulty settings, on the easiest I was thinking of having the game screen wrap around so the snake will come out of the opposite side from where it went through. The problem is im not sure how to best implement this, if this will be too difficult I can always scrap it and make it bordered instead, but any opinions, info or tips would be greatly appreciated. The other thing is that I will have a lot of collision detection, mainly circle, circle, but in order too check the snake against the border(for the harder difficulty or both depending) I was thinking I would have to use line, circle intersection. Does that sound right?

Anyway if anyone takes the time to read this and help me out It would be awesome! Thanks in advance!!

Share on other sites
Hidden
For border collision detection you simply need to check if the head of the snake's X coord is greater than {screenwidth} or smaller than 0 or its Y coord is greater than {screenheight} or less than 0.<BR><BR><BR>You should use an array or similar structure to store the location of all the snakes "circles". Let the snake's head be at index 0. Each time the snake moves adjust coords of the snakes head then let the coords of the next "circle" (at index 1) = the snakes old head circle coord.<BR>then let the coords of the next "circle" (at index 2) = the old coord of the circle at index 1.<BR>then let the coords of the next "circle" (at index 3) = the old coord of the circle at index 2.<BR>then let the coords of the next "circle" (at index 4) = the old coord of the circle at index 3.<BR><BR><BR>In other words write a nice loop to handle this. You might need to do the loop in reverse so that you dont overwrite the "old" values before you need them. If you got this working it wont matter if the snake can pass through the borders or not. It will work either way.<BR>

For border colision detection you simply need to check if the head of the snake's X coord is greater than {screenwidth} or smaller than 0 or its Y coord is greater than {screenheight} or less than 0.

You should use an array or similar structure to store the location of all the snakes "circles". Let the snake's head be at index 0. Each time the snake moves adjust coords of the snakes head then let the coords of the next "circle" (at index 1) = the snakes old head circle coord.
then let the coords of the next "circle" (at index 2) = the old coord of the circle at index 1.
then let the coords of the next "circle" (at index 3) = the old coord of the circle at index 2.
then let the coords of the next "circle" (at index 4) = the old coord of the circle at index 3.
etc.

In other words write a nice loop to handle this. You might need to do the loop in reverse so that you dont overwrite the "old" values before you need them. If you got this working it wont matter if the snake can pass through the borders or not. It will work either way.

For detecting a collision between the snake's head and its body you can loop through the array and do a distance check between the head's coords and each body "circle" coords using pythagoras.

Share on other sites

Hi,

This is my first post here and I just wanted to get some opinions on what would be best to use in a couple of situations. The game I will be making is snake with a few variations to the original, using windows GDI. Basically I was going to have two difficulty settings, on the easiest I was thinking of having the game screen wrap around so the snake will come out of the opposite side from where it went through. The problem is im not sure how to best implement this, if this will be too difficult I can always scrap it and make it bordered instead, but any opinions, info or tips would be greatly appreciated. The other thing is that I will have a lot of collision detection, mainly circle, circle, but in order too check the snake against the border(for the harder difficulty or both depending) I was thinking I would have to use line, circle intersection. Does that sound right?

Anyway if anyone takes the time to read this and help me out It would be awesome! Thanks in advance!!

I don't think implementing the "no wall" part would be difficult, but I encourage you to try to find a solution by yourself. Programming is not just having the solution and typing it in code format. More important than that, is learning to solve problems. You should definitivelly try it by yourself, and then if after a few hours of hitting your head against the wall you can't accomplish it, then you could ask or help. but I don't think it will be difficult.

Second, you really don't need to know about lines or circles. For example, if the border is in x=1, then you can simply do something like this;
if (snake.x >= 1) // keep moving else // stop moving to the left

will your snake move around everywhere, or in fixed tiles? if it's going to act like a square, you can almost do the same thing for other blocks/snakes, but if not, think about the circle collision like this:
a circle has a radius of 2, if you want to know if that circle has collided with another circle, you do this
if(distance(snake_position, block_position) < snake_radius + block_radius) //it has collided else // keep moving 
distance would be something like this
float distance(point pos1, point pos2) { float xd = pos2.x - pos1.x; float yd = pos2.y - pos1.y; return sqrt((xd * xd) + (yd * yd)); // a bit of trigonometry here } 

and another thing, why using windows GDI? if you're starting to learn game programming, I suggest XNA with C#. You can download it for free with visual studio C# express 2010 and there is a lot of support out there and tutorials/books.