Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Animating a snake (aka Snafu) [modified]

Animating a snake (aka Snafu) [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
comtutorialquestion
15 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cyclone_S

    I'm not sure how to go about this. What to use to draw the snake to the screen and how to get it to animate properly like grow in size when it collects an item or just moving around the screen. I was told not to use a Picture box but to use Panels and no timers. http://www.youtube.com/watch?v=s2tlk1LpWjI&feature=related Maybe start off more simple like in the vid the snake just grows and the entire snake body doesn't move. Thanks in advance.

    modified on Friday, March 25, 2011 8:38 PM

    Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #2

    Well, what's your best guess at how to accomplish this? Most likely it has something to do with what's been taught most recently. What kinds of graphics features have been gone over?

    The difficult we do right away... ...the impossible takes slightly longer.

    C 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      Well, what's your best guess at how to accomplish this? Most likely it has something to do with what's been taught most recently. What kinds of graphics features have been gone over?

      The difficult we do right away... ...the impossible takes slightly longer.

      C Offline
      C Offline
      Cyclone_S
      wrote on last edited by
      #3

      hmm my best guess would be to paint the snake onto the screen using some onpaint method which I don't know which one to use. And how to update it so it can be animated. I don't know much about Brushes or graphics in .net. But I did use owner drawn tool tips and menues. I tried this and updated it using a timer and "this->Invalidate();" but it didn't do what I wanted.

      virtual void OnPaint(PaintEventArgs^ e) override
      {

      }

      Richard Andrew x64R 1 Reply Last reply
      0
      • C Cyclone_S

        hmm my best guess would be to paint the snake onto the screen using some onpaint method which I don't know which one to use. And how to update it so it can be animated. I don't know much about Brushes or graphics in .net. But I did use owner drawn tool tips and menues. I tried this and updated it using a timer and "this->Invalidate();" but it didn't do what I wanted.

        virtual void OnPaint(PaintEventArgs^ e) override
        {

        }

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #4

        Personally, I would create segments of the snake out of small panel objects that have a solid foreground color. Then, to move the snake, simply move the panel that's at the rear of the snake to the front of the snake.

        The difficult we do right away... ...the impossible takes slightly longer.

        C 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Personally, I would create segments of the snake out of small panel objects that have a solid foreground color. Then, to move the snake, simply move the panel that's at the rear of the snake to the front of the snake.

          The difficult we do right away... ...the impossible takes slightly longer.

          C Offline
          C Offline
          Cyclone_S
          wrote on last edited by
          #5

          Hi, So far I created a Snake_Part class. When the Form1 class is first created is calls the Make_Snake Function which creates 4 Snake_Part objects which are added to the list. How do I move the last panel to the front. I'm using a list to store the Snake_Part instances. A timer will be used to move the snake. I need to some how find the last/first elements in the list and then move them acordingly.

          #include "stdafx.h"
          using namespace System;
          using namespace System::Drawing;
          using namespace System::Windows::Forms;
          using namespace System::Collections::Generic; // Required to make lists.

          public ref class Snake_Part
          {
          public:
          Panel^ panel;

          		Snake\_Part( Form ^ form ) // Class Constructor.
          		{ 
          			panel = gcnew Panel();
          			panel->BackColor = Color::Blue;
          			panel->Width = 20; panel->Height = 20;
          			form->Controls->Add(panel);
          		}
          

          };

          public ref class Form1 : public Form
          {
          public:
          List<Snake_Part^>^ Parts; // A list that contains the snake parts.
          int length; // The length of the snake.
          Timer^ Timer1; // The timer that will move the snake;
          //Panel^ p;
          Form1() // Class constructor.
          {
          Parts = gcnew List<Snake_Part^>();
          Timer1 = gcnew Timer();
          Timer1->Interval = 2000;
          Timer1->Start();
          Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
          Make_Snake();
          }

          		void Make\_Snake()
          		{
          			double clr = 255;
          			double dclr = 128 / 2;
          
          			for(int I = 0; I <= 3; I++)
          			{
          				Snake\_Part^ p = gcnew Snake\_Part(this);
          				p->panel->BackColor = Color::FromArgb(0,0,clr);
          				Parts->Add(p);
          				clr -= dclr;
          			}
          
          			for each (Snake\_Part^ p in Parts)
          			{
          				length += p->panel->Width + 2;
          				p->panel->Left = length;
          			}	
          		}
          
          		void move\_snake()
          		{
          				
          		}
          
          		System::Void timer1\_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that will move the snake;
          		{
          			move\_snake();
          			
          		}
          

          };

          [STAThread]
          int main()
          {
          Application::Run(gcnew Form1());
          }

          Thanks

          Richard Andrew x64R 1 Reply Last reply
          0
          • C Cyclone_S

            Hi, So far I created a Snake_Part class. When the Form1 class is first created is calls the Make_Snake Function which creates 4 Snake_Part objects which are added to the list. How do I move the last panel to the front. I'm using a list to store the Snake_Part instances. A timer will be used to move the snake. I need to some how find the last/first elements in the list and then move them acordingly.

            #include "stdafx.h"
            using namespace System;
            using namespace System::Drawing;
            using namespace System::Windows::Forms;
            using namespace System::Collections::Generic; // Required to make lists.

            public ref class Snake_Part
            {
            public:
            Panel^ panel;

            		Snake\_Part( Form ^ form ) // Class Constructor.
            		{ 
            			panel = gcnew Panel();
            			panel->BackColor = Color::Blue;
            			panel->Width = 20; panel->Height = 20;
            			form->Controls->Add(panel);
            		}
            

            };

            public ref class Form1 : public Form
            {
            public:
            List<Snake_Part^>^ Parts; // A list that contains the snake parts.
            int length; // The length of the snake.
            Timer^ Timer1; // The timer that will move the snake;
            //Panel^ p;
            Form1() // Class constructor.
            {
            Parts = gcnew List<Snake_Part^>();
            Timer1 = gcnew Timer();
            Timer1->Interval = 2000;
            Timer1->Start();
            Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
            Make_Snake();
            }

            		void Make\_Snake()
            		{
            			double clr = 255;
            			double dclr = 128 / 2;
            
            			for(int I = 0; I <= 3; I++)
            			{
            				Snake\_Part^ p = gcnew Snake\_Part(this);
            				p->panel->BackColor = Color::FromArgb(0,0,clr);
            				Parts->Add(p);
            				clr -= dclr;
            			}
            
            			for each (Snake\_Part^ p in Parts)
            			{
            				length += p->panel->Width + 2;
            				p->panel->Left = length;
            			}	
            		}
            
            		void move\_snake()
            		{
            				
            		}
            
            		System::Void timer1\_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that will move the snake;
            		{
            			move\_snake();
            			
            		}
            

            };

            [STAThread]
            int main()
            {
            Application::Run(gcnew Form1());
            }

            Thanks

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #6

            A couple of suggestions. You might want to use a Queue to store the snake segments. That makes it easy to retrieve the last segment and push it onto the front. But a List will work too. You'll need a value that determines what direction the snake is moving in. Maybe two values, an X and Y value. These should be integers so that they can store 1, 0, or -1. From there, you can calculate where to position the panel, based upon the head segment's X and Y values, multiplied by the movement indicator values. The flow would be as follows:

            Retrieve the last segment.

            Store it in the Head segment holder variable.

            Push it onto the front of the snake.

            Calculate the new head position.

            Position the Head segment according to the new position.

            The difficult we do right away... ...the impossible takes slightly longer.

            C 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              A couple of suggestions. You might want to use a Queue to store the snake segments. That makes it easy to retrieve the last segment and push it onto the front. But a List will work too. You'll need a value that determines what direction the snake is moving in. Maybe two values, an X and Y value. These should be integers so that they can store 1, 0, or -1. From there, you can calculate where to position the panel, based upon the head segment's X and Y values, multiplied by the movement indicator values. The flow would be as follows:

              Retrieve the last segment.

              Store it in the Head segment holder variable.

              Push it onto the front of the snake.

              Calculate the new head position.

              Position the Head segment according to the new position.

              The difficult we do right away... ...the impossible takes slightly longer.

              C Offline
              C Offline
              Cyclone_S
              wrote on last edited by
              #7

              Thanks for the helpful suggestions. I got it to move the snake right fine but I need help with some of the coding to get it to move properly in the other directions. I'm curently trying to move the snake downwards but I'm stuck. Thanks.

              #include "stdafx.h"
              using namespace System;
              using namespace System::Drawing;
              using namespace System::Windows::Forms;
              using namespace System::Collections::Generic; // Required to make lists.

              public ref class snake_segment
              {
              public:
              Panel^ panel;

              		snake\_segment( Form ^ form ) // Class Constructor.
              		{ 
              			panel = gcnew Panel();
              			panel->BackColor = Color::Blue;
              			panel->Width = 20; panel->Height = 20;
              			form->Controls->Add(panel);
              		}
              

              };

              public ref class Form1 : public Form
              {
              public:
              List<snake_segment^>^ segments; // A list that contains the snake segments.
              int l; // Length of the snake at begining of game.
              int size; // The number of segments in snake/list.
              int last; // The last segment of in the snake.
              int x, y; // The coordinates of the snake segment(head).
              int x2,y2; // Values that will store the direction the snake will move.
              Timer^ Timer1; // The timer that will move the snake;
              bool game_started;

              		Form1() // Class constructor.
              		{	
              			segments = gcnew List<snake\_segment^>();
              			Timer1 = gcnew Timer();
              			Timer1->Interval = 2000;
              			Timer1->Start();
              			Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1\_Tick);
              			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form\_KeyDown);
              			Make\_Snake();
              		}
              		
              		void Make\_Snake()
              		{
              			double clr = 255;
              			double dclr = 128 / 2;
              
              			for(int I = 0; I <= 3; I++)
              			{
              				snake\_segment^ s = gcnew snake\_segment(this);
              				s->panel->BackColor = Color::FromArgb(0,0,clr);
              				segments->Add(s);
              				clr -= dclr;
              			}
              
              			for each (snake\_segment^ s in segments)
              			{
              				l += s->panel->Width + 2;
              				s->panel->Left = l;
              			}	
              		}
              
              		void get\_size\_of\_snake()
              		{
              			size=0;
              			for each (snake\_segment^ s in segments)
              			{
              				size++;	
              			}				
              		}
              
              		void head\_position() // The head position at start of game.
              		{
              			x = segments\[size-1\]->panel->Left;
              			y = segments\[size-1\]->panel->Top;
              		}
              
              		void move\_snake()
              		{
              			if(x2 ==1){segments\[last\]->panel->Left = x + segments\[last\]->panel->Width + 2;} // Move snake right.
              			if(y2 == -1){segm
              
              Richard Andrew x64R 1 Reply Last reply
              0
              • C Cyclone_S

                Thanks for the helpful suggestions. I got it to move the snake right fine but I need help with some of the coding to get it to move properly in the other directions. I'm curently trying to move the snake downwards but I'm stuck. Thanks.

                #include "stdafx.h"
                using namespace System;
                using namespace System::Drawing;
                using namespace System::Windows::Forms;
                using namespace System::Collections::Generic; // Required to make lists.

                public ref class snake_segment
                {
                public:
                Panel^ panel;

                		snake\_segment( Form ^ form ) // Class Constructor.
                		{ 
                			panel = gcnew Panel();
                			panel->BackColor = Color::Blue;
                			panel->Width = 20; panel->Height = 20;
                			form->Controls->Add(panel);
                		}
                

                };

                public ref class Form1 : public Form
                {
                public:
                List<snake_segment^>^ segments; // A list that contains the snake segments.
                int l; // Length of the snake at begining of game.
                int size; // The number of segments in snake/list.
                int last; // The last segment of in the snake.
                int x, y; // The coordinates of the snake segment(head).
                int x2,y2; // Values that will store the direction the snake will move.
                Timer^ Timer1; // The timer that will move the snake;
                bool game_started;

                		Form1() // Class constructor.
                		{	
                			segments = gcnew List<snake\_segment^>();
                			Timer1 = gcnew Timer();
                			Timer1->Interval = 2000;
                			Timer1->Start();
                			Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1\_Tick);
                			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form\_KeyDown);
                			Make\_Snake();
                		}
                		
                		void Make\_Snake()
                		{
                			double clr = 255;
                			double dclr = 128 / 2;
                
                			for(int I = 0; I <= 3; I++)
                			{
                				snake\_segment^ s = gcnew snake\_segment(this);
                				s->panel->BackColor = Color::FromArgb(0,0,clr);
                				segments->Add(s);
                				clr -= dclr;
                			}
                
                			for each (snake\_segment^ s in segments)
                			{
                				l += s->panel->Width + 2;
                				s->panel->Left = l;
                			}	
                		}
                
                		void get\_size\_of\_snake()
                		{
                			size=0;
                			for each (snake\_segment^ s in segments)
                			{
                				size++;	
                			}				
                		}
                
                		void head\_position() // The head position at start of game.
                		{
                			x = segments\[size-1\]->panel->Left;
                			y = segments\[size-1\]->panel->Top;
                		}
                
                		void move\_snake()
                		{
                			if(x2 ==1){segments\[last\]->panel->Left = x + segments\[last\]->panel->Width + 2;} // Move snake right.
                			if(y2 == -1){segm
                
                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #8

                I think you have the values for y2 reversed in the Key handler. If you want the snake to move down, you should be incrementing y2, and decrementing it for up.

                The difficult we do right away... ...the impossible takes slightly longer.

                C 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  I think you have the values for y2 reversed in the Key handler. If you want the snake to move down, you should be incrementing y2, and decrementing it for up.

                  The difficult we do right away... ...the impossible takes slightly longer.

                  C Offline
                  C Offline
                  Cyclone_S
                  wrote on last edited by
                  #9

                  The snake automaticaly moves once the right key is pressed. It sorta works but not sure on how to code it for it moving downwords. Here is the move_snake function;

                  void move_snake()
                  {
                  if(x2 ==1) // Move snake right.
                  {
                  segments[last]->panel->Left = x + segments[last]->panel->Width + 2;
                  x = segments[last]->panel->Left;
                  y = segments[last]->panel->Top;
                  last++;
                  if(last >= size){last=0;}
                  }
                  if(y2 == -1) // Move snake down.
                  {
                  segments[last]->panel->Top = y + segments[last]->panel->Height + 2;
                  segments[last]->panel->Left = x;
                  x = segments[last]->panel->Left;
                  y = segments[last]->panel->Top;
                  last++;
                  if(last >= size){last=0;}
                  }

                  		}
                  

                  Would you be able to help with the code. It would be much aprecieated. Thanks.

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • C Cyclone_S

                    The snake automaticaly moves once the right key is pressed. It sorta works but not sure on how to code it for it moving downwords. Here is the move_snake function;

                    void move_snake()
                    {
                    if(x2 ==1) // Move snake right.
                    {
                    segments[last]->panel->Left = x + segments[last]->panel->Width + 2;
                    x = segments[last]->panel->Left;
                    y = segments[last]->panel->Top;
                    last++;
                    if(last >= size){last=0;}
                    }
                    if(y2 == -1) // Move snake down.
                    {
                    segments[last]->panel->Top = y + segments[last]->panel->Height + 2;
                    segments[last]->panel->Left = x;
                    x = segments[last]->panel->Left;
                    y = segments[last]->panel->Top;
                    last++;
                    if(last >= size){last=0;}
                    }

                    		}
                    

                    Would you be able to help with the code. It would be much aprecieated. Thanks.

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #10

                    I think the problem might be that you're not keeping track of which panel is at the head of the snake, since movement is referenced against the head panel. The code might go something like this: ( I have not tested this code. It might need modification. )

                    if ( y2 == -1 ) // Move down
                    {
                    segments[last]->panel->Top = y + segments[head]->panel->Top + 2;
                    segments[last]->panel->Left = segments[head]->panel->Left;

                    head = last;
                    last++;
                    if ( last >= size )
                    {
                        last = 0;
                    }
                    

                    }

                    The difficult we do right away... ...the impossible takes slightly longer.

                    C 2 Replies Last reply
                    0
                    • Richard Andrew x64R Richard Andrew x64

                      I think the problem might be that you're not keeping track of which panel is at the head of the snake, since movement is referenced against the head panel. The code might go something like this: ( I have not tested this code. It might need modification. )

                      if ( y2 == -1 ) // Move down
                      {
                      segments[last]->panel->Top = y + segments[head]->panel->Top + 2;
                      segments[last]->panel->Left = segments[head]->panel->Left;

                      head = last;
                      last++;
                      if ( last >= size )
                      {
                          last = 0;
                      }
                      

                      }

                      The difficult we do right away... ...the impossible takes slightly longer.

                      C Offline
                      C Offline
                      Cyclone_S
                      wrote on last edited by
                      #11

                      You are correct, that's what the problem was. The snake is now moving in all directions. Thanks for helping me out. :)

                      1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        I think the problem might be that you're not keeping track of which panel is at the head of the snake, since movement is referenced against the head panel. The code might go something like this: ( I have not tested this code. It might need modification. )

                        if ( y2 == -1 ) // Move down
                        {
                        segments[last]->panel->Top = y + segments[head]->panel->Top + 2;
                        segments[last]->panel->Left = segments[head]->panel->Left;

                        head = last;
                        last++;
                        if ( last >= size )
                        {
                            last = 0;
                        }
                        

                        }

                        The difficult we do right away... ...the impossible takes slightly longer.

                        C Offline
                        C Offline
                        Cyclone_S
                        wrote on last edited by
                        #12

                        One last thing. I got the collision function working 99% but there's a small glitch where if the head collides with the tail the collision function doesn't catch it and the snake keeps moving. The collision function.

                        void check_for_collision()
                        {
                        for(int Tail = 0; Tail <= (size-1); Tail++)
                        {
                        for(int I = 0;I <= (size-1); I++)
                        {
                        if(I != head){segments[I]->panel->BackColor= Color::Blue;} else
                        {
                        segments[head]->panel->BackColor= Color::Yellow; // Color the head a different yellow.
                        }

                        					if(head!= Tail && segments\[head\]->panel->Left == segments\[Tail\]->panel->Left && segments\[head\]->panel->Top == segments\[Tail\]->panel->Top) // Detect a collision is segments overlap.
                        					{
                        						segments\[head\]->panel->BackColor= Color::Red;
                        						Timer1->Stop();
                        					}
                        				
                        				}
                        			}
                        		}
                        

                        Entire code.

                        #include "stdafx.h"
                        using namespace System;
                        using namespace System::Drawing;
                        using namespace System::Windows::Forms;
                        using namespace System::Collections::Generic; // Required to make lists.

                        public ref class snake_segment
                        {
                        public:
                        Panel^ panel; // A Panel is used to make the snake.

                        		snake\_segment( Form ^ form ) // Class Constructor.
                        		{ 
                        			
                        			panel = gcnew Panel();
                        			//panel->BackColor= Color::Blue;
                        			panel->Width = 20; panel->Height = 20;
                        			form->Controls->Add(panel);
                        		}
                        

                        };

                        public ref class Form1 : public Form
                        {
                        public:
                        List<snake_segment^>^ segments; // A list that contains the snake segments.
                        int l; // Length of the snake at begining of game.
                        int size; // The number of segments in snake/list.
                        int tail; // The tail segment of the snake.
                        int head; // The head segment of the snake.
                        int x, y; // The coordinates of the snake segment(head).
                        int direction; // Stores the direction the snake will move.
                        Timer^ Timer1; // The timer that will move the snake.
                        bool game_started;

                        		Form1() // Class constructor.
                        		{	
                        			segments = gcnew List<snake\_segment^>();
                        			Timer1 = gcnew Timer();
                        			Timer1->Interval = 500;
                        			Timer1->Start();
                        			Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1\_Tick);
                        			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form\_KeyDown);
                        			Make\_Snake();
                        		}
                        		
                        		void Make\_Snake()
                        		{
                        			for(int I = 0; I <= 5; I++)
                        			{
                        				snake\_segment^ s
                        
                        Richard Andrew x64R 1 Reply Last reply
                        0
                        • C Cyclone_S

                          One last thing. I got the collision function working 99% but there's a small glitch where if the head collides with the tail the collision function doesn't catch it and the snake keeps moving. The collision function.

                          void check_for_collision()
                          {
                          for(int Tail = 0; Tail <= (size-1); Tail++)
                          {
                          for(int I = 0;I <= (size-1); I++)
                          {
                          if(I != head){segments[I]->panel->BackColor= Color::Blue;} else
                          {
                          segments[head]->panel->BackColor= Color::Yellow; // Color the head a different yellow.
                          }

                          					if(head!= Tail && segments\[head\]->panel->Left == segments\[Tail\]->panel->Left && segments\[head\]->panel->Top == segments\[Tail\]->panel->Top) // Detect a collision is segments overlap.
                          					{
                          						segments\[head\]->panel->BackColor= Color::Red;
                          						Timer1->Stop();
                          					}
                          				
                          				}
                          			}
                          		}
                          

                          Entire code.

                          #include "stdafx.h"
                          using namespace System;
                          using namespace System::Drawing;
                          using namespace System::Windows::Forms;
                          using namespace System::Collections::Generic; // Required to make lists.

                          public ref class snake_segment
                          {
                          public:
                          Panel^ panel; // A Panel is used to make the snake.

                          		snake\_segment( Form ^ form ) // Class Constructor.
                          		{ 
                          			
                          			panel = gcnew Panel();
                          			//panel->BackColor= Color::Blue;
                          			panel->Width = 20; panel->Height = 20;
                          			form->Controls->Add(panel);
                          		}
                          

                          };

                          public ref class Form1 : public Form
                          {
                          public:
                          List<snake_segment^>^ segments; // A list that contains the snake segments.
                          int l; // Length of the snake at begining of game.
                          int size; // The number of segments in snake/list.
                          int tail; // The tail segment of the snake.
                          int head; // The head segment of the snake.
                          int x, y; // The coordinates of the snake segment(head).
                          int direction; // Stores the direction the snake will move.
                          Timer^ Timer1; // The timer that will move the snake.
                          bool game_started;

                          		Form1() // Class constructor.
                          		{	
                          			segments = gcnew List<snake\_segment^>();
                          			Timer1 = gcnew Timer();
                          			Timer1->Interval = 500;
                          			Timer1->Start();
                          			Timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1\_Tick);
                          			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form\_KeyDown);
                          			Make\_Snake();
                          		}
                          		
                          		void Make\_Snake()
                          		{
                          			for(int I = 0; I <= 5; I++)
                          			{
                          				snake\_segment^ s
                          
                          Richard Andrew x64R Offline
                          Richard Andrew x64R Offline
                          Richard Andrew x64
                          wrote on last edited by
                          #13

                          I think you should simplify the collision function. I don't think you need two loops. If you want to check whether the head is colliding with any of the other segments, then just do:

                          for (int ThisSegment = 0; ThisSegment < (size-1); ThisSegment ++ )
                          {
                          if ( ThisSegment == head ) continue;

                          if ( segments\[ThisSegment\]->panel->Left == segments\[head\]->panel->Left &&
                               segments\[ThisSegment\]->panel->Top  == segments\[head\]->panel->Top )
                          {
                              Timer->Stop();
                          }
                          

                          }

                          The difficult we do right away... ...the impossible takes slightly longer.

                          C 2 Replies Last reply
                          0
                          • Richard Andrew x64R Richard Andrew x64

                            I think you should simplify the collision function. I don't think you need two loops. If you want to check whether the head is colliding with any of the other segments, then just do:

                            for (int ThisSegment = 0; ThisSegment < (size-1); ThisSegment ++ )
                            {
                            if ( ThisSegment == head ) continue;

                            if ( segments\[ThisSegment\]->panel->Left == segments\[head\]->panel->Left &&
                                 segments\[ThisSegment\]->panel->Top  == segments\[head\]->panel->Top )
                            {
                                Timer->Stop();
                            }
                            

                            }

                            The difficult we do right away... ...the impossible takes slightly longer.

                            C Offline
                            C Offline
                            Cyclone_S
                            wrote on last edited by
                            #14

                            Thanks. Thats much simpler code then what I had.

                            1 Reply Last reply
                            0
                            • Richard Andrew x64R Richard Andrew x64

                              I think you should simplify the collision function. I don't think you need two loops. If you want to check whether the head is colliding with any of the other segments, then just do:

                              for (int ThisSegment = 0; ThisSegment < (size-1); ThisSegment ++ )
                              {
                              if ( ThisSegment == head ) continue;

                              if ( segments\[ThisSegment\]->panel->Left == segments\[head\]->panel->Left &&
                                   segments\[ThisSegment\]->panel->Top  == segments\[head\]->panel->Top )
                              {
                                  Timer->Stop();
                              }
                              

                              }

                              The difficult we do right away... ...the impossible takes slightly longer.

                              C Offline
                              C Offline
                              Cyclone_S
                              wrote on last edited by
                              #15

                              Hi Richard. I was wondering if you would be able to help me with a bit of code. I posted it here. Thank You. [Algorthm][^]

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups