Pong program in C++.Score error












0












$begingroup$


I have written a code for my program of the game Pong, but there seems to be a problem.
When I run the program it works but the score is not displayed at the end even though I have given print for the score.
If possible, please correct me.
Given below is the code.



#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<time.h>
int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
char sym,pit[20];
int score=0,speed=10;
void ball()
{
time_t t;
xcenter=xnewcenter;
ycenter =ynewcenter;
xnewcenter=xcenter+ballspeedx;
ynewcenter=ycenter+ballspeedy;
setcolor(0);
setfillstyle(1,0);
fillellipse(xcenter,ycenter,radius,radius);
setfillstyle(1,11);
fillellipse(xnewcenter,ynewcenter,radius,radius);
setcolor(11);
if(xnewcenter-radius<0)
ballspeedx=-ballspeedx;
if(xnewcenter+radius>640)
ballspeedx=-ballspeedx;
if(ynewcenter-radius<0)
ballspeedy=-ballspeedy;
if(ynewcenter+radius>340)
{
if(xnewcenter>x1 && xnewcenter<x2)
{
ballspeedy=-ballspeedy;
srand((int)time(&t));
ballspeedx=random(3);
if(random(3)==1)
ballspeedx=-ballspeedx;
score++;
}
else
{
cleardevice();
outtextxy(300,150,"Game Over");
sprintf(pit,"Score %d",score);
outtextxy(300,170,"Press Q to Exit");
getch();
}
return;
}
delay(10);
}
void bat(char sym)
{
switch(sym)
{
case 'z' : if(x1>0)
{
x1old=x1;
x2old=x2;
x1=x1-5;
x2=x2-5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x1==0)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
case 'x' : if(x2<640)
{
x1old=x1;
x2old=x2;
x1=x1+5;
x2=x2+5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x2==640)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
}
}
void play()
{
while(inportb(0x60)!=16)
{
if(inportb(0x60)==44) bat('z');
if(inportb(0x60)==45) bat('x');
ball();
}
}
void initialize()
{
time_t t;
x1=rand();
x1=320;
y1=x1+30;
x2=x1+80;
y2=y1+40;
srand((int)time(&t));
xnewcenter=random(200);
ynewcenter=random(200);
ballspeedx=2;
srand((int)time(&t));
ballspeedy=random(3);
if(random(3)==0)
{
ballspeedx=-ballspeedx;
ballspeedy=-ballspeedy;
}
radius=20;
play();
}
void main()
{
int gd=0,gm;
initgraph(&gd,&gm,"C:\turboc3\bgi");
outtextxy(90,150,"Instractions:");
outtextxy(90,170,"Press Q to Exit");
outtextxy(90,190,"Press Z, X to move the bar");
outtextxy(90,210,"Press any key to continue.");
getch();
cleardevice();
initialize();
getch();
}









share|improve this question







New contributor




