TRex.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <stdio.h>
  2. #include <U8g2lib.h>
  3. // https://github.com/sarafong/T-Rex-Runner
  4. // Upgraded u8g2 version of the classic Google Chrome T-Rex Runner game.
  5. // You may reference Drivers/drv_gpio.c for pinout
  6. // In u8x8.h #define U8X8_USE_PINS
  7. #define U8G2_PIN_UP 3 // PA3
  8. #define U8G2_PIN_DOWN 4 // PA4
  9. #define U8G2_PIN_LEFT 6 // PA6
  10. #define U8G2_PIN_RIGHT 24 // PB8
  11. #define U8G2_PIN_SELECT 21 // PB5
  12. #define U8G2_PIN_HOME 17 // PB1
  13. #define OLED_SPI_PIN_CLK 5 // PA5
  14. #define OLED_SPI_PIN_MOSI 7 // PA7
  15. #define OLED_SPI_PIN_RES 2 // PA2
  16. #define OLED_SPI_PIN_DC 1 // PA1
  17. #define OLED_SPI_PIN_CS 0 // PA0
  18. // Check https://github.com/olikraus/u8g2/wiki/u8g2setupcpp for all supported devices
  19. // U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
  20. static U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0,
  21. /* clock=*/ OLED_SPI_PIN_CLK,
  22. /* data=*/ OLED_SPI_PIN_MOSI,
  23. /* cs=*/ OLED_SPI_PIN_CS,
  24. /* dc=*/ OLED_SPI_PIN_DC,
  25. /* reset=*/ OLED_SPI_PIN_RES);
  26. static int random(int low, int high)
  27. {
  28. return rand() % (high - low + 1) + low;
  29. }
  30. static unsigned long millis() {
  31. return ((unsigned long)rt_tick_get() / RT_TICK_PER_SECOND * 1000);
  32. }
  33. //Multiple Dino Bitmaps
  34. //--------------------Bitmaps--------------------//
  35. //----------Dimensions----------//
  36. #define smallcactus_width 8
  37. #define smallcactus_height 17
  38. #define standing_width 21
  39. #define standing_height 20
  40. #define frontLeg_width 21
  41. #define frontLeg_height 20
  42. #define backLeg_width 21
  43. #define backLeg_height 20
  44. #define gameover_width 99
  45. #define gameover_height 35
  46. #define easy_width 32
  47. #define easy_height 15
  48. #define medium_width 30
  49. #define medium_height 20
  50. #define hard_width 32
  51. #define hard_height 15
  52. #define insane_width 30
  53. #define insane_height 20
  54. #define menu_width 32
  55. #define menu_height 15
  56. #define replay_width 32
  57. #define replay_height 15
  58. //----------Bitmap Arrays----------//
  59. static const unsigned char smallcactus_bits[] = {
  60. 0x18, 0x18, 0x98, 0x98, 0x99, 0x99, 0x99, 0x99, 0x79, 0x19, 0x1e, 0x18,
  61. 0x18, 0x18, 0x18, 0x18, 0x18
  62. };
  63. static const unsigned char standing_bits[] = {
  64. 0x00, 0xf8, 0x0f, 0x00, 0xcc, 0x1f, 0x00, 0xf8, 0x1f, 0x00, 0xfc, 0x1f,
  65. 0x00, 0xf8, 0x1f, 0x00, 0xfc, 0x00, 0x00, 0xf8, 0x07, 0x01, 0x3e, 0x00,
  66. 0x01, 0x7f, 0x00, 0xc3, 0xff, 0x01, 0xef, 0x7f, 0x00, 0xff, 0x7f, 0x00,
  67. 0xfe, 0x3f, 0x00, 0xfc, 0x3f, 0x00, 0xf8, 0x1f, 0x00, 0xf0, 0x07, 0x00,
  68. 0xe0, 0x0e, 0x00, 0x60, 0x04, 0x00, 0x20, 0x08, 0x00, 0x60, 0x08, 0x00
  69. };
  70. static const unsigned char frontLeg_bits[] = {
  71. 0x00, 0xf0, 0x0f, 0x00, 0xdc, 0x1f, 0x00, 0xf8, 0x1f, 0x00, 0xf8, 0x1f,
  72. 0x00, 0xf8, 0x1f, 0x00, 0xfc, 0x00, 0x00, 0xf8, 0x07, 0x01, 0x7e, 0x00,
  73. 0x01, 0x7f, 0x00, 0xc3, 0xff, 0x01, 0xef, 0x7f, 0x01, 0xff, 0x7f, 0x00,
  74. 0xfe, 0x7f, 0x00, 0xfc, 0x3f, 0x00, 0xf8, 0x1f, 0x00, 0xf0, 0x0f, 0x00,
  75. 0xe0, 0x18, 0x00, 0xe0, 0x00, 0x00, 0x20, 0x00, 0x00, 0x60, 0x00, 0x00
  76. };
  77. static const unsigned char backLeg_bits[] = {
  78. 0x00, 0xf8, 0x0f, 0x00, 0xd8, 0x1f, 0x00, 0xfc, 0x1f, 0x00, 0xf8, 0x1f,
  79. 0x00, 0xfc, 0x1f, 0x00, 0xf8, 0x00, 0x00, 0xfc, 0x07, 0x01, 0x3e, 0x00,
  80. 0x01, 0x7f, 0x00, 0xc3, 0xff, 0x01, 0xef, 0x7f, 0x00, 0xff, 0x7f, 0x00,
  81. 0xfe, 0x3f, 0x00, 0xfc, 0x3f, 0x00, 0xf8, 0x1f, 0x00, 0xf0, 0x07, 0x00,
  82. 0x60, 0x0e, 0x00, 0xc0, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x0c, 0x00
  83. };
  84. static const unsigned char gameover_bits[] = {
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0xc0, 0x03, 0x0e, 0xd8, 0xc0, 0x07, 0x80, 0x07, 0x24,
  89. 0xe0, 0x01, 0x0f, 0x00, 0x20, 0x00, 0x09, 0xf8, 0x40, 0x00, 0x80, 0x04,
  90. 0x66, 0x30, 0x80, 0x09, 0x00, 0x20, 0x03, 0x19, 0xe8, 0xc0, 0x03, 0x80,
  91. 0x04, 0x3c, 0xe0, 0x01, 0x0d, 0x00, 0x60, 0x02, 0x1b, 0x88, 0x40, 0x00,
  92. 0xc0, 0x0c, 0x18, 0x20, 0x00, 0x07, 0x00, 0xc0, 0x03, 0x11, 0x98, 0xc0,
  93. 0x07, 0x80, 0x07, 0x08, 0xe0, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54,
  105. 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x7e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x1e, 0xe6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x4e, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x6e, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xce, 0xef, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0xef, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0xf4, 0x01,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe,
  118. 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  123. };
  124. static const unsigned char easy_bits[] = {
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  126. 0x00, 0x00, 0x00, 0xc0, 0xf0, 0x00, 0x00, 0xf0, 0x10, 0x00, 0x00, 0xfc,
  127. 0x50, 0x6d, 0x09, 0xff, 0x10, 0x2c, 0xc5, 0xff, 0x10, 0x4b, 0x06, 0xff,
  128. 0xf0, 0x7e, 0x02, 0xfc, 0x00, 0x00, 0x02, 0xf0, 0x00, 0x00, 0x00, 0xc0,
  129. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  130. };
  131. static const unsigned char medium_bits[] = {
  132. 0x00, 0x00, 0x00, 0x00, 0x22, 0x40, 0x01, 0x00, 0x66, 0x40, 0x00, 0x00,
  133. 0xaa, 0x75, 0xd5, 0x0f, 0xda, 0x4f, 0x61, 0x08, 0x82, 0x48, 0x45, 0x09,
  134. 0x42, 0x77, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00,
  136. 0x00, 0xe0, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0xf0, 0x01, 0x00,
  137. 0x00, 0xf8, 0x03, 0x00, 0x00, 0xf8, 0x03, 0x00, 0x00, 0xfc, 0x07, 0x00,
  138. 0x00, 0xfc, 0x07, 0x00, 0x00, 0xfe, 0x0f, 0x00
  139. };
  140. static const unsigned char hard_bits[] = {
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  142. 0x07, 0x00, 0x00, 0x00, 0x1f, 0x40, 0x00, 0x20, 0x7f, 0x40, 0x04, 0x20,
  143. 0xff, 0x41, 0xbb, 0x3b, 0xff, 0x43, 0x62, 0x24, 0xff, 0x41, 0x8c, 0x24,
  144. 0x7f, 0x40, 0xb8, 0x38, 0x1f, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  145. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  146. };
  147. static const unsigned char insane_bits[] = {
  148. 0x00, 0xfe, 0x0f, 0x00, 0x00, 0xfc, 0x07, 0x00, 0x00, 0xfc, 0x07, 0x00,
  149. 0x00, 0xf8, 0x03, 0x00, 0x00, 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x01, 0x00,
  150. 0x00, 0xf0, 0x01, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00,
  151. 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x18, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xc0, 0xdd, 0x9d, 0x03,
  153. 0x50, 0x84, 0xa5, 0x03, 0x50, 0x52, 0x65, 0x00, 0x58, 0xdc, 0xa5, 0x03,
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  155. };
  156. static const unsigned char menu_bits[] = {
  157. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  158. 0x07, 0x00, 0x00, 0x00, 0x1f, 0x10, 0x01, 0x00, 0x7f, 0x30, 0x01, 0x00,
  159. 0xff, 0xb1, 0xdd, 0x25, 0xff, 0x53, 0x5d, 0x26, 0xff, 0x11, 0x44, 0x4a,
  160. 0x7f, 0x10, 0x5d, 0x3a, 0x1f, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  161. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  162. };
  163. static const unsigned char replay_bits[] = {
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  165. 0x00, 0x00, 0x00, 0xc0, 0x07, 0x20, 0x00, 0xf0, 0x09, 0x20, 0x00, 0xfc,
  166. 0xe9, 0xae, 0x4b, 0xff, 0xe7, 0x32, 0xeb, 0xff, 0x15, 0xb2, 0x34, 0xff,
  167. 0xe9, 0xae, 0x13, 0xfc, 0x00, 0x02, 0x10, 0xf0, 0x00, 0x00, 0x00, 0xc0,
  168. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  169. };
  170. //----------Constructor from U8GLIB----------//
  171. //U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  172. //U8G2_ST7567_JLX12864_F_4W_SW_SPI u8g(U8G2_R0, 13, 11, 10, 9, 8);
  173. //----------Initializing Variables----------//
  174. static unsigned long dinoTimeToMove = 0; //Used to space out time(millis) between draws/moves
  175. //static unsigned long dinoTimeToDraw = 0;
  176. static int score;
  177. static int highScore = 0;
  178. static int multiplier; //Score multiplier
  179. //bool ignoreRepeat = false; //Avoid Switch Bouncing
  180. static long compensation; //To compensate for the amount of time the Arduino has been running for after each run (essentially "resets" the time)
  181. static bool gameOver;
  182. static bool menu = true;
  183. static bool down, jump; //Boolean values to check the state of the dinosaur
  184. static int dinoSwitch; //Switch between dino bitmaps to simulate movement
  185. static int dinoY, cactiX1, cactiX2; //Positions of objects
  186. static int velocity; //Speed of entire game
  187. static int difficulty; //How fast the velocity increases
  188. //--------------------Functions--------------------//
  189. //Set/Reset Loop for Playing Again
  190. static void reset() {
  191. compensation = millis();
  192. gameOver = false;
  193. down = false;
  194. jump = false;
  195. dinoSwitch = true;
  196. dinoY = 44;
  197. cactiX1 = 130;
  198. cactiX2 = cactiX1 + random(70, 120);
  199. velocity = 2;
  200. }
  201. //Deteching Joystick Interaction
  202. static void keyPress(rt_uint8_t event) {
  203. //if (event != 0 )rt_kprintf("Key Event%d\n", event);
  204. if ( menu && event!=0 /* && ignoreRepeat */) { //Any Input
  205. //rt_kprintf("Key Pressed %d\n", event);
  206. //ignoreRepeat = false;
  207. reset();
  208. menu = false;
  209. if ( event == U8X8_MSG_GPIO_MENU_PREV ) difficulty = 10000; //Left
  210. else if ( event == U8X8_MSG_GPIO_MENU_SELECT ) difficulty = random( 2500, 10000 ); //Click
  211. else if ( event == U8X8_MSG_GPIO_MENU_DOWN ) difficulty = 2500; //Down
  212. else if ( event == U8X8_MSG_GPIO_MENU_NEXT ) difficulty = 5000; // Right
  213. else if ( event == U8X8_MSG_GPIO_MENU_UP )difficulty = 7500; //Up
  214. multiplier = 15000 / difficulty;
  215. }
  216. else if ( gameOver && ( event == U8X8_MSG_GPIO_MENU_PREV || event == U8X8_MSG_GPIO_MENU_NEXT ) ) { //Left -> PlayAgain // Right -> Menu
  217. //ignoreRepeat = false;
  218. reset();
  219. if ( event == U8X8_MSG_GPIO_MENU_NEXT ) menu = true; //Pushing Right
  220. }
  221. else if ( dinoY == 44 && event == U8X8_MSG_GPIO_MENU_UP ) { //Corresponds to pushing up on the joystick
  222. jump = true;
  223. down = true;
  224. }
  225. //else if ( input > 1000 ) ignoreRepeat = true; //Avoid switch bouncing
  226. }
  227. //Collision Detecting and Setting HighScore
  228. static void collision() {
  229. if ( cactiX1 <= 23 && cactiX1 + smallcactus_width >= 15 && dinoY + 20 >= 50 ) {
  230. if ( (millis() - compensation)*multiplier / 250 > highScore && !gameOver ) highScore = (millis() - compensation) * multiplier / 250; //Changes highscore if current score is greater
  231. gameOver = true;
  232. }
  233. }
  234. //-------Move Functions-------//
  235. static void moveDino() {
  236. if ( dinoY > 44 ) dinoY = 44; //Resets dinosaur to it passes it
  237. if ( jump || dinoY <= 43 ) { //Allows jumping if on the ground
  238. jump = false;
  239. if ( dinoY >= 16 && down ) dinoY -= velocity; //Going up
  240. else { //Going down
  241. down = false;
  242. dinoY += velocity;
  243. }
  244. }
  245. }
  246. static void moveCactus() {
  247. cactiX1 -= velocity;
  248. cactiX2 -= velocity;
  249. if ( cactiX1 <= -15 ) { //Once cacti reaches left side of screen, resets to right side of screen
  250. cactiX1 = cactiX2;
  251. cactiX2 = cactiX1 + random(70, 150);
  252. }
  253. }
  254. static void moveObjects() {
  255. if ( millis() > dinoTimeToMove + 50 ) { //Updates every 50 milliseconds
  256. velocity = (millis() - compensation) / difficulty + 2; //Increases velocity as game progresses
  257. moveDino();
  258. moveCactus();
  259. dinoTimeToMove = millis();
  260. }
  261. }
  262. static void drawDinoCactus( ) {
  263. if ( dinoY < 44 ) u8g2.drawXBM(10, dinoY, standing_width, standing_height, standing_bits); //While jumping don't change bitmap
  264. else if ( (millis() - compensation) % 16 > 9 ) u8g2.drawXBM(10, dinoY, frontLeg_width, frontLeg_height, frontLeg_bits); //Switch between bitmaps every 9 millis
  265. else u8g2.drawXBM(10, dinoY, backLeg_width, backLeg_height, backLeg_bits);
  266. u8g2.drawXBM(cactiX1, 47, smallcactus_width, smallcactus_height, smallcactus_bits); //Draw cacti
  267. u8g2.drawXBM(cactiX2, 47, smallcactus_width, smallcactus_height, smallcactus_bits);
  268. }
  269. static void drawScore() {
  270. char scoreBuff[50];
  271. if ( menu ) { //Print highscore on menu screen
  272. score = highScore;
  273. snprintf(scoreBuff, 50, "HighS: %d", score);
  274. } else if ( !gameOver ) { //Increments score ONLY while playing
  275. score = (millis() - compensation) * multiplier / 250;
  276. snprintf(scoreBuff, 50, "Score: %d", score);
  277. }
  278. u8g2.setFont(u8g2_font_t0_11_mf );
  279. u8g2.drawStr( 55, 8, scoreBuff );
  280. }
  281. //Draws Game Over Screen
  282. static void playAgain() {
  283. u8g2.drawXBM(14, 14, gameover_width, gameover_height, gameover_bits);
  284. u8g2.drawXBM(23, 30, replay_width, replay_height, replay_bits);
  285. u8g2.drawXBM(70, 30, menu_width, menu_height, menu_bits);
  286. }
  287. //Draws Menu Screen
  288. static void menuScreen() {
  289. u8g2.drawXBM(26, 30, easy_width, easy_height, easy_bits);
  290. u8g2.drawXBM(49, 12, medium_width, medium_height, medium_bits);
  291. u8g2.drawXBM(69, 30, hard_width, hard_height, hard_bits);
  292. u8g2.drawXBM(49, 43, insane_width, insane_height, insane_bits);
  293. }
  294. static void draw() {
  295. u8g2.clearBuffer();
  296. drawDinoCactus();
  297. drawScore();;
  298. if ( menu ) menuScreen(); //Draw Menu Screen
  299. else if ( gameOver ) playAgain(); //Draw Game Over Screen
  300. u8g2.sendBuffer();
  301. }
  302. static void u8g2_trex_entry(void *parameter)
  303. {
  304. reset();
  305. u8g2.begin(/*Select=*/ U8G2_PIN_SELECT, /*Right/Next=*/ U8G2_PIN_RIGHT, /*Left/Prev=*/ U8G2_PIN_LEFT, /*Up=*/ U8G2_PIN_UP, /*Down=*/ U8G2_PIN_DOWN, /*Home/Cancel=*/ U8G2_PIN_HOME);
  306. for(;;)
  307. {
  308. keyPress(u8g2.getMenuEvent());
  309. if ( !gameOver && !menu ) moveObjects(); //Only move objects while not on menu or game over screen
  310. draw();
  311. collision();
  312. }
  313. }
  314. #define THREAD_PRIORITY 25
  315. #define THREAD_STACK_SIZE 1024
  316. #define THREAD_TIMESLICE 5
  317. static rt_thread_t tid1 = RT_NULL;
  318. static void u8g2_game_trex(int argc,char *argv[])
  319. {
  320. tid1 = rt_thread_create("tu8g250",
  321. u8g2_trex_entry, RT_NULL,
  322. THREAD_STACK_SIZE,
  323. THREAD_PRIORITY, THREAD_TIMESLICE);
  324. if (tid1 != RT_NULL)
  325. rt_thread_startup(tid1);
  326. }
  327. MSH_CMD_EXPORT(u8g2_game_trex, u8g2 game T-Rex sample);