Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:03:23+00:00 2026-05-24T09:03:23+00:00

I’m writing Snake in C++, using OpenGL and GLFW. I’m trying to implement a

  • 0

I’m writing Snake in C++, using OpenGL and GLFW. I’m trying to implement a feature where the game exits, when the snakes head crashes into its body.
Here are the move() and CrashCheck() functions of the Snake class that I wrote.
x_pos is a floating point array that stores the x_coordinate of a segment of the snake body. y_pos does the same with the y_coordinate. length is the number of body segments in the snake, and increases when the snake eats food(not yet implemented). x_speed and y_speed store the speeds of the segments along the axis. The snake can never move along both the axes simultaneously; Also, float MAX_S = 0.00075;. I’ve included my draw() function as well. And Fix_Snake_x and Fix_Snake_y are functions that realign the segments of the snake (because they kept separating and causing havoc with the game). I know it’s a stupid way to correct the problem, so if you can suggest fixes in the move() function, that would helpful.

void draw(float win_aspect)
  {
    for(int a = 0; a < length; a++)
      {
        Fix_Snake_y();
        glBegin(GL_QUADS);
        glColor3f(1.0,0.0,0.0);
        glVertex2f(x_pos[a],y_pos[a]);
        glVertex2f((x_pos[a]+0.05),y_pos[a]);
        glVertex2f((x_pos[a]+0.05),y_pos[a]-0.05);
        glVertex2f(x_pos[a],y_pos[a] - 0.05);
        glEnd();
        Fix_Snake_x();
      }
  } 

void move()
  {
      for(int a = length ; a >= 0; a--)
        {
          if(a > 0)
            {
              if(x_pos[a] >= x_pos[a-1] && x_speed[a] < 0)
                {
                  x_pos[a] += -MAX_S;
                  Fix_Snake_y();
                  Fix_Snake_x();
                  if(x_pos[a] <= x_pos[a - 1])
                  {
                     x_speed [a] = 0;
                     if(y_pos[a] <= y_pos[a-1])
                     {
                          y_speed[a] = MAX_S;
                     }
                     else
                     {
                           y_speed[a] = -MAX_S;
                     }
                   }
                }
              if(x_pos[a] <= x_pos[a-1] && x_speed[a] > 0)
                {
                  x_pos[a] += MAX_S;
                  Fix_Snake_y();
                  Fix_Snake_x();
                  if(x_pos[a] >= x_pos[a - 1])
                    {
                      x_speed [a] = 0;
                      if(y_pos[a] <= y_pos[a-1])
                       {
                          y_speed[a] = MAX_S;
                       }
                      else
                        {
                          y_speed[a] = -MAX_S;
                        }
                    }
                }
              if(y_pos[a] <= y_pos[a-1] && y_speed[a] > 0)
                {
                  y_pos[a] += MAX_S;
                  Fix_Snake_y();
                  Fix_Snake_x();
                  if(y_pos[a] >= y_pos[a-1])
                    {
                      y_speed[a] = 0;
                      if(x_pos[a] >= x_pos[a-1])
                        {
                          x_speed[a] = -MAX_S;
                        }
                      if(x_pos[a] <= x_pos[a-1])
                        {
                          x_speed[a] = MAX_S;
                        }
                    }
                }
              if(y_pos[a] >= y_pos[a-1] && y_speed[a] < 0)
                {
                  y_pos[a] += -MAX_S;
                  Fix_Snake_y();
                  Fix_Snake_x();
                  if(y_pos[a] <= y_pos[a-1])
                    {
                      y_speed[a] = 0;
                      if(x_pos[a] >= x_pos[a-1])
                        {
                          x_speed[a] = -MAX_S;
                        }
                      if(x_pos[a] <= x_pos[a-1])
                        {
                          x_speed[a] = MAX_S;
                        }
                    }
                }
        }


          if(a == 0)
                {
                  x_pos[0] += x_speed[0];
                  y_pos[0] += y_speed[0];
                  Fix_Snake_y();
                  Fix_Snake_x();
                }
             CrashCheck();
            }
      }
      void CrashCheck()
      {
        for(int a = 1; a < length; a++)
          {
            if(y_speed[0] > 0 && y_speed[a] == 0)
              {
                if(x_pos[0] < x_pos[a] && x_pos[0] < x_pos[a] + 0.05)
                  {
                    if(y_pos[0] < y_pos[a] && y_pos[0] > y_pos[a] - 0.05)
                      {
                        exit(0);
                      }
                  }
              }
            else if(y_speed[0] < 0 && y_speed[a] == 0)
              {
                if(x_pos[0] > x_pos[a] && x_pos[0] < x_pos[a] + 0.05)
                  {
                    if(y_pos[0] < y_pos[a] && y_pos[0] > y_pos[a] - 0.05)
                      {
                        exit(0);
                      }
                  }
              }
          }
      }
