{"id":3125,"date":"2022-09-13T16:04:37","date_gmt":"2022-09-13T11:04:37","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=3125"},"modified":"2022-09-13T16:04:41","modified_gmt":"2022-09-13T11:04:41","slug":"build-multiplayer-car-racing-game-using-cpp-graphics-program","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/","title":{"rendered":"Build Multiplayer Car Racing Game Using C++ Graphics Program"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Keys Control for 2 Players Car Racing Game in C++<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the &#8216;LEFT&#8217; and &#8216;RIGHT&#8217; arrows to control the right car.<\/li><li>Use keys &#8216;A&#8217; and &#8216;D&#8217; to control the left car.<\/li><\/ul>\n\n\n\n<p><strong>Note:<\/strong> Use <strong>Turbo C Compiler<\/strong> to run the program with all graphics libraries loaded.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Multiplayer Car Racing Game Source Code<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">code.cpp<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include&lt;stdio.h&gt;\n#include&lt;conio.h&gt;\n#include&lt;graphics.h&gt;\n#include&lt;iostream.h&gt;\n#include&lt;dos.h&gt;\n#include&lt;process.h&gt;\n#include&lt;stdlib.h&gt;\n#include&lt;math.h&gt;\n#include&lt;time.h&gt;\n#include&lt;fstream.h&gt;\n#include&lt;string.h&gt;\n\n\n\n#define LEFT 75\n#define RIGHT 77\n#define UP 72\n#define DOWN 80\n#define ENTER 13\n#define ESC 27\n#define A 65\n#define SMALLA 97\n#define D 68\n#define SMALLD 100\n\n\nstruct highscore{\n\tchar name[7];\n\tdouble score;\n};\n\n\nclass Car\n{\n\n  public:\n  float maxx, maxy, one3x, one4x, midx, midy, carTopHeight;\n  float  midRightLeft, midRightRight, carRightMid, midLeftLeft, midLeftRight, carLeftMid;\n  int gameOver, level, speed1, speed2, speed3, speed4;\n  double score, highScore;\n  char highScorer[7];\n  int blocksRight[5][2], blocksCountRight;\n  int blocksLeft[5][2], blocksCountLeft;\n  int posRightCar, posLeftCar; \t\t\/\/ (1) Right , (0) Left\n  char startingBlockAtRight;\n  void initializeData();\n  void showTracks(int);\n\n  void showCar(float);\n  void initCarRight();\n  void initCarLeft();\n\n  void animateBlocks();\n  void initAnimate();\n  void animate();\n  void drawBlock(int, int);\n  void displayBlocksRight();\n  void displayBlocksLeft();\n  void moveRightCarLeft();\n  void moveRightCarRight();\n  void moveLeftCarLeft();\n  void moveLeftCarRight();\n  void showBoomRight(float, float);\n  void showBoomLeft(float, float);\n  int checkGameOver();\n  void displayGameOver();\n  void showScore();\n  void readHighScore();\n\n};\n\n\nvoid Car::initializeData()\n{\n\tsrand ( time(NULL) );\n\tmaxx=getmaxx()-170;\n\tmaxy=getmaxy();\n\tone4x=maxx\/4;\n\tmidx=maxx\/2;\n\tmidy = getmaxy()\/2;\n\tone3x=3*maxx\/4;\n\n\t\/* track mid positions *\/\n\tmidRightLeft = (2*one4x+50+3*one4x+50)\/2.0;\n\tmidRightRight = (3*one4x+60+4*one4x+60)\/2.0;\n\tmidLeftLeft = (20+one4x+20)\/2.0;\n\tmidLeftRight = (one4x+30+2*one4x+30)\/2.0;\n\n\n\t\/**\/\n\n\tblocksRight[0][0] = 110;\n\tblocksLeft[0][0] = 110;\n\tfor(int i=1;i&lt;=4;i++){\n\t\tblocksRight[i][0] = 0;\n\t\tblocksLeft[i][0] = 0;\n\t}\n\tblocksCountRight = 1;\n\tblocksCountLeft = 1;\n\t\/* position of first block , random*\/\n\tif(rand()%2==0) blocksRight[0][1] = 0;\n\telse blocksRight[0][1] = 1;\n\tif(rand()%2==0) blocksLeft[0][1] = 0;\n\telse blocksLeft[0][1] = 1;\n\t\/* *\/\n\tposRightCar = 5;\n\tposLeftCar = 5;\n\tcarTopHeight = maxy-145;\n\tgameOver = 0;\n\tlevel = 1;\n\tspeed1 = 180; speed2 = 120; speed3 = 70; speed4 = 40;\n\tscore = 0;\n\thighScorer[0]=' ';\n\thighScore=0;\n\n\treadHighScore();\n}\n\n\nvoid Car::animate()\n{\n\tint x, i=0;\n\twhile(!kbhit())\n\t{\n\t\tsrand ( time(NULL) );\n\t\tx = i % 5 + 1;\n\t\tshowTracks(x);\n\t\tinitCarRight();\n\t\tinitCarLeft();\n\t\tanimateBlocks();\n\t\tshowScore();\n\t\tif(checkGameOver())\n\t\t{\n\t\t\tgameOver = 1;\n\t\t\tbreak;\n\t\t}\n\t\tswitch(level)\n\t\t{\n\t\t\tcase 1:\n\t\t\t\tdelay(speed1);\n\t\t\t\tbreak;\n\t\t\tcase 2: \n\t\t\t\tdelay(speed2);\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tdelay(speed3);\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tdelay(speed4);\n\t\t\t\tbreak;\n\t\t}\n\t\tcleardevice();\n\t\ti++;\n\t}\n\n}\n\n\nvoid Car::initAnimate()\n{\n\tchar c;\n\tanimate();\n\tif(gameOver) displayGameOver();\n\telse\n\t{\n\t\tc = getch();\n\t\tif(c==0)\n\t\t{\n\t\t\tswitch(getch())\n\t\t\t{\n\t\t\t\tcase LEFT:\n\t\t\t\t\tmoveRightCarLeft();\n\t\t\t\t\tbreak;\n\t\t\t\tcase RIGHT:\n\t\t\t\t\tmoveRightCarRight();\n\t\t\t\t\tbreak;\n\t\t\t\t\n\n\t\t\t\tdefault:\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tswitch(c)\n\t\t{\n\t\t\tcase ESC:\n\t\t\t\texit(0);\n\t\t\t\tbreak;\n\n\t\t\tcase A:\n\t\t\t\tmoveLeftCarLeft();\n\t\t\t\tbreak;\n\n\t\t\tcase SMALLA:\n\t\t\t\tmoveLeftCarLeft();\n\t\t\t\tbreak;\n\n\t\t\tcase D:\n\t\t\t\tmoveLeftCarRight();\n\t\t\t\tbreak;\n\n\t\t\tcase SMALLD:\n\t\t\t\tmoveLeftCarRight();\n\t\t\t\tbreak;\n\t\t}\n\t\t\n\n\t\tinitAnimate();\n\t}\n\n}\n\n\nvoid Car::animateBlocks()\n{\n\tint tempBlocksCount;\n\ttempBlocksCount = blocksCountRight;\n\t\/\/ 0 position \/ distance 1 - Left(0) \/ Right(1)\n\n\tfor(int i=tempBlocksCount-1;i&gt;=0;i--)\n\t{\n\t\tblocksRight[i][0]+=10;\n\t\tblocksLeft[i][0]+=10;\n\n\t\/* check for new block arrival *\/\n\t\tif(blocksRight[i][0]&gt;=210 &amp;&amp; blocksRight[i+1][0] == 0)\n\t\t{\n\t\t\tblocksRight[i+1][0] = 20;\t\t\t\/\/ START NEW BLOCK FROM 20 HEIGHT\n\t\t\tif(rand()%2 == 0) blocksRight[i+1][1] = 0;\n\t\t\telse blocksRight[i+1][1] = 1;\n\n\t\t\tblocksCountRight++;\n\t\t}\n\n\t\tif(blocksLeft[i][0]&gt;=210 &amp;&amp; blocksLeft[i+1][0] == 0)\n\t\t{\n\t\t\tblocksLeft[i+1][0] = 20;\t\t\t\/\/ START NEW BLOCK FROM 20 HEIGHT\n\t\t\tif(rand()%2 == 0) blocksLeft[i+1][1] = 0;\n\t\t\telse blocksLeft[i+1][1] = 1;\n\n\t\t\tblocksCountLeft++;\n\t\t}\n\n\t\/*\t*\/\n\n\t\/*\tcheck if block is gone out of screen height *\/\n\t\tif(blocksRight[i][0]&gt;=479)\n\t\t{\n\t\t\tfor(int j=0;j&lt;blocksCountRight-1;j++)\n\t\t\t{\n\t\t\t\tblocksRight[j][0] = blocksRight[j+1][0];\n\t\t\t\tblocksRight[j][1] = blocksRight[j+1][1];\n\t\t\t}\n\t\t\tblocksRight[j][0] = 0;\n\t\t\tblocksCountRight--;\n\n\n\t\t\t\/* increment score *\/\n\t\t\tscore+=level*10;\n\t\t\tswitch(level){\n\t\t\t\tcase 1:\n\t\t\t\t\tif(score&gt;=2000)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 2:\n\t\t\t\t\tif(score&gt;=6000)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tif(score&gt;=15000)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: break;\n\t\t\t}\n\n\t\t}\n\n\n\t\tif(blocksLeft[i][0]&gt;=479)\n\t\t{\n\t\t\tfor(int j=0;j&lt;blocksCountLeft-1;j++)\n\t\t\t{\n\t\t\t\tblocksLeft[j][0] = blocksLeft[j+1][0];\n\t\t\t\tblocksLeft[j][1] = blocksLeft[j+1][1];\n\t\t\t}\n\t\t\tblocksLeft[j][0] = 0;\n\t\t\tblocksCountLeft--;\n\n\t\t\t\n\t\t\t\/* increment score *\/\n\t\t\tscore+=level*10;\n\t\t\tswitch(level)\n\t\t\t{\n\t\t\t\tcase 1:\n\t\t\t\t\tif(score&gt;=150)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 2:\n\t\t\t\t\tif(score&gt;=400)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tif(score&gt;=800)\n\t\t\t\t\t\tlevel++;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: break;\n\t\t\t}\n\n\t\t}\n\t\t\/**\/\n\n\t}\n\n\tdisplayBlocksRight();\n\tdisplayBlocksLeft();\n\n}\n\nvoid Car::displayBlocksRight()\n{\n\tsetcolor(RED);\n\tfor(int i=0;i&lt;blocksCountRight;i++)\n\t{\n\t\tif(blocksRight[i][1] == 0) drawBlock(midRightLeft, blocksRight[i][0]);\n\t\telse drawBlock(midRightRight, blocksRight[i][0]);\n\t}\n}\n\n\nvoid Car::displayBlocksLeft()\n{\n\tsetcolor(BLUE);\n\tfor(int i=0;i&lt;blocksCountLeft;i++)\n\t{\n\t\tif(blocksLeft[i][1] == 0) drawBlock(midLeftLeft, blocksLeft[i][0]);\n\t\telse drawBlock(midLeftRight, blocksLeft[i][0]);\n\t}\n}\n\nvoid Car::drawBlock(int x, int y)\n{\n\tcircle(x, y, 10);\n\tcircle(x, y, 5);\n\tcircle(x, y, 4);\n\tcircle(x, y, 9);\n}\n\n\nvoid Car::showTracks(int init)\n{\n\tfor ( int i = init;i &lt; 70;i += 5 )\n    {\n\t\/\/ leftmost track\n\t    setcolor( GREEN );\n\t    setfillstyle( 9, GREEN );\n\t    bar( 0, i * 8, 20, ( i + 2 ) * 8 );\n\t    bar( one4x+20, i * 8, one4x+30, ( i + 2 ) * 8 );\n\n\t    \/\/ left track\n\t    setcolor( GREEN );\n\t    setfillstyle( 9, GREEN );\n\t    bar( 2*one4x+30, i * 8, 2*one4x+50, ( i + 2 ) * 8 );\n\n\n\t\/\/ right track\n\t    setcolor( GREEN );\n\t    setfillstyle( 9, GREEN );\n\t    bar( 3*one4x+50, i * 8, 3*one4x+60, ( i + 2 ) * 8 );\n\n\t    \/\/ rightmost track\n\t    setcolor( GREEN );\n\t    setfillstyle( 9, GREEN );\n\t    bar( 4*one4x+60, i * 8, 4*one4x+80, ( i + 2 ) * 8 );\n\n    }\n\n}\n\nvoid Car::initCarRight()\n{\n\tif(posRightCar == 0 || posRightCar == 1)\n\t{\n\t\tif(posRightCar == 0) carRightMid = midRightLeft;\n\t\telse carRightMid = midRightRight;\n\t}\n\telse\n\t{\n\t\tif( rand()%2 == 0 )\n\t\t{\n\t\t\tposRightCar = 0;\n\t\t\tcarRightMid = midRightLeft;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tposRightCar = 1;\n\t\t\tcarRightMid = midRightRight;\n\t\t}\n\t}\n\n\t\/* display the car *\/\n\tshowCar(carRightMid);\n\tshowCar(carRightMid+1);\n\t\/**\/\n\n\t\t\n}\n\nvoid Car::initCarLeft()\n{\n\tif(posLeftCar == 0 || posLeftCar == 1)\n\t{\n\t\tif(posLeftCar == 0) carLeftMid = midLeftLeft;\n\t\telse carLeftMid = midLeftRight;\n\t}\n\telse\n\t{\n\t\tif( rand()%2 == 0 )\n\t\t{\n\t\t\tposLeftCar = 0;\n\t\t\tcarLeftMid = midLeftLeft;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tposLeftCar = 1;\n\t\t\tcarLeftMid = midLeftRight;\n\t\t}\n\t}\n\n\t\/* display the car *\/\n\tshowCar(carLeftMid);\n\tshowCar(carLeftMid+1);\n\t\/**\/\n\n\t\t\n}\n\n\nvoid Car::showCar(float mid)\n{\n\tif(mid&lt;midx) setcolor(BLUE);\n\telse setcolor(RED);\n\tline(mid-15, maxy-100, mid-15, maxy-130);\n\tline(mid+15, maxy-100, mid+15, maxy-130);\n\tarc(mid, maxy-130, 0, 180, 15);\t\t\/\/ so the carTopHeight is maxy-130-15 = maxy-145\n\tarc(mid, maxy-100, 180, 360, 15);\n\tif(mid&lt;midx) setfillstyle( 1, BLUE );\n\telse setfillstyle(1, RED);\n\tbar(mid-6, maxy-118, mid+6, maxy-112);\n\t\n\n\tline(mid-6, maxy-118, mid-9, maxy-125);\n\tline(mid+6, maxy-118, mid+9, maxy-125);\t\n\tline(mid-6, maxy-112, mid-9, maxy-105);\n\tline(mid+6, maxy-112, mid+9, maxy-105);\t\n\n\tarc(mid, maxy-125, 0, 180, 10);\n\tarc(mid, maxy-105, 180, 360, 10);\t\n}\n\n\n\n\nvoid Car::moveRightCarLeft(){\n\t\/\/ if car is at right position then only move to left\n\tif(posRightCar == 1)\n\t{\n\t\tposRightCar = 0;\n\t\tshowCar(midRightLeft);\n\t\tshowCar(midRightLeft+1);\n\t}\n\n}\n\nvoid Car::moveLeftCarLeft(){\n\t\/\/ if car is at right position then only move to left\n\tif(posLeftCar == 1)\n\t{\n\t\tposLeftCar = 0;\n\t\tshowCar(midLeftLeft);\n\t\tshowCar(midLeftLeft+1);\n\t}\n\n}\n\nvoid Car::moveRightCarRight(){\n\tif(posRightCar == 0){\n\t\tposRightCar = 1;\n\t\tshowCar(midRightRight);\n\t\tshowCar(midRightRight+1);\n\t}\n}\n\nvoid Car::moveLeftCarRight(){\n\tif(posLeftCar == 0){\n\t\tposLeftCar = 1;\n\t\tshowCar(midLeftRight);\n\t\tshowCar(midLeftRight+1);\n\t}\n}\n\n\n\nvoid Car::showBoomRight(float x, float y)\n{\n\tsetcolor(6);\n\tline(x+4,y+3,x+4,y+4.5);\n\tline(x+4,y+4.5,x+6,y);\n\tline(x+6,y,x+9,y+4);\n\tline(x+9,y+4,x+16,y);\n\tline(x+16,y+0,x+16,y+4.8);\n\tline(x+16,y+4.8,x+20,y+4.7);\n\tline(x+20,y+4.7,x+13,y+5.5);\n\tline(x+13,y+5.5,x+20,y+8);\n\tline(x+20,y+8,x+9,y+7);\n\tline(x+9,y+7,x+6,y+10);\n\tline(x+6,y+10,x+4,y+7);\n\tline(x+4,y+7,x,y+8);\n\tline(x,y+8,x+5,y+5.5);\n\tline(x+5,y+5.5,x,y+3);\n}\n\nint Car::checkGameOver()\n{\n\tfloat boomRightY = 0, boomLeftY = 0, midRightCar=0, midLeftCar=0, boomRightX, boomLeftX;\n\t\n\t\/* check if right car hit the block *\/\n\tif( posRightCar == blocksRight[0][1] &amp;&amp; ( carTopHeight &lt;= blocksRight[0][0]+10  &amp;&amp; blocksRight[0][0] &lt;= carTopHeight+70) )\t\/\/ 15, 15 - arc, 30 - line\n\t{\t\n\t\tif(blocksRight[0][0] &gt; carTopHeight+15)\n\t\t{\n\t\t\tif(posRightCar == 0)\n\t\t\t{\n\t\t\t\tmidRightCar = midRightLeft+20;\n\t\t\t\tboomRightX = midRightLeft+5;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmidRightCar = midRightRight-20;\n\t\t\t\tboomRightX = midRightRight-10;\n\t\t\t}\n\n\t\t\tboomRightY = blocksRight[0][0]-5;\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(posRightCar == 0 )\n\t\t\t{\n\t\t\t\tmidRightCar = midRightLeft;\n\t\t\t\tboomRightX = midRightLeft-10;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmidRightCar = midRightRight;\n\t\t\t\tboomRightX = midRightRight-10;\n\t\t\t}\n\t\t\tboomRightY = blocksRight[0][0]+5;\n\t\t}\t\t\t\n\t\tgameOver = 1;\n\t}\n\n\t\/* check if left car hit the block *\/\n\tif( posLeftCar == blocksLeft[0][1] &amp;&amp; ( carTopHeight &lt;= blocksLeft[0][0]+10  &amp;&amp; blocksLeft[0][0] &lt;= carTopHeight+70) )\n\t{\n\t\tif(blocksLeft[0][0] &gt; carTopHeight+15)\n\t\t{\n\t\t\tif(posLeftCar == 0)\n\t\t\t{\n\t\t\t\tmidLeftCar = midLeftLeft+20;\n\t\t\t\tboomLeftX = midLeftLeft+5;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmidLeftCar = midLeftRight-20;\n\t\t\t\tboomLeftX = midLeftRight-10;\n\t\t\t}\n\n\t\t\tboomLeftY = blocksLeft[0][0]-5;\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(posLeftCar == 0 )\n\t\t\t{\n\t\t\t\tmidLeftCar = midLeftLeft;\n\t\t\t\tboomLeftX = midLeftLeft-10;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmidLeftCar = midLeftRight;\n\t\t\t\tboomLeftX = midLeftRight-10;\n\t\t\t}\n\t\t\tboomLeftY = blocksLeft[0][0]+5;\n\t\t}\t\t\t\n\t\tgameOver = 1;\n\t}\n\n\tif(gameOver)\n\t{\n\t\tcleardevice();\n\t\tdisplayBlocksRight();\n\t\tdisplayBlocksLeft();\n\t\tshowTracks(0);\n\t\tshowScore();\n\t\tif(midRightCar!=0)\n\t\t{\n\t\t\tshowCar(midRightCar);\n\t\t\tshowCar(midRightCar+1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(posRightCar == 0)\n\t\t\t{\n\t\t\t\tshowCar(midRightLeft);\n\t\t\t\tshowCar(midRightLeft+1);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshowCar(midRightRight);\n\t\t\t\tshowCar(midRightRight+1);\n\t\t\t}\n\n\t\t}\n\t\tif(midLeftCar!=0)\n\t\t{\n\t\t\tshowCar(midLeftCar);\n\t\t\tshowCar(midLeftCar+1);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(posLeftCar == 0)\n\t\t\t{\n\t\t\t\tshowCar(midLeftLeft);\n\t\t\t\tshowCar(midLeftLeft+1);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tshowCar(midLeftRight);\n\t\t\t\tshowCar(midLeftRight+1);\n\t\t\t}\n\n\t\t}\n\t\t\/* display boom in the side where it hits block either of side or both side *\/\n\t\tif(boomRightY!=0)\n\t\t{\t\n\t\t\tshowBoomRight(boomRightX, boomRightY);\n\t\t\tshowBoomRight(boomRightX+1, boomRightY);\n\t\t}\n\t\tif(boomLeftY!=0)\n\t\t{\t\n\t\t\tshowBoomRight(boomLeftX, boomLeftY);\n\t\t\tshowBoomRight(boomLeftX+1, boomLeftY);\n\t\t}\n\t\t\/**\/\n\t\treturn 1;\n\t}\n\telse return 0;\n\n}\n\n\n\nvoid Car::displayGameOver()\n{\n\tsetcolor(RED);\n\tsettextstyle( 3, 0, 7 );\n\touttextxy(midx-130, midy-65, &quot;Game Over!&quot;);\n\t\/* save if its high score *\/\n\t\tstruct highscore s, ns;\n\t\tFILE *read, *write;\n\t\tread = fopen(&quot;highscore.txt&quot;,&quot;r&quot;);\n\t\tif(read==NULL)\n\t\t{\n\t\t\tfclose(read);\n\t\t\tgetch();\t\t\n\t\t\tclrscr();\n\t\t\tcleardevice();\t\t\n\t\t\ts.score = score;\n\t\t\tcout&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;\\t\\t\\tCongratulation you got highest score !&quot;;\n\t\t\tcout&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;\\t\\t\\tEnter your name &quot;;\n\t\t\tscanf(&quot;%s&quot;,&amp;s.name);\n\t\t\twrite = fopen(&quot;highscore.txt&quot;, &quot;w+&quot;);\n\t\t\tfwrite(&amp;s,sizeof(s),1,write);\n\t\t\tfclose(write);\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfread(&amp;s, sizeof(s),1, read);\n\t\t\tif(s.score&lt;score)\n\t\t\t{\n\t\t\t\tgetch();\n\t\t\t\tclrscr();\n\t\t\t\tcleardevice();\n\t\t\t\tns.score = score;\n\t\t\t\tcout&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;\\t\\t\\tCongratulation you got highest score !&quot;;\n\t\t\t\tcout&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;\\t\\t\\tEnter your name &quot;;\n\t\t\t\tscanf(&quot;%s&quot;,&amp;ns.name);\n\t\t\t\twrite = fopen(&quot;highscore.txt&quot;, &quot;w+&quot;);\n\t\t\t\tfwrite(&amp;ns,sizeof(s),1,write);\n\t\t\t\tfclose(write);\n\t\t\t}\n\t\t\tfclose(read);\n\t\t}\n\t\/**\/\n\tcout&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;&quot;\\t\\t\\tPress ESC to exit ! &quot;;\n\twhile(getch() != ESC){}\n\n\n}\n\n\n\nvoid Car::showScore()\n{\n\tchar buf[5];\n\tsetfillstyle(1, BLUE );\n\tbar(maxx+110, 30, maxx+170, 50);\n\tsetcolor(WHITE);\n\tsprintf( buf, &quot;%.0f&quot;, score );\n\touttextxy(maxx+115, 40, buf );\n\t\/*level*\/\n\tsetfillstyle(1, BROWN );\n\tbar(maxx+110, 70, maxx+170, 90);\n\tsetcolor(WHITE);\n\tsprintf( buf, &quot;%d&quot;, level );\n\touttextxy(maxx+115, 80, buf );\n\t\/* high score *\/\n\tif(highScore != 0)\n\t{\n\t\tsetfillstyle(1, GREEN );\n\t\tbar(maxx+110, 110, maxx+170, 130);\n\t\tsetcolor(WHITE);\n\t\touttextxy(maxx+115, 120, highScorer);\n\t\tsetfillstyle(1, RED );\n\t\tbar(maxx+110, 150, maxx+170, 170);\n\t\tsetcolor(WHITE);\n\t\tsprintf( buf, &quot;%.0f&quot;, highScore);\n\t\touttextxy(maxx+115, 160, buf);\n\t}\n}\n\nvoid Car::readHighScore()\n{\n\tFILE *read;\n\tstruct highscore s;\n\tread = fopen(&quot;highscore.txt&quot;,&quot;r&quot;);\n\tif(read != NULL)\n\t{\n\t\tfread(&amp;s, sizeof(s), 1, read);\n\t\tfclose(read);\n\t\tstrcpy(highScorer, s.name);\n\t\thighScore = s.score;\n\t}\n\t\n}\n\n\n\nvoid main()\n{\n\tclrscr();\n\tfloat x,y,a,b,c;\n\tint gd=DETECT,gm;\n\tinitgraph(&amp;gd,&amp;gm,&quot;c:\\\\tc\\\\bgi&quot;);\n\tCar car;\n\tcar.initializeData();\n\tcar.initAnimate();\n\n}<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Multiplayer Car Racing Game Demo<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"338\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/Cpp_Multiplayer_Car_Racing_Game.gif\" alt=\"C++ Multiplayer Car Racing Game Demo\" class=\"wp-image-3126\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Keys Control for 2 Players Car Racing Game in C++ Use the &#8216;LEFT&#8217; and &#8216;RIGHT&#8217; arrows to control the right car. Use keys &#8216;A&#8217; and &#8216;D&#8217; to control the left car. Note: Use Turbo C Compiler to run the program with all graphics libraries loaded. C++ Multiplayer Car Racing Game Source Code code.cpp C++ Multiplayer &#8230; <a title=\"Build Multiplayer Car Racing Game Using C++ Graphics Program\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/\" aria-label=\"Read more about Build Multiplayer Car Racing Game Using C++ Graphics Program\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-3125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Build Multiplayer Car Racing Game Using C++ Graphics Program<\/title>\n<meta name=\"description\" content=\"Keys Control for 2 Players Car Racing Game in C++ Use the &#039;LEFT&#039; and &#039;RIGHT&#039; arrows to control the right car.Use keys &#039;A&#039; and &#039;D&#039; to control the left car.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Build Multiplayer Car Racing Game Using C++ Graphics Program\" \/>\n<meta property=\"og:description\" content=\"Keys Control for 2 Players Car Racing Game in C++ Use the &#039;LEFT&#039; and &#039;RIGHT&#039; arrows to control the right car.Use keys &#039;A&#039; and &#039;D&#039; to control the left car.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-13T11:04:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-13T11:04:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/Cpp_Multiplayer_Car_Racing_Game.gif\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Build Multiplayer Car Racing Game Using C++ Graphics Program","description":"Keys Control for 2 Players Car Racing Game in C++ Use the 'LEFT' and 'RIGHT' arrows to control the right car.Use keys 'A' and 'D' to control the left car.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/","og_locale":"en_US","og_type":"article","og_title":"Build Multiplayer Car Racing Game Using C++ Graphics Program","og_description":"Keys Control for 2 Players Car Racing Game in C++ Use the 'LEFT' and 'RIGHT' arrows to control the right car.Use keys 'A' and 'D' to control the left car.","og_url":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-09-13T11:04:37+00:00","article_modified_time":"2022-09-13T11:04:41+00:00","og_image":[{"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/Cpp_Multiplayer_Car_Racing_Game.gif","type":"","width":"","height":""}],"author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Build Multiplayer Car Racing Game Using C++ Graphics Program","datePublished":"2022-09-13T11:04:37+00:00","dateModified":"2022-09-13T11:04:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/"},"wordCount":68,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/","url":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/","name":"Build Multiplayer Car Racing Game Using C++ Graphics Program","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-09-13T11:04:37+00:00","dateModified":"2022-09-13T11:04:41+00:00","description":"Keys Control for 2 Players Car Racing Game in C++ Use the 'LEFT' and 'RIGHT' arrows to control the right car.Use keys 'A' and 'D' to control the left car.","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/build-multiplayer-car-racing-game-using-cpp-graphics-program\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Build Multiplayer Car Racing Game Using C++ Graphics Program"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=3125"}],"version-history":[{"count":1,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3125\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3125\/revisions\/3127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=3125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=3125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=3125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}