Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #include <M5Stack.h>
  2. // #include "esp32_digital_led_lib.h"
  3.  
  4. // This part of the code is for a different version of the M5 stack (with LED):
  5. // strand_t m_sLeds = {.rmtChannel = 0, .gpioNum = 15, .ledType = LED_WS2812B_V3, .brightLimit = 32, .numPixels = 10, .pixels = nullptr, ._stateVars = nullptr};
  6. // Extern means that the data is provided in a different file - the array of the logo bitmap is imported from logo.c
  7. extern unsigned char logo[];
  8. // Specify screen size
  9. int screen_width = 320;
  10. int screen_height = 240;
  11. // Declares variables (uninitialized)
  12. // The location of the pingpong racket
  13. int racket_position;
  14. int racket2_position;
  15. // Set amount in seconds
  16. long interval;
  17. // Set the coordinates of the ball
  18. int ball_x;
  19. int ball_y;
  20. // Ball size specified by its radius
  21. int ball_r;
  22. // Acceleration
  23. int accel_x;
  24. int accel_y;
  25. // The player's score
  26. int score;
  27. // Second player's score
  28. int score2;
  29.  
  30. /*void ledBar(int R, int G, int B, int M) {
  31. if ((M < 0) || (M > 12)) return;
  32. if (M == 11) // right
  33. {
  34. for (int i = 0; i < 5; i++)
  35. {
  36. m_sLeds.pixels[i] = pixelFromRGBW(R, G, B, 0);
  37. }
  38. }
  39. else if (M == 10) // left
  40. {
  41. for (int i = 5; i < 10; i++)
  42. {
  43. m_sLeds.pixels[i] = pixelFromRGBW(R, G, B, 0);
  44. }
  45. }
  46. else if (M == 12) // all
  47. {
  48. for (int i = 0; i < 10; i++)
  49. {
  50. m_sLeds.pixels[i] = pixelFromRGBW(R, G, B, 0);
  51. }
  52. }
  53. else
  54. {
  55. m_sLeds.pixels[M] = pixelFromRGBW(R, G, B, 0);
  56. }
  57. digitalLeds_updatePixels(&m_sLeds);
  58. }*/
  59.  
  60. void start() {
  61. // Resets the screen to BLACK
  62. M5.Lcd.fillScreen(0x0000);
  63. // Set how long an interval is
  64. interval = 60;
  65. // Set the ball to be at the screen center
  66. ball_x = 160;
  67. ball_y = 120;
  68. ball_r = 6;
  69. // Ball starts moving from left top corner to right bottom corner
  70. accel_x = 5;
  71. accel_y = 5;
  72. score = 0;
  73. score2 = 0;
  74. // Draws the logo
  75. M5.Lcd.drawBitmap(0, 0, screen_width, screen_height, (uint16_t *)logo);
  76. // Set the size of the score text
  77. M5.Lcd.setTextSize(2);
  78. // Resets time count
  79. long previousMillis = 0;
  80. // Initializes and declares local interval variable
  81. long interval = 500;
  82. int color = 0x0000;
  83. // This while loop is the HOME menu of the game which has a text that alternates colors every interval count of seconds
  84. while (true)
  85. {
  86. unsigned long currentMillis = millis();
  87. // If the time counter exceeds the limit, reset time count
  88. if (currentMillis - previousMillis > interval) {
  89. previousMillis = currentMillis;
  90. color = (color == 0x7bef) ? 0xffff : 0x7bef;
  91. M5.Lcd.setCursor(35, 200);
  92. M5.Lcd.setTextColor(color);
  93. M5.Lcd.print("press any key to play");
  94. }
  95. M5.update();
  96. if (M5.BtnA.wasPressed() || M5.BtnB.wasPressed() || M5.BtnC.wasPressed()) break;
  97. }
  98. // Sets the screen to black and plays a sequence of sounds after the user STARTs the game
  99. M5.Lcd.fillScreen(0x0000);
  100. M5.Speaker.tone(800);
  101. delay(100);
  102. M5.Speaker.tone(1200);
  103. delay(100);
  104. M5.Speaker.mute();
  105. delay(500);
  106. M5.Lcd.fillScreen(0xffff);
  107. M5.Lcd.setCursor(120, 0);
  108. M5.Lcd.setTextColor(0x0000);
  109. M5.Lcd.print(0);
  110. M5.Lcd.setCursor(200, 0);
  111. M5.Lcd.setTextColor(0x0000);
  112. M5.Lcd.print(0);
  113. }
  114.  
  115. // This is the game loop
  116. void game() {
  117. // Draws the ball with set properties
  118. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, 0x7bef);
  119. long previousMillis = 0;
  120. while (true)
  121. {
  122. // Converts the voltage scale?? only for cases where voltage is limited
  123. int voltage = analogRead(36) * 3400 / 4096;
  124. int voltage2 = analogRead(35) * 3400 / 4096;
  125. int percentage = voltage * 100 / 3400;
  126. int percentage2 = voltage2 * 100 / 3400;
  127. // Scaling to integer values e.g 0 to 100 to 0 to 10 instead (32 percent becomes 3)
  128. racket_position = map(percentage, 0, 100, 0, 10);
  129. racket2_position = map(percentage2, 0, 100, 0, 10);
  130. // Declare the racket's properties
  131. int racket_width = 20;
  132. int racket_height = 40;
  133. int racket_margin = 10;
  134. int racket2_width = 20;
  135. int racket2_height = 40;
  136. int racket2_margin = 10;
  137. int x, y, color, x2, y2, color2, racket_x, racket_y, racket2_x, racket2_y;
  138. // Enables the racket to slide
  139. for (int i = 0; i < 10; i++)
  140. {
  141. // Condition for rendering the racket for both players/sides
  142. // This is for the left side
  143. if (i < 5)
  144. {
  145. x = 0;
  146. y = i * (racket_height + racket_margin);
  147. }
  148. // This is for the right side;
  149. else
  150. {
  151. x2 = screen_width - racket2_width;
  152. y2 = (9 - i) * (racket2_height + racket2_margin);
  153. }
  154. color = (i == racket_position) ? RED : WHITE;
  155. color2 = (i == racket2_position) ? RED : WHITE;
  156. M5.Lcd.fillRect(x, y, racket_width, racket_height, color);
  157. M5.Lcd.fillRect(x2, y2, racket2_width, racket2_height, color2);
  158. // ledBar(0, 0, 0, 12);
  159. // ledBar(255, 0, 0, 9 - racket_position);
  160. if (i == racket_position)
  161. {
  162. racket_x = x;
  163. racket_y = y;
  164. }
  165. if (i == racket2_position)
  166. {
  167. racket2_x = x2;
  168. racket2_y = y2;
  169. }
  170. }
  171. unsigned long currentMillis = millis();
  172. // This is where the global variable is used
  173. if (currentMillis - previousMillis > interval) {
  174. previousMillis = currentMillis;
  175. // Sets the previous ball position as WHITE, so it disappears
  176. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, WHITE);
  177. ball_x += accel_x;
  178. ball_y += accel_y;
  179. // Sets new ball position
  180. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, 0x7bef);
  181. if (ball_y <= 0)
  182. accel_y *= -1;
  183. if (ball_y >= 240)
  184. accel_y *= -1;
  185. // Makes the ball bounce off the racket
  186. if ((ball_x + ball_r >= racket2_x) && (ball_y + ball_r >= racket2_y) && (ball_y - ball_r <= racket2_y + racket2_height))
  187. {
  188. accel_x *= -1;
  189. accel_y *= 1;
  190. repulse();
  191. }
  192. if ((ball_x - ball_r <= racket_x + racket_width) && (ball_y + ball_r >= racket_y) && (ball_y - ball_r <= racket_y + racket_height))
  193. {
  194. accel_x *= -1;
  195. accel_y *= 1;
  196. repulse();
  197. }
  198. // PLAYER 1 WINS
  199. if (ball_x > screen_width) {
  200. score++;
  201.  
  202.  
  203. // Set the ball to be at the screen center
  204. ball_x = 160;
  205. ball_y = 120;
  206. ball_r = 6;
  207. // Ball starts moving from left top corner to right bottom corner
  208. accel_x = 5;
  209. accel_y = 5;
  210. }
  211. // PLAYER 2 WINS
  212. if (ball_x < 0) {
  213. score2++;
  214.  
  215.  
  216. // Set the ball to be at the screen center
  217. ball_x = 160;
  218. ball_y = 120;
  219. ball_r = 6;
  220. // Ball starts moving from left top corner to right bottom corner
  221. accel_x = -5;
  222. accel_y = -5;
  223. }
  224. p1wins();
  225. p2wins();
  226. }
  227. }
  228. }
  229.  
  230. // What happens after a rebound
  231. void repulse() {
  232. // Speeds up the game after each point earned
  233. interval -= (interval >= 25) ? 5 : 0;
  234. }
  235.  
  236. void p1wins() {
  237. M5.Lcd.setCursor(120, 0);
  238. M5.Lcd.setTextColor(0xffff);
  239. score == 0? M5.Lcd.print(0): M5.Lcd.print(score - 1);
  240. M5.Lcd.setCursor(120, 0);
  241. M5.Lcd.setTextColor(0x0000);
  242. M5.Lcd.print(score);
  243. }
  244.  
  245. void p2wins() {
  246. M5.Lcd.setCursor(200, 0);
  247. M5.Lcd.setTextColor(0xffff);
  248. score2 == 0? M5.Lcd.print(0): M5.Lcd.print(score2 - 1);
  249. M5.Lcd.setCursor(200, 0);
  250. M5.Lcd.setTextColor(0x0000);
  251. M5.Lcd.print(score2);
  252. }
  253.  
  254. void setup() {
  255. M5.begin();
  256. pinMode(36, INPUT);
  257. pinMode(35, INPUT);
  258. // pinMode(15, OUTPUT);
  259. // digitalWrite (15, LOW);
  260. /*
  261. if (digitalLeds_initStrands(&m_sLeds, 1)) {
  262. Serial.println("Can't init LED driver().");
  263. }
  264. digitalLeds_resetPixels(&m_sLeds);
  265. */
  266. }
  267.  
  268. void loop() {
  269. start();
  270. game();
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement