Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include "system.h"
  2. #include "display.h"
  3. #include "tinygl.h"
  4. #include "../fonts/font5x7_1.h"
  5. #include "ball.h"
  6. #include "pacer.h"
  7. #include "ir_uart.h"
  8. #include "navswitch.h"
  9. #include "paddle.h"
  10.  
  11. #define OFF_BOARD 10
  12. #define ON_BOARD 11
  13. #define GAME_LOST 20
  14. #define GAME_WON 21
  15. #define LOOP_RATE 500
  16. #define MESSAGE_RATE 15
  17. #define PADDLE_RATE 500
  18.  
  19. static char row = 3;
  20. static char col = 4;
  21. static int rowinc = 1;
  22. static int colinc = 1;
  23. static int paddle = 3;
  24.  
  25.  
  26.  
  27. int starting(void)
  28. {
  29.     int location = 0;
  30.     bool not_started = true;
  31.     tinygl_text("Push Navswitch to play   ");
  32.    
  33.     while (not_started)
  34.     {
  35.         //Updating compontants
  36.         navswitch_update();
  37.         pacer_wait();
  38.         tinygl_update();
  39.        
  40.         //If the center button has been pushed, intiating the start of the game on this board
  41.         if (navswitch_push_event_p (NAVSWITCH_PUSH))
  42.         {
  43.             not_started = false;
  44.             ir_uart_putc(OFF_BOARD);
  45.             location = ON_BOARD;
  46.            
  47.         //Else if signal is being sent to the board, read it
  48.         } else if (ir_uart_read_ready_p()) {
  49.             location = ir_uart_getc();
  50.             not_started = false;
  51.         }
  52.     }
  53.    
  54.     //Clearing the screen
  55.     tinygl_clear();
  56.     tinygl_update();
  57.    
  58.     return location;
  59. }
  60.  
  61. int game_play(void)
  62. {
  63.     row += rowinc;
  64.     col += colinc;
  65.    
  66.     //If the ball has hit the wall and needs to be reflected
  67.     if (row <= 0 || row >= 7)
  68.     {
  69.         rowinc = -rowinc;
  70.         row = 2 * rowinc;
  71.     }
  72.    
  73.     //If the ball is in the paddle col
  74.     if (col == 5)
  75.     {
  76.         //When the ball hits the paddle is reflected
  77.         if (row <= --paddle && row >= ++paddle)
  78.         {
  79.             col--;
  80.             colinc = -colinc;
  81.            
  82.         //When the ball is not reflected
  83.         } else {
  84.             ball_display(row, col);
  85.             return GAME_LOST;
  86.         }
  87.        
  88.     //The ball is off the board and needs to be transmitted
  89.     } else if (col == 0) {
  90.         ir_uart_putc(row);
  91.         return OFF_BOARD;
  92.         //Is the a way of sending the rowinc as well - talked to Sam (COMSOC) there is and it was confusing
  93.     }
  94.    
  95.     //Displaying the ball
  96.     pacer_wait(); ///testing
  97.     ball_display(row, col);
  98.    
  99.     return ON_BOARD;
  100. }
  101.      
  102.    
  103. int main(void)
  104. {
  105.     //Initialing all the functions
  106.     system_init();
  107.     navswitch_init();
  108.     display_init();
  109.     ir_uart_init();
  110.     pacer_init(LOOP_RATE);
  111.     tinygl_init(LOOP_RATE);
  112.     tinygl_font_set(&font5x7_1);
  113.     tinygl_text_speed_set(MESSAGE_RATE);
  114.     tinygl_text_mode_set(TINYGL_TEXT_MODE_SCROLL);
  115.    
  116.     //Initialising the variables
  117.     int location; //If the ball is on or off the board
  118.     int counter = 0;
  119.    
  120.     //Starting function
  121.     location = starting();
  122.    
  123.     //Main Run cycle
  124.     while (location != GAME_LOST && location != GAME_WON)
  125.     {
  126.         counter++;
  127.        
  128.         //When the ball is on the board
  129.         if (location == ON_BOARD)
  130.         {
  131.             if (counter > 80)
  132.             {
  133.                 pacer_wait(); ///testing
  134.                 location = game_play();
  135.                 pacer_wait(); ///testing
  136.                 counter = 0;
  137.             }
  138.             pacer_wait(); ///testing
  139.             paddle = paddle_piece(paddle);
  140.            
  141.         //When the ball is off the board
  142.         } else {
  143.             paddle = paddle_piece(paddle);
  144.            
  145.             //Checking if there is signal ready to recieve
  146.             if (ir_uart_read_ready_p())
  147.             {
  148.                 row = ir_uart_getc();
  149.                 col = 0;
  150.  
  151.                 //Checking that this is not the win indicator
  152.                 if (row == 21) {
  153.                     location = GAME_WON;
  154.                 }
  155.  
  156.                 game_play();
  157.             }
  158.         }
  159.     }
  160.    
  161.     if (location == GAME_LOST)
  162.     {
  163.         tinygl_text("Loser   ");
  164.         tinygl_update();
  165.         pacer_wait();
  166.         tinygl_clear();
  167.        
  168.     } else {
  169.         tinygl_text("Winner   ");
  170.         tinygl_update();
  171.         pacer_wait();
  172.         tinygl_clear();
  173.     }
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement