Advertisement

Pong in ASCII

Started by July 03, 2001 11:10 AM
1 comment, last by whereiswez 23 years, 5 months ago
Hello yet again .. Using C++ Command Line ? .. and only conio.h and iostream.h .. it should be possible to write Pong in ASCII right ? kay .. in pascal there was something we used like (if not keypressed) do such .. how would i do that in c++ ? .. so that the ball bounces all over, while receiving keystrokes or not ? i am trying to keep the libraries to a minium tho, coz the more there are the more complexed it seems to get concerning repetitive definitions which it doesnt seem to like .. thanx
Yup you can do pong with iostream.h and conio.h
I recall for text-based games, compilers like DJGPP and Borland work best. Here it is:

char KeyPressed;
if ( kbhit () )
KeyPressed = getch();

kbhit() checks the keyboard buffer if there is a button press waiting to be handled. getch() takes that and puts it into a char variable. kbhit () returns 1 (true) if the user hit a key, and 0 if the user didn''t (before getch() was last called).

Good luck!
Advertisement
THANKS ! btw ! :D

>

!!! ---> K ! .. I'm stuck again !

BC++ Command Line, NO TASM MASM OR ASM .. though ..

I have looked about for posts on multiple key down handling ..
I'm not too sure on how they work, and also the posts I've read concern Direct this and that, which I'm not using ..

Can somebody give me a bit more than a hint on how to make the game so that :

Player 1 (left paddle) can continuously move up, while player 2 (right paddle) continuously moves up, without any player letting go of the "UP" keys ?

Also .. ASCII GFX wise, whats the better way to work out the whole collision, deflection thing ? ..

I've got mine to work (The whole if hit top center or low, and from direction, then reverse direction if this and that), but I think possibly if I could substitute actual calculated formulas, the game would be a bit more realistic. (Then again, I'm working ASCII *LOL*) .. I'm more after the concept, and equations ..

And YES .. my maths is lost and further more a bit off but the PONG game I've written so far doesn't use hectic maths but works, probably slower yes, but i'd like to make it work better.

