In Linux under the C language using the signal mechanism to complete a pinball game, the code is as follows:
Bounce.h
/* bounce.h*//* Some settings for the game*/#defineBLANK ' #defineDFL_SYMBOL ' o ' #defineTOP_ROW5 #definebot_row 20# defineleft_edge10#defineright_edge70#definex_init10/* starting col*/#defineY_INIT10/* Starting row*/#defineTICKS_ per_sec50/* affects speed*/#defineX_TTM5 #definey_ttm8/** the ping pong ball **/struct ppball {inty_pos, X_pos,y_ttm, X_tt M,Y_TTG, X_ttg,y_dir, X_dir;charsymbol;} ;
Pppaddle.h
struct Pppaddle{int pad_top;int pad_bot;int pad_col;char Pad_char;}; void Paddle_init (struct pppaddle * p); int paddle_up (struct pppaddle * p); int paddle_down (struct pppaddle * p); int Paddle_c ontact (int x,int y,struct pppaddle * p);
Pong.c
#include <curses.h> #include <signal.h> #include "bounce.h" #include <stdlib.h> #include "pppaddle.h" #define Maxspeed 10#define maxmodify 3struct ppball the_ball;struct pppaddle the_paddle;int ball_left = 3;/** the main lo Op **/void set_up (); void wrap_up (); void Draw_wall (); void Draw_paddle (); int main () {int c;set_up (); while (Ball_left > 0 && (c = GetChar ()) = ' Q ') {if (c = = ' J ') {if (Paddle_down (&the_paddle) = = 1) {Move (the_paddle.pad_top-1, t He_paddle.pad_col); Addch ("); Move (The_paddle.pad_bot, The_paddle.pad_col); ADDCH (' # ');}} if (c = = ' K ') {if (paddle_up (&the_paddle) = = 1) {Move (The_paddle.pad_bot + 1, the_paddle.pad_col); ADDCH ("); Move (the_ Paddle.pad_top, The_paddle.pad_col); ADDCH (' # ');}}} Wrap_up ();} void Set_up ()/* *init structure and other stuff */{void ball_move (int);p addle_init (&the_paddle); the_ball.y_pos = Y_ Init;the_ball.x_pos = X_INIT;THE_BALL.Y_TTG = The_ball.y_ttm = rand ()% MAXSPEED;THE_BALL.X_TTG = The_ball.x_ttm = rand () % Maxspeed; the_ball.y_dir = 1;the_ball.x_dir = 1;the_ball.symbol = Dfl_symbol;initscr (); NoEcho (); Crmode (); Signal (SIGINT, SIG_ IGN); Mvaddch (The_ball.y_pos, The_ball.x_pos, The_ball.symbol);d raw_wall (); 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 = t He_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-&G T;y_dir = 1;change_the_speed (); return_val = 1;} else if (Bp->y_pos = = Bot_row) {Bp->y_dir = -1;change_the_speed (); return_val = 1;} if (Bp->x_pos = = Left_edge) {Bp->x_dir = 1;change_the_speed (); return_val = 1;} else if (Paddle_contact (bp->x_pos , Bp->y_pos, &the_paddle)) {Bp->x_dir = -1;change_the_speed (); return_val = 1;} if (The_ball.x_pos = =) {Move (The_ball.y_pos,the_ball.x_pos); Addch ("); Move (LINES-1, COLS-1); refresh (); bp->x_ pos = 10;bp->y_pos = 10;ball_left--;if (Ball_left = = 0) {clear (); exit (0);}} return return_val;} void Draw_wall () {int i;for (i = 9; I <=; i++) {Move (4, I); ADDCH (' * ');} for (i = 4; I <=; i++) {Move (I, 9); ADDCH (' * ');} for (i = 9; I <=; i++) {Move (+, i); ADDCH (' * ');} for (i = the_paddle.pad_top; I <= The_paddle.pad_bot; i++) {Move (I, The_paddle.pad_col); ADDCH (' # ');}} void Draw_paddle () {int i;for (i = the_paddle.pad_top; I <= The_paddle.pad_bot; i++) {Move (I, the_paddle.pad_col); addch ('#');}} void Change_the_speed () {int add_or_delete = rand ()% maxspeed;if (add_or_delete% 2 = = 0) Add_or_delete = 1;elseadd_or_del Ete = -1;int Speed_change = rand ()% maxmodify;if (The_ball.x_ttm + (Speed_change * add_or_delete) > 0) The_ball.x_ttm = The_ball.x_ttm + (Speed_change * add_or_delete); add_or_delete = rand ()% maxspeed;if (add_or_delete% 2 = = 0) add_or_delet E = 1;elseadd_or_delete = -1;speed_change = rand ()% maxmodify;if (The_ball.y_ttm + (Speed_change * add_or_delete) > 0) The_ball.y_ttm = The_ball.y_ttm + (Speed_change * add_or_delete);}
Pppaddle.c
#include "pppaddle.h" void paddle_init (struct pppaddle * p) {P->pad_char = ' # ';p->pad_col = 71;p->pad_bot = 16;p-& Gt;pad_top = 11;} int paddle_up (struct Pppaddle * p) {if (p->pad_top! = 5) {p->pad_top--;p->pad_bot--;return 1;} return 0;//int i;//for (i = the_paddle.pad_top; I <= the_paddle.pad_bot;i++)//{//move (i,the_paddle.pad_col);//addch (' # ');//}}int paddle_down (struct Pppaddle * p) {if (P->pad_bot! =) {p->pad_top++;p->pad_bot++;return 1;} return 0;//int i;//for (i = the_paddle.pad_top; I <= the_paddle.pad_bot;i++)//{//move (i,the_paddle.pad_col);//addch (' # ');//}}int paddle_contact (int x,int y,struct Pppaddle * p) {if (x = = && y <= p->pad_bot && y ; = p->pad_top) return 1;elsereturn 0;}
Set_ticker.c
#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 in Tervals * returns-1 On Error, 0 for OK * * arg in milliseconds, converted into micro Seoncds */void set_tic Ker (int n_msecs) {struct Itimerval new_timeset; Long N_sec, n_usecs; N_sec = n_msecs/1000; N_usecs = (n_msecs%) * 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 */setitimer (Itimer_real, &new_timeset, NULL);}
Linux C Project:p Ong