Hai, I'm a higher secondary student in kerala. As our project I have to create a simple car racing program in c++. I implemented the following code fragment. May I get some instructions to improve it?
Especially the method of accessing input to controll movement of object?

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
void car(int &);
void right(int &);
void left(int &);
void bgmove(int);
void play(void);
void car(int &cp)
{	gotoxy(cp,25);
	cout<<"|--|";
	gotoxy(cp-1,26);
	cout<<"|    |";
	gotoxy(cp,27);
	cout<<"++++";
	return;
}
void right(int &cp)
{       if(cp<40)
	{
	cp=cp+1;
	}
	return;
}
void left(int &cp)
{       if(cp>20)
	{
	cp=cp-1;
	}

	return;
}
void bgmove(int i)
{       for(int l=i;l<50;l+=5)
	{


	 gotoxy(15,l);cout<<"***";gotoxy(45,l);cout<<"***";
	 }
	 return;
}
void main()
{
	clrscr();
	int cp=28,n,s=1;
	char m;
	cout<<"Enter number of laps\n";
	cin>>n;
	clrscr();
	for(int i=0;i<n;i++)
	{	for(int j=1;j<5;j+=s)
	       {        clrscr();
			bgmove(j);
			car(cp);
			move:
			m=getch();
			delay(20);
			switch(m)
			{
			case '4':left(cp);
				 break;
			case '6':right(cp);
				 break;
			case '8':break;
			case 'q':exit(0);
			case 'c':if(s<5)
			{s=s+1;}
			default :goto move;
			}
	       }
	       }
}

May I get some instructions to improve it?

Yes, don't use iostream.h, use iostream, don't use void main, use int main,
don't use gotoxy, find a replacement, and don't use goto. Also why
do you have a case '8' if you don;t use it?

Thank you for co-operation.
OK I agree. 8 is not required.
How can I move the car if gotoxy is removed?
May I get the graphics codes for a car,road and for tree with explenations?
How can I include sound file in the program?
Can I replace m=getch() with any other function effective than getch()?

Restricting yourself to C++ using standard methods and standard libraries to maintain portability restricts graphics capability. Conversely, doing something with graphics in C++ beyond simple static "ASCII art" restricts portability. To my knowledge at least, you basically have to accept one or the other restrictions if you want to use C++.

However, you don't usually have to use goto() in either standard C++ ASCII art or full blown graphics. Generally speaking goto() can be replaced with loops and control statements.

If your instructor has approved use of non-standard functions/libraries to do this assignment and you are happy with the results you have achieved, then go ahead and stick with what you have.

However, you could think about the field you want to display as table of char. Some of the char are spaces so it looks like nothing is there. Other char may represent the racetrack or the car as it "moves" around the (square) race track. Each move of the car can then represented as a change in the position of the appropriate char representing the car in the table without using goto(). The table can be redisplayed after a given interval of time or after a given event. Likewise, the interface to accept user input to determine direction and speed of the car and how to represent said changes to the position of the "car" within the table is pretty straightforward.

Unfortunately, the graphical appearance of ASCII art isn't very appealing. In a graphics program you are essentially manipulating a table of pixels as opposed to a table of char but the philosophy and sequence of steps to demonstrate the game is similar, once you get beyond the graphics interface itself.

If you want you can replace gotoxy with this windows function :

// Cursor Position //
void gotoxy(short x, short y)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {x, y};
    SetConsoleCursorPosition(handle, position);
}
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.