954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Functions.

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.

ashmew2
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
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)?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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.

ashmew2
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

//Grades All Functions

#include

#include


// 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 "< ";

cin>>check1;

}

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

{

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

cin >> name2;

cout<<"\n\tYou said "< ";

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 "< ";

cin>>checkit1;

}

for (int i=0;i>list[i];

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

cout <<" is "<> checkit2;

}

}

//Echo Check Grade Array to Screen.

cout<<"\n\nHere are the grades in your list:\n"< list[j] )

indexOfMin = j;

}

// Swap the max with list[i].

int temp = list[i];

list[i] = 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= 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 "<> choice;

return (choice);

}



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

void goodbye ( )

{

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

}

Luciusduplantis
Newbie Poster
2 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: