Hi , I am basically working on a snakes game in C++. I have managed to move the snake around on the screen with the WASD keys . The snake is a result of a looped putpixel() functions in the ancient graphics.h Borland file. I was hoping to find solution to two problems.

Is there some way to simultaneously run two functions ? Because when the snake turns , it leaves behind a residue which i want to clear but cant figure out a way other than to make a separate function.

Also , is there some sort of removepixel() function , so that you can remove a pixel from the screen by using its x,y coordinates..

Thanks!
PS - Ill attach the code if needed.

Recommended Answers

All 3 Replies

Is there some way to simultaneously run two functions ? Because when the snake turns , it leaves behind a residue which i want to clear but cant figure out a way other than to make a separate function.

You could use multi-threading which allows you to run multiple tasks simultaneously. But this isn't very easy stuff, so you might want to consider doing it like this:

while (1){
     DrawNewPosition(....);
     RemoveOldPixels(....);
}

Also , is there some sort of removepixel() function , so that you can remove a pixel from the screen by using its x,y coordinates..

I don't know. Turbo is too old for me :) Can't you just print a pixel using the backgroundcolor (black for example)?

Thanks for the reply , niek_e. I actually looked at threading and *gulp* , its actually too much to handle.
Im actually priting the black pixels just like you selected , but the problem is that so far , it sort of works , but when you turn the snake , it leaves a trail behind of (not black) colour.

So , is there any other alternative to Threading ?

#include <iostream.h>

#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <process.h>



void MoveRight(int,int);

void MoveLeft(int,int);

void MoveUp(int,int);

void MoveDown(int,int);

void cleanlr(int , int);

void cleanud(int , int);



int l,los=100,xd=3;char ch; //los = length of snake. xd = delay var. less xd= more speed



int main()

{

clrscr();

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

MoveRight(1,10);

getch();

return 0;

}

void MoveRight(int i,int j)

{       int l=i;

	for (i;;i++)

	{

	putpixel(i,j,12);

	delay (xd);

	for (l;l<i-los;l++) putpixel (l,j,0);

		if (kbhit())

		{



		ch=getch();

		if (ch=='s'){cleanlr(i,j); MoveDown(i,j);}

		if (ch=='w'){cleanlr(i,j); MoveUp(i,j);}

		if (ch=='q') exit(0);

		}

	}

}



void MoveLeft(int i,int j)

{       los++;

	int l=i;

	for (i;;i--)

	{

	putpixel(i,j,12);

	delay (xd);

	for (l;i<l-los;l--) putpixel(l,j,0);



	if (kbhit())

		{

		ch=getch();

		if (ch=='q')exit(0);

		if (ch=='s'){cleanlr(i,j);MoveDown(i,j);}

		if (ch=='w'){cleanlr(i,j);MoveUp(i,j);}

		}

	}

}



void MoveUp(int i,int j)

{

	int l=j,z;

	for (j;;j--)

	{





	delay(xd);

	putpixel(i,j,12);

	for (l;l>j+los;l--) putpixel(i,l,0);

		if(kbhit())

		{

		ch=getch();

		if (ch=='a'){cleanud(i,j);MoveLeft(i,j);}

		if (ch=='d'){cleanud(i,j);MoveRight(i,j);}

		if (ch=='q') exit(0);

		}

	}

}



void MoveDown(int i,int j)

{

	int l=j;

	for (j;;j++)

	{

	delay(xd);



	putpixel(i,j,12);

	for (l;l<j-los;l++) putpixel(i,l,0);

		if(kbhit())

		{

		ch=getch();

		if (ch=='a'){cleanud(i,j); MoveLeft(i,j);}

		if (ch=='d'){cleanud(i,j); MoveRight(i,j);}

		if (ch=='q') exit(0);

		}

	}

}



void cleanlr(int i , int j)

{

int z=0;

for (z=0;z<=los;z++)

	{

	delay(xd);

	putpixel(i+z-los,j,0);

	putpixel(i-z+los,j,0);

	}

}



void cleanud(int i,int j)

{

int z=0;

for (z=0;z<=los;z++)

	{

	delay(xd);

	putpixel(i,j+z-los,0);

	putpixel(i,j-z+los,0);

	}

}

Ive done this on Turbo C 3.0 Use the WASD Keys.

//I'm Not sure What the key functions are in this program can you help me?

//Grades All Functions

#include <iostream.h>

#include <fstream.h>


// Functions Called
void namefile (char filename [ ]);

void names (char name1[ ],char name2[ ]);

int load_list (int list[ ]);

void selection_sort (int list[ ],int howmany);

float averageof (int list[ ],int howmany);

char lettergrade (float average);

void screenoutput(char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g);

void fileoutput(char filename[ ], char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g);

char morestudents ( );

void goodbye( );

int main (void)

{

char filename [12]; // Name of file to store data on disk

char lastname[20], firstname[20]; // Student's first and last names

int howmany; // Number of Grades

int gradelist[50]; // List of grades (up to 50)

float average; // Average Grade

int high, low; // Highest and Lowest Grades

char grade; // Grade of A, B, C, D or F

char anotherstudent='y';

// Program steps begin here:======================================
namefile (filename);

while (anotherstudent == 'y' || anotherstudent == 'Y')

{

names(firstname,lastname);

howmany = load_list (gradelist);

selection_sort (gradelist,howmany);

high = gradelist[(howmany-1)];

low = gradelist [0];

average = averageof (gradelist, howmany);

grade = lettergrade (average);

screenoutput(firstname,lastname,howmany,gradelist,high,low,average,grade);

fileoutput(filename,firstname,lastname,howmany,gradelist,high,low,average,grade);

anotherstudent = morestudents ( );

}

goodbye ( );

return (0);

} // End of main program ..... functions follow
//Functions----------------------------------------------------------------------------------------------------
void namefile (char filename [ ] )

