Jump to content
  • Advertisement
Sign in to follow this  
AhmedCoeia

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!