标签:
bounce2d2.c
1 /* 2 * bounce2d 1.0 3 * bounce a character (default is ‘o‘) around the screen 4 * defined by some parameters 5 * user input: s slow down x component, S: slow y component 6 * f speed up x component, F: speed y component 7 * Q quit 8 * blocks on read, but timer tick sends SIGALRM caught by ball_move 9 * build: cc bounce2d.c set_ticker.c -lcurses -o bounce2d 10 */ 11 #include <curses.h> 12 #include <string.h> 13 #include <signal.h> 14 #include "bounce.h" 15 16 struct ppball the_ball; 17 18 /** the main loop **/ 19 int flap_pos = RIGHT_EDGE / 2 - LEFT_EDGE; 20 int old_pos; 21 void set_up(); 22 void wrap_up(); 23 void move_flap(); 24 int bounce_or_lose(struct ppball *); 25 26 int main() 27 { 28 printf("LEFT_EDGE: %d, RIGHT_EDGE: %d\n",LEFT_EDGE, RIGHT_EDGE); 29 int c; 30 set_up(); 31 while (((c = getchar())) != ‘Q‘) 32 { 33 if (c == ‘f‘) the_ball.x_ttm--; 34 else if (c == ‘s‘) the_ball.x_ttm++; 35 else if (c == ‘F‘) the_ball.y_ttm--; 36 else if (c == ‘S‘) the_ball.y_ttm++; 37 else if (c == ‘a‘){ 38 if (flap_pos > LEFT_EDGE){ 39 old_pos = flap_pos; 40 flap_pos -= FLAP_SPEED; 41 move_flap(); 42 } 43 }else if (c == ‘d‘){ 44 if (flap_pos + (int)strlen(FLAP) < RIGHT_EDGE){ 45 old_pos = flap_pos; 46 flap_pos += FLAP_SPEED; 47 move_flap(); 48 } 49 } 50 } 51 wrap_up(); 52 return 0; 53 } 54 55 void set_up() 56 /* 57 * init structure and other stuff 58 */ 59 { 60 void ball_move(int); 61 the_ball.y_pos = Y_INIT; 62 the_ball.x_pos = X_INIT; 63 the_ball.y_ttg = the_ball.y_ttm = Y_TIM; 64 the_ball.x_ttg = the_ball.x_ttm = X_TIM; 65 the_ball.y_dir = 1; 66 the_ball.x_dir = 1; 67 the_ball.symbol = DFL_SYMBOL; 68 the_ball.x_moved = the_ball.y_moved = false; 69 70 initscr(); 71 noecho(); 72 crmode(); 73 74 signal(SIGINT, SIG_IGN); 75 mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol); 76 move_flap(); 77 signal(SIGALRM, ball_move); 78 set_ticker(1000 / TICKS_PER_SEC); 79 } 80 81 void wrap_up() 82 { 83 set_ticker(0); 84 endwin(); 85 } 86 87 void move_flap() 88 { 89 move(BOT_ROW + 1, old_pos); 90 addstr(FLAP); //FLAP is blank. Here it is used to clear its old existence. 91 92 move(BOT_ROW + 1, flap_pos); 93 standout(); 94 addstr(FLAP); 95 standend(); 96 refresh(); 97 } 98 99 void ball_move(int signum) 100 { 101 int y_cur, x_cur, moved; 102 103 signal(SIGALRM, SIG_IGN); 104 x_cur = the_ball.x_pos; 105 y_cur = the_ball.y_pos; 106 moved = 0; 107 108 if (the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1){ 109 the_ball.y_pos += the_ball.y_dir; /* move */ 110 the_ball.y_ttg = the_ball.y_ttm; /* reset */ 111 the_ball.y_moved = 1; 112 moved = 1; 113 } 114 115 if (the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1){ 116 the_ball.x_pos += the_ball.x_dir; /* move */ 117 the_ball.x_ttg = the_ball.x_ttm; /* reset */ 118 the_ball.x_moved = 1; 119 moved = 1; 120 } 121 122 if (moved){ 123 mvaddch(y_cur, x_cur, BLANK); 124 mvaddch(y_cur, x_cur, BLANK); 125 mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol); 126 if(bounce_or_lose(&the_ball)){ 127 signal(SIGALRM, SIG_IGN); 128 move(LINES / 2, COLS / 2); 129 addstr("GAME OVER"); 130 refresh(); 131 return; 132 } 133 move(LINES-1, COLS-1); 134 if (the_ball.x_moved && the_ball.y_moved){ 135 refresh(); 136 the_ball.x_moved = the_ball.y_moved = false; /* reset */ 137 } 138 } 139 signal(SIGALRM, ball_move); 140 } 141 142 int bounce_or_lose(struct ppball *bp) 143 /* 144 * 1 lose 145 * 0 not lose 146 */ 147 { 148 int return_val = 0; 149 150 if (bp->y_pos == TOP_ROW){ 151 bp->y_dir = 1; 152 }else if (bp->y_pos == BOT_ROW){ 153 bp->y_dir = -1; 154 if (!(bp->x_pos >= flap_pos && bp->x_pos <= (flap_pos + (int)strlen(FLAP)))){ 155 return_val = 1; 156 } 157 } 158 159 if (bp->x_pos == LEFT_EDGE){ 160 bp->x_dir = 1; 161 }else if (bp->x_pos == RIGHT_EDGE){ 162 bp->x_dir = -1; 163 } 164 return return_val; 165 }
bounce.h
1 #define BLANK ‘ ‘ 2 #define DFL_SYMBOL ‘o‘ 3 #define TOP_ROW 5 4 #define BOT_ROW 20 5 #define LEFT_EDGE 10 6 #define RIGHT_EDGE 70 7 #define X_INIT 10 8 #define Y_INIT 10 9 #define TICKS_PER_SEC 50 10 #define Y_TIM 8 11 #define X_TIM 8 12 #define FLAP " " 13 //#define FLAP_LEN 21 14 #define FLAP_SPEED 1 15 16 struct ppball { 17 int x_ttg; // x 轴下次重画还要等待多少个计时器 18 int y_ttg; // y 轴下次重画还要等待多少个计时器 19 int x_ttm; // x 轴移动需要等待的信号间隔 20 int y_ttm; // y 轴移动絮叨等待的信号间隔 21 int y_pos; 22 int x_pos; 23 int y_dir; 24 int x_dir; 25 int x_moved; 26 int y_moved; 27 char symbol; 28 };
set_ticker.c
1 #include <stdio.h> 2 #include <sys/time.h> 3 #include <signal.h> 4 #include <stdlib.h> 5 6 /* 7 * set_ticker.c 8 * set_ticker( number_of_milliseconds ) 9 * arranges for the interval timer to issue 10 * SIGALRM‘s at regular intervals 11 * returns -1 on error, 0 for ok 12 * 13 * arg in milliseconds, converted into micro seoncds 14 */ 15 16 17 set_ticker( n_msecs ) 18 { 19 struct itimerval new_timeset; 20 long n_sec, n_usecs; 21 22 n_sec = n_msecs / 1000 ; 23 n_usecs = ( n_msecs % 1000 ) * 1000L ; 24 25 new_timeset.it_interval.tv_sec = n_sec; /* set reload */ 26 new_timeset.it_interval.tv_usec = n_usecs; /* new ticker value */ 27 new_timeset.it_value.tv_sec = n_sec ; /* store this */ 28 new_timeset.it_value.tv_usec = n_usecs ; /* and this */ 29 30 return setitimer(ITIMER_REAL, &new_timeset, NULL); 31 }
标签:
原文地址:http://www.cnblogs.com/forcheryl/p/4641160.html