K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I have written a code for my program of the game Pong, but there seems to be a problem.
    When I run the program it works but the score is not displayed at the end even though I have given print for the score.
    If possible, please correct me.
    Given below is the code.



    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<dos.h>
    #include<graphics.h>
    #include<time.h>
    int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
    int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
    char sym,pit[20];
    int score=0,speed=10;
    void ball()
    {
    time_t t;
    xcenter=xnewcenter;
    ycenter =ynewcenter;
    xnewcenter=xcenter+ballspeedx;
    ynewcenter=ycenter+ballspeedy;
    setcolor(0);
    setfillstyle(1,0);
    fillellipse(xcenter,ycenter,radius,radius);
    setfillstyle(1,11);
    fillellipse(xnewcenter,ynewcenter,radius,radius);
    setcolor(11);
    if(xnewcenter-radius<0)
    ballspeedx=-ballspeedx;
    if(xnewcenter+radius>640)
    ballspeedx=-ballspeedx;
    if(ynewcenter-radius<0)
    ballspeedy=-ballspeedy;
    if(ynewcenter+radius>340)
    {
    if(xnewcenter>x1 && xnewcenter<x2)
    {
    ballspeedy=-ballspeedy;
    srand((int)time(&t));
    ballspeedx=random(3);
    if(random(3)==1)
    ballspeedx=-ballspeedx;
    score++;
    }
    else
    {
    cleardevice();
    outtextxy(300,150,"Game Over");
    sprintf(pit,"Score %d",score);
    outtextxy(300,170,"Press Q to Exit");
    getch();
    }
    return;
    }
    delay(10);
    }
    void bat(char sym)
    {
    switch(sym)
    {
    case 'z' : if(x1>0)
    {
    x1old=x1;
    x2old=x2;
    x1=x1-5;
    x2=x2-5;
    delay(10);
    setfillstyle(1,0);
    bar(x1old,y1,x2old,y2);
    setfillstyle(1,13);
    bar(x1,y1,x2,y2);
    }
    if(x1==0)
    {
    delay(10);
    setfillstyle(1,13);
    bar(x1,y1,x2,y2);
    }
    break;
    case 'x' : if(x2<640)
    {
    x1old=x1;
    x2old=x2;
    x1=x1+5;
    x2=x2+5;
    delay(10);
    setfillstyle(1,0);
    bar(x1old,y1,x2old,y2);
    setfillstyle(1,13);
    bar(x1,y1,x2,y2);
    }
    if(x2==640)
    {
    delay(10);
    setfillstyle(1,13);
    bar(x1,y1,x2,y2);
    }
    break;
    }
    }
    void play()
    {
    while(inportb(0x60)!=16)
    {
    if(inportb(0x60)==44) bat('z');
    if(inportb(0x60)==45) bat('x');
    ball();
    }
    }
    void initialize()
    {
    time_t t;
    x1=rand();
    x1=320;
    y1=x1+30;
    x2=x1+80;
    y2=y1+40;
    srand((int)time(&t));
    xnewcenter=random(200);
    ynewcenter=random(200);
    ballspeedx=2;
    srand((int)time(&t));
    ballspeedy=random(3);
    if(random(3)==0)
    {
    ballspeedx=-ballspeedx;
    ballspeedy=-ballspeedy;
    }
    radius=20;
    play();
    }
    void main()
    {
    int gd=0,gm;
    initgraph(&gd,&gm,"C:\turboc3\bgi");
    outtextxy(90,150,"Instractions:");
    outtextxy(90,170,"Press Q to Exit");
    outtextxy(90,190,"Press Z, X to move the bar");
    outtextxy(90,210,"Press any key to continue.");
    getch();
    cleardevice();
    initialize();
    getch();
    }









    share|improve this question







    New contributor




    K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I have written a code for my program of the game Pong, but there seems to be a problem.
      When I run the program it works but the score is not displayed at the end even though I have given print for the score.
      If possible, please correct me.
      Given below is the code.



      #include<stdio.h>
      #include<ctype.h>
      #include<conio.h>
      #include<stdlib.h>
      #include<dos.h>
      #include<graphics.h>
      #include<time.h>
      int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
      int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
      char sym,pit[20];
      int score=0,speed=10;
      void ball()
      {
      time_t t;
      xcenter=xnewcenter;
      ycenter =ynewcenter;
      xnewcenter=xcenter+ballspeedx;
      ynewcenter=ycenter+ballspeedy;
      setcolor(0);
      setfillstyle(1,0);
      fillellipse(xcenter,ycenter,radius,radius);
      setfillstyle(1,11);
      fillellipse(xnewcenter,ynewcenter,radius,radius);
      setcolor(11);
      if(xnewcenter-radius<0)
      ballspeedx=-ballspeedx;
      if(xnewcenter+radius>640)
      ballspeedx=-ballspeedx;
      if(ynewcenter-radius<0)
      ballspeedy=-ballspeedy;
      if(ynewcenter+radius>340)
      {
      if(xnewcenter>x1 && xnewcenter<x2)
      {
      ballspeedy=-ballspeedy;
      srand((int)time(&t));
      ballspeedx=random(3);
      if(random(3)==1)
      ballspeedx=-ballspeedx;
      score++;
      }
      else
      {
      cleardevice();
      outtextxy(300,150,"Game Over");
      sprintf(pit,"Score %d",score);
      outtextxy(300,170,"Press Q to Exit");
      getch();
      }
      return;
      }
      delay(10);
      }
      void bat(char sym)
      {
      switch(sym)
      {
      case 'z' : if(x1>0)
      {
      x1old=x1;
      x2old=x2;
      x1=x1-5;
      x2=x2-5;
      delay(10);
      setfillstyle(1,0);
      bar(x1old,y1,x2old,y2);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      if(x1==0)
      {
      delay(10);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      break;
      case 'x' : if(x2<640)
      {
      x1old=x1;
      x2old=x2;
      x1=x1+5;
      x2=x2+5;
      delay(10);
      setfillstyle(1,0);
      bar(x1old,y1,x2old,y2);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      if(x2==640)
      {
      delay(10);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      break;
      }
      }
      void play()
      {
      while(inportb(0x60)!=16)
      {
      if(inportb(0x60)==44) bat('z');
      if(inportb(0x60)==45) bat('x');
      ball();
      }
      }
      void initialize()
      {
      time_t t;
      x1=rand();
      x1=320;
      y1=x1+30;
      x2=x1+80;
      y2=y1+40;
      srand((int)time(&t));
      xnewcenter=random(200);
      ynewcenter=random(200);
      ballspeedx=2;
      srand((int)time(&t));
      ballspeedy=random(3);
      if(random(3)==0)
      {
      ballspeedx=-ballspeedx;
      ballspeedy=-ballspeedy;
      }
      radius=20;
      play();
      }
      void main()
      {
      int gd=0,gm;
      initgraph(&gd,&gm,"C:\turboc3\bgi");
      outtextxy(90,150,"Instractions:");
      outtextxy(90,170,"Press Q to Exit");
      outtextxy(90,190,"Press Z, X to move the bar");
      outtextxy(90,210,"Press any key to continue.");
      getch();
      cleardevice();
      initialize();
      getch();
      }









      share|improve this question







      New contributor




      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have written a code for my program of the game Pong, but there seems to be a problem.
      When I run the program it works but the score is not displayed at the end even though I have given print for the score.
      If possible, please correct me.
      Given below is the code.



      #include<stdio.h>
      #include<ctype.h>
      #include<conio.h>
      #include<stdlib.h>
      #include<dos.h>
      #include<graphics.h>
      #include<time.h>
      int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
      int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
      char sym,pit[20];
      int score=0,speed=10;
      void ball()
      {
      time_t t;
      xcenter=xnewcenter;
      ycenter =ynewcenter;
      xnewcenter=xcenter+ballspeedx;
      ynewcenter=ycenter+ballspeedy;
      setcolor(0);
      setfillstyle(1,0);
      fillellipse(xcenter,ycenter,radius,radius);
      setfillstyle(1,11);
      fillellipse(xnewcenter,ynewcenter,radius,radius);
      setcolor(11);
      if(xnewcenter-radius<0)
      ballspeedx=-ballspeedx;
      if(xnewcenter+radius>640)
      ballspeedx=-ballspeedx;
      if(ynewcenter-radius<0)
      ballspeedy=-ballspeedy;
      if(ynewcenter+radius>340)
      {
      if(xnewcenter>x1 && xnewcenter<x2)
      {
      ballspeedy=-ballspeedy;
      srand((int)time(&t));
      ballspeedx=random(3);
      if(random(3)==1)
      ballspeedx=-ballspeedx;
      score++;
      }
      else
      {
      cleardevice();
      outtextxy(300,150,"Game Over");
      sprintf(pit,"Score %d",score);
      outtextxy(300,170,"Press Q to Exit");
      getch();
      }
      return;
      }
      delay(10);
      }
      void bat(char sym)
      {
      switch(sym)
      {
      case 'z' : if(x1>0)
      {
      x1old=x1;
      x2old=x2;
      x1=x1-5;
      x2=x2-5;
      delay(10);
      setfillstyle(1,0);
      bar(x1old,y1,x2old,y2);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      if(x1==0)
      {
      delay(10);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      break;
      case 'x' : if(x2<640)
      {
      x1old=x1;
      x2old=x2;
      x1=x1+5;
      x2=x2+5;
      delay(10);
      setfillstyle(1,0);
      bar(x1old,y1,x2old,y2);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      if(x2==640)
      {
      delay(10);
      setfillstyle(1,13);
      bar(x1,y1,x2,y2);
      }
      break;
      }
      }
      void play()
      {
      while(inportb(0x60)!=16)
      {
      if(inportb(0x60)==44) bat('z');
      if(inportb(0x60)==45) bat('x');
      ball();
      }
      }
      void initialize()
      {
      time_t t;
      x1=rand();
      x1=320;
      y1=x1+30;
      x2=x1+80;
      y2=y1+40;
      srand((int)time(&t));
      xnewcenter=random(200);
      ynewcenter=random(200);
      ballspeedx=2;
      srand((int)time(&t));
      ballspeedy=random(3);
      if(random(3)==0)
      {
      ballspeedx=-ballspeedx;
      ballspeedy=-ballspeedy;
      }
      radius=20;
      play();
      }
      void main()
      {
      int gd=0,gm;
      initgraph(&gd,&gm,"C:\turboc3\bgi");
      outtextxy(90,150,"Instractions:");
      outtextxy(90,170,"Press Q to Exit");
      outtextxy(90,190,"Press Z, X to move the bar");
      outtextxy(90,210,"Press any key to continue.");
      getch();
      cleardevice();
      initialize();
      getch();
      }






      c++






      share|improve this question







      New contributor




      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 13 mins ago









      K AbishekK Abishek

      11




      11




      New contributor




      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      K Abishek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          K Abishek is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212305%2fpong-program-in-c-score-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          K Abishek is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          K Abishek is a new contributor. Be nice, and check out our Code of Conduct.













          K Abishek is a new contributor. Be nice, and check out our Code of Conduct.












          K Abishek is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212305%2fpong-program-in-c-score-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Список кардиналов, возведённых папой римским Каликстом III

          Deduzione

          Mysql.sock missing - “Can't connect to local MySQL server through socket”