{

//Get Name of Datafile

cout<<"\n\n\tEnter a filename as filename.dat: ";

cin>>filename;

}

void names (char name1[ ],char name2[ ])

{

char check1 = 'N';

char check2 = 'N';

while (check1 == 'N' || check1 == 'n')

{

cout <<"\n\n\tWhat is this student's first name? ";

cin >> name1;

cout<<"\n\tYou said "<<name1<<" Is that Correct? Y or N ---> ";

cin>>check1;

}

while (check2 == 'N' || check2 == 'n')

{

cout <<"\n\n\tWhat is this student's last name? ";

cin >> name2;

cout<<"\n\tYou said "<<name2<<" Is that Correct? Y or N ---> ";

cin>>check2;

}

}

int load_list (int list[ ])

{

char checkit1;

char checkit2;

int howmany;

checkit1 = 'N';
while (checkit1 == 'N' || checkit1 == 'n')

{

cout <<"\n\n\tHow many grades do you wish to enter for this student? ";

cin >> howmany;

cout<<"\n\tYou said "<<howmany<<" Is that Correct? Y or N ---> ";

cin>>checkit1;

}

for (int i=0;i<howmany;i++)

{

checkit2 = 'N';

while (checkit2 == 'N' || checkit2 == 'n')

{

cout << "\n\tPlease Enter grade: \a"<<(i+1)<<" ..... ";

cin >>list;

cout <<"\n\n\tCheck the input: \a Grade "<<(i+1);

cout <<" is "<<list<<" Correct? Y or N ";

cin >> checkit2;

}

}

//Echo Check Grade Array to Screen.

cout<<"\n\nHere are the grades in your list:\n"<<endl;

for (int j=0;j<howmany;j++)

{

cout <<"\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}

return (howmany);

}
void selection_sort (int list[ ],int howmany)

{

for( int i=0; i < howmany - 1; i++ )

{

// Find the smallest item between index i and index howmany-1.

int indexOfMin = i;

for( int j=i+1; j < howmany; j++ )

{

if( list[indexOfMin] > list[j] )

indexOfMin = j;

}

// Swap the max with list.

int temp = list;

list = list[indexOfMin];

list[indexOfMin] = temp;

}

}

float averageof (int list[ ],int howmany)

{

int sum ; //The sum of the numbers in the list

float average; //The average of the numbers in the list

sum = 0;

for (int index=0;index<howmany;index++)

{

sum += list[index];

}

average = float (sum) / float (howmany);

return (average); //send back the sum

}

char lettergrade (float average)

{

char grade;

if (average >= 89.5) grade = 'A';

if (average < 89.5 && average >= 79.5 ) grade = 'B';

if (average < 79.5 && average >= 69.5 ) grade = 'C';

if (average < 69.5 && average >= 59.5 ) grade = 'D';

if (average < 59.5) grade = 'F';

return (grade);

}

void screenoutput(char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g)

{

cout <<"\n\tGrades for "<<a<<" "<<b<<endl;

cout<<"\n\Here are the grades in your list:\n"<<endl;

for (int j=0;j<n;j++)

{

cout <<"\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}


//(continued on next page.......)

cout <<"\n\n\tHighest Grade: "<<h<<endl;

cout <<"\tLowest Grade: "<<l<<endl;

cout <<"\tAverage: "<<avg<<endl;

cout <<"\tfor: "<<n<<" grades."<<endl;

cout <<"\tThis is a letter grade of "<<g;

}


void fileoutput(char filename [],char a[],char b[],int n,int list[],int h, int l,float avg, char g)

{

//Open the Datafile

ofstream datafile;

datafile.open(filename,ios::app);

//If Something Goes Wrong

if (datafile.fail())

{

cout<<"\nFile could not be opened.";

}

//Gets info from user to enter into the Datafile

else

{

datafile <<"\n\n\n\n\n\tGrades for "<<a<<" "<<b<<endl;

datafile<<"\n\n\tHere are the grades in your list:\n"<<endl;

for (int j=0;j<n;j++)

{

datafile <<"\n\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}

datafile <<"\n\n\tHighest Grade: "<<h<<endl;

datafile <<"\tLowest Grade: "<<l<<endl;

datafile <<"\tAverage: "<<avg<<endl;

datafile <<"\tfor: "<<n<<" grades."<<endl;

datafile <<"\tThis is a letter grade of "<<g;

}

// Closes the datafile

datafile.close( );

}


//--------------------------------------------------------------------------------------------------------------

char morestudents( )

{

char choice;

cout<<"\n\n\n\tDo You Wish to Enter Grades for Another Student?"<<endl;

cout<<"\tEnter Y for yes or any other key for no. ";

cin >> choice;

return (choice);

}

//--------------------------------------------------------------------------------------------------------------

void goodbye ( )

{

cout << "\n\n\tGood Things Happen To People Who don't Give Up!!!!!! Bye.\n\n";

}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.