If anyone needs to see my messy bit of code to try and figure out what I mean about it working, but not as well as I'd like it to, here it is :

  /*  to do :  two players .. need simultaneous keypress function  computer ai  if ball goes to extent, then set score, regame !  prod menu system  scoring system ? ..  ball speed / game speed?  add formulas for curve balls !  random flying things to collect, exp pad, speed ball, slow ball, bigger ball ?*/#include <conio.h>#include <iostream.h>#include <time.h>#include <stdlib.h>struct myballpos{  int x;  int y;} ballpos;struct mypaddle{  int x;  int y;  int ex;} lpad;struct mypaddle rpad;void wait (int msec){  clock_t endwait;  endwait = clock () + (msec * CLK_TCK/1000);  while (clock() < endwait) {}};void drawball(int x, int y, int maskoff){  gotoxy(x,y);  switch (maskoff)  {    case 0 :      cout << " ";    break;    default :      cout << "*";  };};void drawpad(int x, int y, int ex, int maskoff){  for (int i=y-ex;i<=y+ex;i++)  {    gotoxy(x,i);    switch (maskoff)    {      case 0 :        cout << " ";      break;      default :        cout << "H";    };  };};int main(){  srand(time(NULL));  char ch;  int gamespeed = 50;  int ai = 1;  int strchmax = 5;  int strch = rand() % strchmax;  int strchc = strch;  int endgame = 0;  int l2r = rand() % 1; // from left to right  int b2t = rand() % 1; // from bottom to top/*  init ball , l pad, r pad positions*/  ballpos.x = 40;  ballpos.y = 12;  lpad.x = 15;  lpad.y = 12;  lpad.ex = 3;  rpad.x = 65;  rpad.y = 12;  rpad.ex = 3;  clrscr();/*  begin the game loop ! :D*/  while (endgame==0)  {/*  if AI enabled then GAME ON WOOHOO ! :D*/    if (ai == 1)    {      drawpad(rpad.x, rpad.y, rpad.ex, 0);      if ((ballpos.y > rpad.ex) && (ballpos.y <= 24-rpad.ex))        rpad.y = ballpos.y;    };    drawpad(lpad.x, lpad.y, lpad.ex, 1);    drawpad(rpad.x, rpad.y, rpad.ex, 1);/*  check which keys are pressed, affect the assigned objects movement  check extents of pad movement*/    if (kbhit())    {      ch = getch();      switch (ch)      {        case '`' :        {          endgame = 1;        };        break;        case '+' :        {          if (gamespeed > 0)            gamespeed = gamespeed -1;        };        break;        case '-' :        {          if (gamespeed < 50)            gamespeed = gamespeed +1;        };        break;        case 's' :        {          if (lpad.y-lpad.ex > 1)          {            drawpad(lpad.x, lpad.y, lpad.ex, 0);            lpad.y = lpad.y-1;            drawpad(lpad.x, lpad.y, lpad.ex, 1);          };        };        break;        case 'x' :        {          if (lpad.y+lpad.ex < 24)          {            drawpad(lpad.x, lpad.y, lpad.ex, 0);            lpad.y = lpad.y+1;            drawpad(lpad.x, lpad.y, lpad.ex, 1);          };        };        break;        case '5' :        {          if (rpad.y-rpad.ex > 1)          {            drawpad(rpad.x, rpad.y, rpad.ex, 0);            rpad.y = rpad.y-1;            drawpad(rpad.x, rpad.y, rpad.ex, 1);          };        };        break;        case '2' :        {          if (rpad.y+rpad.ex < 24)          {            drawpad(rpad.x, rpad.y, rpad.ex, 0);            rpad.y = rpad.y+1;            drawpad(rpad.x, rpad.y, rpad.ex, 1);          };        };        break;      };    };    drawball(ballpos.x,ballpos.y,0);/*  check the stretch of the ball distance to follow  check the distance the ball has followed to the stretch  check the direction of the ball from top to bottom or reversed  reverse the direction*/    if (strchc > 0)    {      strchc = strchc -1;    }    else    if ((strchc == 0) && (strch != 0))    {      strchc = strch;      if (ballpos.y < 2)      {        b2t = 0;      }      else      if (ballpos.y > 23)      {        b2t = 1;      };      switch (b2t)      {        case 0 :          ballpos.y = ballpos.y +1;        break;        case 1 :          ballpos.y = ballpos.y -1;        break;      };    };/*  check if the ball reaches boundary limits  change direction from left to right  reverse the direction*/    if (ballpos.x > 79)    {      l2r = 0;    }    else    if (ballpos.x < 2)    {      l2r = 1;    }/*  check if ball hits LEFT paddle ! :D*/    if ((ballpos.x == lpad.x) && (ballpos.y > lpad.y-lpad.ex-1) && (ballpos.y < lpad.y+lpad.ex+1))    {      l2r = 1;// if hit TOP      if ( ballpos.y < (lpad.y-lpad.ex) + (((lpad.ex*2)+1)/3) )      {// if going DOWN        if (b2t == 0)        {          if ((strch <= strchmax) && (strch > 1))            strch = strch -1;          else          if (strch < 1)            strch = strchmax;        }        else// if going UP        if (b2t == 1)        {          if ((strch < strchmax) && (strch >= 1))          {            strch = strch +1;            b2t = 0;          }          else          if ((strch == strchmax) || (strch == 0))          {            if (strch == 0)              strch = strchmax;            b2t = 0;          };        };      }      else// if hit BOTTOM      if ( ballpos.y > (lpad.y+lpad.ex) - (((lpad.ex*2)+1)/3) )      {// if going DOWN        if (b2t == 0)        {          if ((strch < strchmax) && (strch >= 1))          {            strch = strch +1;            b2t = 1;          }          else          if ((strch == strchmax) || (strch == 0))          {            if (strch == 0)              strch = strchmax;            b2t = 1;          };        }        else// if going UP        if (b2t == 1)        {          if ((strch < strchmax) && (strch > 1))            strch = strch -1;          else          if (strch < 1)            strch = strchmax;        }      };    };/*  check if ball hits RIGHT paddle ! :D*/    if ((ballpos.x == rpad.x) && (ballpos.y > rpad.y-rpad.ex-1) && (ballpos.y < rpad.y+rpad.ex+1))    {      l2r = 0;// if hit TOP      if ( ballpos.y < (rpad.y-rpad.ex) + (((rpad.ex*2)+1)/3) )      {// if going DOWN        if (b2t == 0)        {          if ((strch <= strchmax) && (strch > 1))            strch = strch -1;          else          if (strch < 1)            strch = strchmax;        }        else// if going UP        if (b2t == 1)        {          if ((strch < strchmax) && (strch >= 1))          {            strch = strch +1;            b2t = 0;          }          else          if ((strch == strchmax) || (strch == 0))          {            if (strch == 0)              strch = strchmax;            b2t = 0;          };        };      }      else// if hit BOTTOM      if ( ballpos.y > (rpad.y+rpad.ex) - (((rpad.ex*2)+1)/3) )      {// if going DOWN        if (b2t == 0)        {          if ((strch < strchmax) && (strch >= 1))          {            strch = strch +1;            b2t = 1;          }          else          if ((strch == strchmax) || (strch == 0))          {            if (strch == 0)              strch = strchmax;            b2t = 1;          };        }        else// if going UP        if (b2t == 1)        {          if ((strch < strchmax) && (strch > 1))            strch = strch -1;          else          if (strch < 1)            strch = strchmax;        }      };    };/*  increase the ball position from left to right or reverse*/    switch (l2r)    {      case 0 :        ballpos.x = ballpos.x -1;      break;      case 1 :        ballpos.x = ballpos.x +1;      break;    };/*  statistics check*/    gotoxy(1,25);    cout << "b.x:" << ballpos.x << " ";    cout << "b.y:" << ballpos.y << " ";    cout << "sc:" << strchc << " ";    cout << "s:" << strch << " ";    cout << "l.x:" << lpad.x << " ";    cout << "l.y:" << lpad.y << " ";    cout << "l.ex:" << lpad.ex << " ";    cout << "r.x:" << rpad.x << " ";    cout << "r.y:" << rpad.y << " ";    cout << "r.ex:" << rpad.ex << " ";    cout << "gs:" << gamespeed << " ";    wait(0);    drawball(ballpos.x,ballpos.y,1);    wait(gamespeed);  };  return 0;}  


Edited by - whereiswez on July 7, 2001 12:33:40 PM

This topic is closed to new replies.

Advertisement