ReadMe:
弹球游戏按键说明(注意大小写):
Q End Up Game 游戏停止
P Play Again 再玩一次
f 速度x轴方向减速
s 速度x轴方向加速
F 速度y轴方向减速
S 速度y轴方向加速
J 移动挡板变短
L 移动挡板变长
4 移动挡板向左移动
6 移动挡板向右移动
补充说明:
游戏屏幕左上角显示当前得分和游戏状态( GameOn or GameOver )
游戏过程中忽略ctrl+c和ctrl+信号,游戏结束后恢复信号
/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/
分。。。。割。。。。线
/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/
1 #include <stdio.h> 2 3 #include <stdlib.h> 4 5 #include <string.h> 6 7 #include <curses.h> 8 9 #include <signal.h> 10 11 #include "bounce.h" 12 13 #include <sys/time.h> 14 15 16 17 18 19 bool set_up( bool ); 20 21 void wrap_up(); 22 23 void ball_move(); 24 25 void show_baffle(); 26 27 void in_bound( struct BallInfo * ); 28 29 int set_ticker( int ); 30 31 bool judge_in_bound( int ,int ); 32 33 void show_baffle(); 34 35 void try_move_my_moving_baffle( char ); 36 37 void show_my_moving_baffle(); 38 39 void cover_my_moving_baffle(); 40 41 void show_score_gameon(); 42 43 void show_score_gameoff(); 44 45 void clear_up(); 46 47 void solve_sigint(); 48 49 void solve_sigquit(); 50 51 bool ball_crash_brick(); 52 53 void show_my_moving_brick( int ,int ); 54 55 void cover_my_moving_brick( int ,int ); 56 57 58 59 struct BallInfo my_moving_ball; 60 61 struct BaffleInfo my_moving_Baffle; 62 63 struct Brick my_moving_brick[2]; 64 65 int score; 66 67 68 69 void clear_up(){ 70 71 int i,j; 72 73 for( i=0;i<24;i++ ){ 74 75 for( j=0;j<80;j++ ){ 76 77 mvaddch( i,j,BLANK ); 78 79 } 80 81 } 82 83 return ; 84 85 } 86 87 /******************************************************** 88 89 clear up the whole screen 90 91 *********************************************************/ 92 93 94 95 96 97 bool set_up( bool flag ){ 98 99 char od; 100 101 my_moving_ball.x_pos = startX; 102 103 my_moving_ball.y_pos = startY; 104 105 my_moving_ball.x_Time1 = my_moving_ball.x_Time2 = X_Time; 106 107 my_moving_ball.y_Time1 = my_moving_ball.y_Time2 = Y_Time; 108 109 my_moving_ball.x_dir = 1; 110 111 my_moving_ball.y_dir = 1; 112 113 my_moving_ball.ball_symbol = BALL; 114 115 116 117 my_moving_Baffle.len = BaffleLen; 118 119 my_moving_Baffle.baffle_symbol = BaffleSymbol; 120 121 my_moving_Baffle.x = startX_; 122 123 my_moving_Baffle.y = startY_; 124 125 126 127 my_moving_brick[0].x = TopRow+DELTA; 128 129 my_moving_brick[0].y = LeftCol+1; 130 131 my_moving_brick[0].speed = speed1; 132 133 my_moving_brick[0].dir = 1; 134 135 my_moving_brick[0].flag = true; 136 137 138 139 score = 0; 140 141 142 143 initscr(); 144 145 noecho(); 146 147 crmode(); 148 149 keypad( stdscr,true ); 150 151 //attroff( A_BLINK ); 152 153 154 155 if( flag==true ){ 156 157 mvaddstr( startX-3,startY,"Do you want play game (y/n)?" ); 158 159 mvaddstr( startX-2,startY,"Press 's' to slow (x-axis)"); 160 161 mvaddstr( startX-1,startY,"Press 'f' to fast (x-axis)"); 162 163 mvaddstr( startX,startY,"Press 'S' to slow (y-axis)"); 164 165 mvaddstr( startX+1,startY,"Press 'F' to slow (y-axis)"); 166 167 mvaddstr( startX+2,startY,"Press '4' to the left"); 168 169 mvaddstr( startX+3,startY,"Press '6' to the right"); 170 171 mvaddstr( startX+4,startY,"(Please refer to the ReadMe.)"); 172 173 move( LINES-1,COLS-1 ); 174 175 refresh(); 176 177 od = getch(); 178 179 if( od=='y'||od=='Y' ){ clear_up(); } 180 181 else { 182 183 wrap_up(); 184 185 return false; 186 187 } 188 189 } 190 191 /* 游戏最开始进入界面 */ 192 193 194 195 show_score_gameon(); 196 197 198 199 signal( SIGINT,solve_sigint ); 200 201 signal( SIGQUIT,solve_sigquit ); 202 203 204 205 mvaddch( my_moving_ball.y_pos, my_moving_ball.x_pos, my_moving_ball.ball_symbol ); 206 207 show_baffle(); 208 209 show_my_moving_baffle(); 210 211 show_my_moving_brick( my_moving_brick[0].x,my_moving_brick[0].y ); 212 213 refresh(); 214 215 216 217 signal( SIGALRM, ball_move ); 218 219 //signal( SIGALRM, brick_move ); 220 221 set_ticker( 1000 / TICKS_PER_SEC ); 222 223 return true; 224 225 } 226 227 /******************************************************** 228 229 启动二维弹球 230 231 set up the game 232 233 *********************************************************/ 234 235 236 237 238 239 void wrap_up(){ 240 241 set_ticker( 0 ); 242 243 beep(); 244 245 endwin(); 246 247 } 248 249 /******************************************************** 250 251 终止二维弹球 252 253 end up the game 254 255 *********************************************************/ 256 257 258 259 260 261 void solve_sigint(){ 262 263 mvaddstr( 2,0,"You enter ctrl+C"); 264 265 return ; 266 267 } 268 269 /******************************************************** 270 271 solve the signal :ctrl + C 272 273 *********************************************************/ 274 275 276 277 278 279 void show_my_moving_brick( int x,int y ){ 280 281 if( my_moving_brick[0].flag==true ){ 282 283 mvaddstr( x,y,BRICK ); 284 285 move( LINES-1,COLS-1 ); 286 287 } 288 289 return ; 290 291 } 292 293 void cover_my_moving_brick( int x,int y ){ 294 295 mvaddstr( x,y," " ); 296 297 move( LINES-1,COLS-1 ); 298 299 return ; 300 301 } 302 303 /******************************************************** 304 305 show my moving brick 306 307 *********************************************************/ 308 309 310 311 312 313 void solve_sigquit(){ 314 315 mvaddstr( 2,0,"You enter ctrl+\"); 316 317 return ; 318 319 } 320 321 /******************************************************** 322 323 solve the signal :ctrl + 324 325 *********************************************************/ 326 327 328 329 330 331 void show_score_gameon(){ 332 333 move( 0,0 ); 334 335 addstr( Tip1 ); 336 337 char str1[ 105 ],str2[ 105 ]; 338 339 int cnt1 = 0,cnt2 = 0; 340 341 int tp = score/2; 342 343 int i; 344 345 while( tp ){ 346 347 str1[ cnt1++ ] = tp%10 + '0'; 348 349 tp /= 10; 350 351 } 352 353 for( i=cnt1-1;i>=0;i-- ){ 354 355 str2[ cnt2++ ] = str1[ i ]; 356 357 } 358 359 if( cnt2==0 ) str2[ cnt2++ ] = '0'; 360 361 str2[ cnt2 ] = '