Advertisement
Guest User

game

a guest
Oct 16th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. /** @file game.c
  2. @author Gordon Lay and Henry Mossman
  3. @date 17/10/2019
  4. @brief A simple ball catching game
  5.  
  6. @defgroup game A simple ball catching game.
  7. */
  8.  
  9. #include <string.h>
  10. #include "system.h"
  11. #include "tinygl.h"
  12. #include "task.h"
  13. #include "navswitch.h"
  14. #include "eeprom.h"
  15. #include "uint8toa.h"
  16. #include "../fonts/font3x5_1.h"
  17. #include "pacer.h"
  18. #include <avr/io.h>
  19. #include "tweeter.h"
  20. #include "mmelody.h"
  21. #include "button.h"
  22. #include "pio.h"
  23. #include <stdbool.h>
  24. #include "ir_serial.h"
  25. #include "ir.h"
  26. #include "move.h"
  27. #include "led.h"
  28. #include "throwBall.h"
  29.  
  30. #define PACER_RATE 500
  31. #define MESSAGE_RATE 25
  32. #define ready_char 'z'
  33.  
  34. //Ready pending boolean
  35. static _Bool ready;
  36.  
  37. //Initialise the ready boolean to false
  38. static void ready_screen_init(void)
  39. {
  40. ready = false;
  41. }
  42.  
  43. //Task: ready is now true
  44. static void ready_screen_task(void)
  45. {
  46. ready = true;
  47. }
  48.  
  49. //Initialising tinygl font, speed, scroll and rotate settings
  50. static void tinygl_task_init(void)
  51. {
  52. tinygl_font_set(&font3x5_1);
  53. tinygl_text_speed_set(MESSAGE_RATE);
  54. tinygl_text_mode_set(TINYGL_TEXT_MODE_SCROLL);
  55. tinygl_text_dir_set (TINYGL_TEXT_DIR_ROTATE);
  56. }
  57.  
  58. //IR error message
  59. static void show_err (uint8_t err)
  60. {
  61. char buffer[3];
  62. buffer[0] = 'E';
  63. buffer[1] = err + '0';
  64. buffer[2] = 0;
  65. tinygl_text (buffer);
  66. }
  67.  
  68. //Ready-state loop; shows message 'READY?' while sending and checking
  69. //for 1's transmitted via serial ir
  70. static void ready_loop (void)
  71. {
  72. uint16_t errorTime = 0;
  73. tinygl_clear();
  74. tinygl_text("READY?");
  75. //Signal to other funkit that this funkit is ready
  76. ir_serial_transmit (ready_char);
  77. while (1)
  78. {
  79. uint8_t data;
  80. pacer_wait();
  81. tinygl_update();
  82. //Receive serial ir signals
  83. ir_serial_ret_t ret;
  84. ret = ir_serial_receive (&data);
  85. //If the ir signal is ok and it is the ready character,
  86. //return to main
  87. if (ret == IR_SERIAL_OK) {
  88. if (data == ready_char) {
  89. ir_serial_transmit (ready_char);
  90. return;
  91. }
  92. //else diplay error code for some time, then return to the
  93. //top of main
  94. } else if (ret < 0) {
  95. tinygl_clear();
  96. show_err (-ret);
  97. while(1) {
  98. pacer_wait();
  99. tinygl_update();
  100. errorTime++;
  101. if (errorTime >= 1800) {
  102. main();
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
  109. //The game function
  110. static void game (void)
  111. {
  112. //Start with loading the move function
  113. move();
  114.  
  115. }
  116.  
  117.  
  118. int main (void)
  119. {
  120. //Initiation
  121. ready_screen_init();
  122. button_init();
  123. system_init();
  124. ir_serial_init();
  125. pacer_init (PACER_RATE);
  126. tinygl_init (PACER_RATE);
  127. tinygl_task_init();
  128. //Intro screen
  129. tinygl_text("CATCHIN BALL Z");
  130.  
  131. while(1)
  132. {
  133. pacer_wait();
  134. tinygl_update();
  135. button_update();
  136. //Break out of Intro screen and into the Ready loop
  137. if (button_push_event_p(BUTTON1) && !ready) {
  138. ready_screen_task();
  139. ready_loop();
  140. //When both players are ready, start the game
  141. tinygl_clear();
  142. game();
  143. }
  144. }
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement