Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pong in UNIX writen in C code..

Posted on 2006-05-11
2
Medium Priority
?
1,439 Views
Last Modified: 2012-05-05
Hi experts,

Basically I am trying to implement a simple pong game in a unix shell. I have the code for the ball bouncing around the screen(walls), however I have found it difficult to make the paddle which the user can use the keyboard keys (i.e. q,a) to move the paddle and, well you know its the old pong game!!

Screen should look like:

XXXXXXXXXXXXXXX - Walls
X
X                          X
X     ball - o           X - paddle
X                          X
X
XXXXXXXXXXXXXXX


/*  ppong.c
 *      bounce a character (default is 'o') around the screen
 *      defined by some parameters
 *
 *      user input:       s slow down x component, S: slow y component
 *                   f speed up x component,  F: speed y component
 *                  Q quit
 *
 *      blocks on read, but timer tick sends SIGALRM caught by ball_move
 *      build:   cc ppong.c set_ticker.c -lcurses -o ppong
 */

#include      <curses.h>
#include      <signal.h>
#include      "ppong.h"

struct ppball the_ball ;

/**  the main loop  **/

void set_up();
void wrap_up();

int main()
{
      int      c;

      set_up();

      while ( ( c = getchar()) != 'Q' ){
            if ( c == 'f' )           the_ball.x_ttm--;
            else if ( c == 's' ) the_ball.x_ttm++;
            else if ( c == 'F' ) the_ball.y_ttm--;
            else if ( c == 'S' ) the_ball.y_ttm++;
      }

      wrap_up();
}


void set_up()
/*
 *      init structure and other stuff
 */
{
      void      ball_move(int);

      the_ball.y_pos = Y_INIT;
      the_ball.x_pos = X_INIT;
      the_ball.y_ttg = the_ball.y_ttm = Y_TTM ;
      the_ball.x_ttg = the_ball.x_ttm = X_TTM ;
      the_ball.y_dir = 10  ;
      the_ball.x_dir = 10 ;
      the_ball.symbol = DFL_SYMBOL ;

      initscr();
      noecho();
      crmode();

      signal( SIGINT , SIG_IGN );
      mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol  );
      refresh();
      
      signal( SIGALRM, ball_move );
      set_ticker( 1000 / TICKS_PER_SEC );      /* send millisecs per tick */
}

void wrap_up()
{

      set_ticker( 0 );
      endwin();            /* put back to normal      */
}


void ball_move(int signum)
{
      int      y_cur, x_cur, moved;

      signal( SIGALRM , SIG_IGN );            /* dont get caught now       */
      y_cur = the_ball.y_pos ;            /* old spot            */
      x_cur = the_ball.x_pos ;
      moved = 0 ;

      if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){
            the_ball.y_pos += the_ball.y_dir ;      /* move      */
            the_ball.y_ttg = the_ball.y_ttm  ;      /* reset*/
            moved = 1;
      }

      if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){
            the_ball.x_pos += the_ball.x_dir ;      /* move      */
            the_ball.x_ttg = the_ball.x_ttm  ;      /* reset*/
            moved = 1;
      }

      if ( moved ){
            mvaddch( y_cur, x_cur, BLANK );
            mvaddch( y_cur, x_cur, BLANK );
            mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
            bounce_or_lose( &the_ball );
            move(LINES-1,COLS-1);
            refresh();
      }
      signal( SIGALRM, ball_move);            /* for unreliable systems */

}

int bounce_or_lose(struct ppball *bp)
{
      int      return_val = 0 ;

      if ( bp->y_pos == TOP_ROW ){
            bp->y_dir = 1 ;
            return_val = 1 ;
      } else if ( bp->y_pos == BOT_ROW ){
            bp->y_dir = -1 ;
                   return_val = 1;
      }
      if ( bp->x_pos == LEFT_EDGE ){
            bp->x_dir = 1 ;
                   return_val = 1 ;
      } else if ( bp->x_pos == RIGHT_EDGE ){
            bp->x_dir = -1;
                   return_val = 1;
      }

      return return_val;
}




/* ppong.h                  */

/* some settings for the game      */

#define      BLANK            ' '
#define      DFL_SYMBOL      'o'
#define      TOP_ROW            5
#define      BOT_ROW       20
#define      LEFT_EDGE      10
#define      RIGHT_EDGE      70
#define      X_INIT            10            /* starting col            */
#define      Y_INIT            10            /* starting row            */
#define      TICKS_PER_SEC      50            /* affects speed      */

#define      X_TTM            5
#define      Y_TTM            8

/** the ping pong ball **/

struct ppball {
            int      y_pos, x_pos,
                  y_ttm, x_ttm,
                  y_ttg, x_ttg,
                  y_dir, x_dir;
            char      symbol ;

      } ;






#include      <stdio.h>
#include        <sys/time.h>
#include        <signal.h>

/*
 *      set_ticker.c
 *          set_ticker( number_of_milliseconds )
 *                   arranges for the interval timer to issue
 *                   SIGALRM's at regular intervals
 *          returns -1 on error, 0 for ok
 *
 *      arg in milliseconds, converted into micro seoncds
 */


set_ticker( n_msecs )
{
        struct itimerval new_timeset;
        long    n_sec, n_usecs;

        n_sec = n_msecs / 1000 ;
        n_usecs = ( n_msecs % 1000 ) * 1000L ;

        new_timeset.it_interval.tv_sec  = n_sec;        /* set reload  */
        new_timeset.it_interval.tv_usec = n_usecs;      /* new ticker value */
        new_timeset.it_value.tv_sec     = n_sec  ;      /* store this   */
        new_timeset.it_value.tv_usec    = n_usecs ;     /* and this     */

      return setitimer(ITIMER_REAL, &new_timeset, NULL);
}


I have looked at similar pong code, however being in Java it is a little hard to reference.

This one is a brain tester.. Any help would be great..My first interactive game..


0
Comment
Question by:Omer_85
2 Comments
 
LVL 5

Accepted Solution

by:
Dragon_Krome earned 800 total points
ID: 16659098
I'm kinda rusty at curses programming, but here's a worms game with source code. You can see how the keyboard is handled there:

http://www.paulgriffiths.net/program/c/curworm.html
0
 

Author Comment

by:Omer_85
ID: 16775212
Question was not answered, or and helpful tips given at all.

As I only had one comment they will get the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Six Sigma Control Plans
Screencast - Getting to Know the Pipeline

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question