Click here to Skip to main content
15,886,769 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S26-Mar-11 13:09
Cyclone_S26-Mar-11 13:09 
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6426-Mar-11 13:50
professionalRichard Andrew x6426-Mar-11 13:50 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S27-Mar-11 13:27
Cyclone_S27-Mar-11 13:27 
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6427-Mar-11 14:12
professionalRichard Andrew x6427-Mar-11 14:12 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S27-Mar-11 14:25
Cyclone_S27-Mar-11 14:25 
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6427-Mar-11 15:24
professionalRichard Andrew x6427-Mar-11 15:24 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S28-Mar-11 13:21
Cyclone_S28-Mar-11 13:21 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S30-Mar-11 13:22
Cyclone_S30-Mar-11 13:22 
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 = gcnew snake_segment(this);
					s->panel->BackColor = Color::FromArgb(0,0,255);
					segments->Add(s);
				}

				for each (snake_segment^ s in segments) // Position the segments at start of game.
				{
					l += s->panel->Width + 2;
					s->panel->Left = l;
				}
				get_size_of_snake();
				segments[size-1]->panel->BackColor= Color::Yellow; // Color the head yellow.
			}

			void get_size_of_snake() // Get the size of the snake when a new segment is added by eating something etc.
			{
				size=0;
				for each (snake_segment^ s in segments)
				{
					size++;	
				}
				head=size-1;
			}

			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(direction==1) // Move snake left.
				{
					segments[tail]->panel->Location = Drawing::Point(x - 20 - 2,y); // Note using "Location" instead of "Left/Top" solved a problem where the segment would flicker as it's being moved to the head position.
					head = tail;
					x = segments[head]->panel->Left;
					y = segments[head]->panel->Top;
					tail++;
					if(tail >= size){tail=0;}
				}

				if(direction==-1) // Move snake right.
				{
					segments[tail]->panel->Location = Drawing::Point(x + 20 + 2,y);
					head = tail;
					x = segments[head]->panel->Left;
					y = segments[head]->panel->Top;
					tail++;
					if(tail >= size){tail=0;}
				}

				if(direction==2) // Move snake up;
				{
					segments[tail]->panel->Location = Drawing::Point(x,y - 20 - 2);
					head = tail;
					x = segments[head]->panel->Left;
					y = segments[head]->panel->Top;
					tail++;
					if(tail >= size){tail=0;}
				}
				
				if(direction==-2) // Move snake down;
				{
					segments[tail]->panel->Location = Drawing::Point(x,y + 20 + 2);
					head = tail;
					x = segments[head]->panel->Left;
					y = segments[head]->panel->Top;
					tail++;
					if(tail >= size){tail=0;}
				}
				
			}

			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();
						}
					
					}
				}
			}

			System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) // A timer that will move the snake;
			{
				
				if(!game_started){head_position();game_started=true;} // Find first head position at start of game.
				move_snake();
				check_for_collision();
			}

			System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
			{
				if(e->KeyCode == Keys::Left){direction=1;}
				if(e->KeyCode == Keys::Right){direction=-1;}
				if(e->KeyCode == Keys::Up){direction=2;}
				if(e->KeyCode == Keys::Down){direction=-2;}

				if(e->KeyCode == Keys::Space) // Add a new segment to the snake.
				{
					snake_segment^ s = gcnew snake_segment(this);
					segments->Insert(tail,s);
					head = tail;
					segments[tail]->panel->Location = Drawing::Point(x + segments[head]->panel->Width + 2,y); 
					x = segments[head]->panel->Left;
					y = segments[head]->panel->Top;
					tail++;
					get_size_of_snake();
					if(tail >= size){tail=0;}
				}	
			}
};

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


Thanks.
GeneralRe: Animating a snake (aka Snafu) Pin
Richard Andrew x6430-Mar-11 14:41
professionalRichard Andrew x6430-Mar-11 14:41 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S31-Mar-11 9:14
Cyclone_S31-Mar-11 9:14 
GeneralRe: Animating a snake (aka Snafu) Pin
Cyclone_S16-Apr-11 8:44
Cyclone_S16-Apr-11 8:44 
Questionissue while loading MFC extension dll dynamically Pin
ptr_Electron24-Mar-11 23:37
ptr_Electron24-Mar-11 23:37 
AnswerRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 0:02
mveRichard MacCutchan25-Mar-11 0:02 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 0:21
ptr_Electron25-Mar-11 0:21 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 1:03
mveRichard MacCutchan25-Mar-11 1:03 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 2:50
ptr_Electron25-Mar-11 2:50 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 4:24
mveRichard MacCutchan25-Mar-11 4:24 
QuestionInstances of an object Pin
Cyclone_S19-Mar-11 15:47
Cyclone_S19-Mar-11 15:47 
AnswerRe: Instances of an object Pin
John Schroedl19-Mar-11 17:51
professionalJohn Schroedl19-Mar-11 17:51 
GeneralRe: Instances of an object Pin
John Schroedl19-Mar-11 18:01
professionalJohn Schroedl19-Mar-11 18:01 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 9:56
Cyclone_S20-Mar-11 9:56 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 10:47
sitebuilderLuc Pattyn20-Mar-11 10:47 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 14:47
Cyclone_S20-Mar-11 14:47 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 14:50
sitebuilderLuc Pattyn20-Mar-11 14:50 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 15:30
Cyclone_S20-Mar-11 15:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.