void Fix_Snake_x()
  {
    for(int a = 1; a<length; a++)
      {
        if(a > 0)
          {
            if(x_pos[a] <= x_pos[a-1] - 0.05)
              {
                x_pos[a] = x_pos[a-1] - 0.05;
              }
            if(x_pos[a] >= x_pos[a -1] + 0.05)
              {
                x_pos[a] = x_pos[a-1] + 0.05;
              }
          }
      }
  }
  void Fix_Snake_y()
  {
    for(int a = 1; a < length; a++)
          {
            if(a > 0)
              {
                if(y_pos[a] <= y_pos[a-1] - 0.05)
                  {
                    y_pos[a] = y_pos[a-1] - 0.05;
                  }
                if(y_pos[a] >= y_pos[a-1] + 0.05)
                  {
                    y_pos[a] = y_pos[a-1] + 0.05;
                  }
              }
          }
  }

Edit:
New move function

for(int a = 0; a < length; a++)
        {
            if(a > 0)
              {
                if(x_speed[a] < 0 && x_pos[a] >= x_pos[a-1])
                  {
                    x_pos[a] += x_speed[a];
                    if(x_pos[a] == x_pos[a-1])
                      {
                        y_speed[a] = y_speed[a-1];
                        x_speed[a] = 0;
                        continue;
                      }
                  }
                if(x_speed[a] > 0 && x_pos[a] <= x_pos[a-1])
                  {
                    x_pos[a] += x_speed[a];
                    if(x_pos[a] == x_pos[a-1])
                      {
                        y_speed[a] = y_speed[a-1];
                        x_speed[a] = 0;
                        continue;
                      }
                  }
                if(y_speed[a] > 0 && y_pos[a] <= y_pos[a-1])
                  {
                    y_pos[a] += y_speed[a];
                    if(y_pos[a] == y_pos[a-1])
                      {
                        x_speed[a] = x_speed[a-1];
                        y_speed[a] = 0;
                      }
                  }
                if(y_speed[a] < 0 && y_pos[a] >= y_pos[a-1])
                  {
                    y_pos[a] += y_speed[a];
                    if(y_pos[a] == y_pos[a-1])
                      {
                        x_speed[a] = x_speed[a-1];
                        y_speed[a] = 0;
                      }
                  }
              }
            else
              {
                x_pos[0] += x_speed[0];
                y_pos[0] += y_speed[0];
              }
        }

Is causing a few problems. The snake breaks it there are too many simultaneous turns. Only the first two blocks remain in motion

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:03:24+00:00Added an answer on May 24, 2026 at 9:03 am

    If I were you, I would store a std::set with all the invalid coordinates that the snake can’t go to. That would include:

    • the border of the “playground”
    • obstacles
    • the snake’s body

    Then for each move of the snake, considering the x/y speed(s) I would first try to InsertLocation into CInvalidPlaces, if that returns true then I can step there, if false then the snake’s just about to hit a wall, the border or it’s own body and the “game” can finish. Here’s the code for that:

    #include <set>
    using namespace std;
    
    typedef pair<int,int> tInvalidLocation;
    
    struct ltSeCmp
    {
        bool operator()(tInvalidLocation s1, tInvalidLocation s2) const
        {
            if (s1.first == s2.first) return s1.second > s2.second;
            return s1.first > s2.first;
        }
    };
    
    typedef set<tInvalidLocation, ltSeCmp> tInvalidLocations;
    
    class CInvalidPlaces
    {
    private:
        tInvalidLocations mInvalid; //this set will hold all the invalid locations for the snake to go to
    public:
        bool InsertLocation(tInvalidLocation iLoc)
        {
            if (mInvalid.find(iLoc) != mInvalid.end()) return false;    //check if the location is already in the set
            //we survived.. it's safe to go there :)
            mInvalid.insert(iLoc);
            return true;
        }
        bool RemoveLocation(tInvalidLocation iLoc)
        {
            if (mInvalid.find(iLoc)== mInvalid.end()) return false;
            mInvalid.insert(iLoc);
            return true;
        }
    };
    

    What you will have to do additionally is :

    • initially add the margins, all the obstacles, and all the positions of the snake just as they are from where the snake starts
    • modify the move routine, so that when the snake moves, it also has to remove from CInvalidPlaces it’s tail using RemoveLocation
    • after you implement the “enlargement” of the snake you’ll also have to add to CInvalidPlaces the extra segment.

    If need be, you can find in the following places extra information about an stl::set :

    • SGI
    • CPP.com

    HTH,
    JP

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.