⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bounce.c

📁 该程序完善了《UnixLinux编程实践教程》中的弹球功能
💻 C
字号:
/*  mybounce 1.0	
 *	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
 *                      a: move left              d: move right
 *			q: quit                   w: win
 *
 *	blocks on read, but timer tick sends SIGALRM caught by ball_move
 *	build:   cc mybounce.c set_ticker.c -lcurses -o mybounce
 */

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

struct ppball the_ball ;

int targets[RIGHT_EDGE-LEFT_EDGE+1];
int boardl,boardr,ts;
int score=0;
/**  the main loop  **/

void set_up();
void wrap_up();

int main()
{
    int i ,c ,c2=1;

    for(;c2!='q'&&c2!='Q';)
       {
        c2=1;
        clear();
	initscr();
	noecho();
	crmode();

        boardl=LEFT_EDGE ;
        boardr=LEFT_EDGE+8 ;
        ts=RIGHT_EDGE-LEFT_EDGE+1 ;
        for(i=0;i<=RIGHT_EDGE-LEFT_EDGE;i++)targets[i]=1;

        for(i=TOP_ROW;i<=BOT_ROW;i++)mvaddch( i, LEFT_EDGE-1, WALL);
        for(i=TOP_ROW;i<=BOT_ROW;i++)mvaddch( i, RIGHT_EDGE+1, WALL);
        for(i=LEFT_EDGE-1;i<=RIGHT_EDGE+1;i++)mvaddch( TOP_ROW-1, i, BOARD);

        for(i=LEFT_EDGE;i<=RIGHT_EDGE;i++)mvaddch( TOP_ROW, i, TARGET);
        for(i=boardl;i<=boardr;i++)mvaddch( BOT_ROW+1, i, BOARD );
        move(LINES-1,COLS-1);
        refresh();

	set_up();

	while ( ( c = getchar()) != 'q' && ts>0 ){
		     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++;
                else if ( c == 'w' ) ts=0;
		else if ( c == 'a' ) {if(boardl>LEFT_EDGE)
                                     {mvaddch( BOT_ROW+1, --boardl, BOARD );
                                      mvaddch( BOT_ROW+1, boardr--, BLANK );
		                      move(LINES-1,COLS-1);
		                      refresh();}}
		else if ( c == 'd' ) if(boardr<RIGHT_EDGE)
                                     {mvaddch( BOT_ROW+1, ++boardr, BOARD );
                                      mvaddch( BOT_ROW+1, boardl++, BLANK );
		                      move(LINES-1,COLS-1);
		                      refresh();}
                }
        clear();
        move((LINES-1)/2,(COLS-1)/2);
        if(ts==0){addstr("Perfect!");move(LINES-1,0);}
        else if(ts<0){if(score>10)
        {
        		addstr("A good score! ");
                      	fflush(stdout);
        		move(LINES-1,0);
        		refresh();
        }
        else
                {
        		addstr("A little low score,come on! ");
                      	fflush(stdout);
        		move(LINES-1,0);
        		refresh();
        }

        		
        		}
        else {addstr("You quit!");move(LINES-2,0);}
        addstr(" Q for Exit, ohter for restart.\n");
        move(LINES-1,COLS-1);
         refresh();
        c2=getchar();
        }
        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 = 1  ;
	the_ball.x_dir = 1  ;
	the_ball.symbol = DFL_SYMBOL ;


	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 */
        if(ts<=0)return;
}

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 ){
		if(targets[x_cur-LEFT_EDGE]==1 && y_cur==TOP_ROW )( y_cur, x_cur, BLANK );
		else mvaddch( y_cur, x_cur, BLANK );
		mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
		bounce_or_lose( &the_ball );
                if(targets[the_ball.x_pos-LEFT_EDGE]==0)mvaddch( TOP_ROW, the_ball.x_pos, BLANK );
		move(LINES-1,COLS-1);
		refresh();
	}
	signal( SIGALRM, ball_move);		/* for unreliable systems */
        if(ts<=0)return;
}

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

        if( bp->y_pos==TOP_ROW+1 && targets[bp->x_pos]==1 ){
                targets[bp->x_pos-LEFT_EDGE]=0;
                score++;
                ts--;
		bp->y_dir = 1 ; 
		return_val = 1 ;
	}
	if ( bp->y_pos == TOP_ROW ){
		bp->y_dir = 1 ; 
		return_val = 1 ;
	} else if ( bp->y_pos == BOT_ROW ){
            if( boardl<=bp->x_pos && boardr>=bp->x_pos )
		{bp->y_dir = -1 ;
	       	 return_val = 1;}
            else {ts=-1;return 2;}
	}
	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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -