bounce2d.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* bounce2d 1.0
  2. * bounce a character (default is 'o') around the screen
  3. * defined by some parameters
  4. *
  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. *
  9. * blocks on read, but timer tick sends SIGALRM caught by ball_move
  10. * build: cc bounce2d.c set_ticker.c -lcurses -o bounce2d
  11. */
  12. #include <curses.h>
  13. #include <signal.h>
  14. #include "bounce.h"
  15. struct ppball the_ball ;
  16. /** the main loop **/
  17. void set_up();
  18. void wrap_up();
  19. int main()
  20. {
  21. int c;
  22. set_up();
  23. while ( ( c = getchar()) != 'Q' ){
  24. if ( c == 'f' ) the_ball.x_ttm--;
  25. else if ( c == 's' ) the_ball.x_ttm++;
  26. else if ( c == 'F' ) the_ball.y_ttm--;
  27. else if ( c == 'S' ) the_ball.y_ttm++;
  28. }
  29. wrap_up();
  30. }
  31. void set_up()
  32. /*
  33. * init structure and other stuff
  34. */
  35. {
  36. void ball_move(int);
  37. the_ball.y_pos = Y_INIT;
  38. the_ball.x_pos = X_INIT;
  39. the_ball.y_ttg = the_ball.y_ttm = Y_TTM ;
  40. the_ball.x_ttg = the_ball.x_ttm = X_TTM ;
  41. the_ball.y_dir = 1 ;
  42. the_ball.x_dir = 1 ;
  43. the_ball.symbol = DFL_SYMBOL ;
  44. initscr();
  45. noecho();
  46. crmode();
  47. signal( SIGINT , SIG_IGN );
  48. mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
  49. refresh();
  50. signal( SIGALRM, ball_move );
  51. set_ticker( 1000 / TICKS_PER_SEC ); /* send millisecs per tick */
  52. }
  53. void wrap_up()
  54. {
  55. set_ticker( 0 );
  56. endwin(); /* put back to normal */
  57. }
  58. void ball_move(int signum)
  59. {
  60. int y_cur, x_cur, moved;
  61. signal( SIGALRM , SIG_IGN ); /* dont get caught now */
  62. y_cur = the_ball.y_pos ; /* old spot */
  63. x_cur = the_ball.x_pos ;
  64. moved = 0 ;
  65. if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){
  66. the_ball.y_pos += the_ball.y_dir ; /* move */
  67. the_ball.y_ttg = the_ball.y_ttm ; /* reset*/
  68. moved = 1;
  69. }
  70. if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){
  71. the_ball.x_pos += the_ball.x_dir ; /* move */
  72. the_ball.x_ttg = the_ball.x_ttm ; /* reset*/
  73. moved = 1;
  74. }
  75. if ( moved ){
  76. mvaddch( y_cur, x_cur, BLANK );
  77. mvaddch( y_cur, x_cur, BLANK );
  78. mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
  79. bounce_or_lose( &the_ball );
  80. move(LINES-1,COLS-1);
  81. refresh();
  82. }
  83. signal( SIGALRM, ball_move); /* for unreliable systems */
  84. }
  85. int bounce_or_lose(struct ppball *bp)
  86. {
  87. int return_val = 0 ;
  88. if ( bp->y_pos == TOP_ROW ){
  89. bp->y_dir = 1 ;
  90. return_val = 1 ;
  91. } else if ( bp->y_pos == BOT_ROW ){
  92. bp->y_dir = -1 ;
  93. return_val = 1;
  94. }
  95. if ( bp->x_pos == LEFT_EDGE ){
  96. bp->x_dir = 1 ;
  97. return_val = 1 ;
  98. } else if ( bp->x_pos == RIGHT_EDGE ){
  99. bp->x_dir = -1;
  100. return_val = 1;
  101. }
  102. return return_val;
  103. }