# Programming a game play Essentials

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

## Recommended Posts

Thanks norman. I have done what you wrote, the problem that I can not put the offsets between the nodes of the snake. I tried for 4 days and I couldn't :(

##### Share on other sites

The problem is that these games are assuming you are moving by clear obvious jumps every step. You have velocity and are moving smoothly around. Taking what I did last time, I think this should do what you need, but the movement might be slightly erratic since I haven't tested it.

#include <math.h>

double GetDistance( double x1, double y1, double x2, double y2 )
{
double A, B, C;

//A is the y length
A = fabs( y1 - y2 );

//B is the x length
B = fabs( x1 - x2 );

//A^2 + B^2 = C^2; C = sqrt( A^2 + B^2 ); C = distance between two points( yay right triangles ); Pythagoreans Theorem
C = sqrt( ( A * A ) + ( B * B ) );

return C;
}

void YourGameLoop()
{
//Offset of segments in pixels
double dOffset = 8;

for ( int i = 1 ; i < nodes.size(); i++ )
{
if( i == 0 )
{
// Do calculations for new position

//Set position and previous position
nodes[i].m_PrevPos.x = nodes[i].m_Pos.x;
nodes[i].m_PrevPos.y = nodes[i].m_Pos.y;
nodes[i].m_Pos.x = newX;
nodes[i].m_Pos.y = newX;
}
else
{
if( GetDistance( nodes[i-1].m_Pos.x, nodes[i-1].m_Pos.y, nodes[i].m_Pos.x, nodes[i].m_Pos.y ) >= dOffset  )
{
nodes[i].m_PrevPos.x = nodes[i-1].m_Pos.x;
nodes[i].m_PrevPos.y = nodes[i-1].m_Pos.y;
nodes[i].m_Pos.x = nodes[i-1].m_PrevPos.x;
nodes[i].m_Pos.y = nodes[i-1].m_PrevPos.y;
}
}
}
}


I probably screwed up somewhere, but this should be a step in the right direction!

##### Share on other sites

The movement was jirky, and the loop starts with i =1, so I think that is a mistake ?

Furthermore, you're updating the previous nodes, when they reach a min-distance, is that correct ? but that will let the movement of the snake body's crawling..

I'm thinking about moving all the snake segments with their own velocity regardless head's position, and when the head's velocity changes, all of the other nodes change their velocity too ?

##### Share on other sites

any ideas guys ? I'm still stuck :(

##### Share on other sites

You wanna throw your code up online somewhere? Or at least some code fragments related to the movement?

##### Share on other sites

@J-locke,

The code fragment is the same that PandDragonThing wrote. I didn't change anything.

My code also just copies previous node postitions one by one. I still can not put an offset "WHILE" copying the previous node positions.  How that can be done actually ?

We are copying the previous node positions like :for (i=9; i>=1; i--) snake[i]=snake[i-1];  it can not be possible to the set offset here for sure ?

##### Share on other sites

We are copying the previous node positions like :for (i=9; i>=1; i--) snake=snake[i-1];  it can not be possible to the set offset here for sure ?

The problem is that these games are assuming you are moving by clear obvious jumps every step. You have velocity and are moving smoothly around.

if you're not using tile based movement, all bets are off!

to do it with smooth movement, you'll need to do the cloth / rope modeling thing.

##### Share on other sites

why there is a need for tile based movement ?

is it ok to just move the snake body movements and when the heads colloids, all other snake body segments change the velocity too ?

##### Share on other sites

why there is a need for tile based movement ?

is it ok to just move the snake body movements and when the heads colloids, all other snake body segments change the velocity too ?

Some source code of Snake uses tile based or array-based implementation for collision. You can use a bounding rectangle on the head and use an interesect method of the rectangle class if the programming language supports it. You can do this in Java.

You mean if the snake eats an apple and update the speed of the snake? Sure you can do that. It is up to you. This idea is the rule of the game. It does make challenging gameplay.

• 12
• 13
• 9
• 12
• 15