Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <M5Stack.h>
  2.  
  3. extern unsigned char logo[];
  4. int screen_width = 320;
  5. int screen_height = 240;
  6. int raket_position;
  7. int raket2_position;
  8. long interval;
  9. int ball_x;
  10. int ball_y;
  11. int ball_r;
  12. int accel_x;
  13. int accel_y;
  14. int score = 0;
  15. int score2 = 0;
  16.  
  17.  
  18. void start() {
  19. M5.Lcd.fillScreen(0x0000);
  20. interval = 60;
  21. ball_x = 160;
  22. ball_y = 120;
  23. ball_r = 6;
  24. accel_x = 5;
  25. accel_y = 5;
  26. score = 0;
  27.  
  28. M5.Lcd.drawBitmap(0, 0, screen_width, screen_height, (uint16_t *)logo);
  29. M5.Lcd.setTextSize(2);
  30. long previousMillis = 0;
  31. long interval = 500;
  32. int color = 0x0000;
  33. while(true)
  34. {
  35. unsigned long currentMillis = millis();
  36. if (currentMillis - previousMillis > interval) {
  37. previousMillis = currentMillis;
  38. color = (color == 0x7bef) ? 0xffff : 0x7bef;
  39. M5.Lcd.setCursor(35, 200);
  40. M5.Lcd.setTextColor(color);
  41. M5.Lcd.print("press any key to play");
  42. }
  43. M5.update();
  44. if (M5.BtnA.wasPressed() || M5.BtnB.wasPressed() || M5.BtnC.wasPressed()) break;
  45. }
  46. M5.Lcd.fillScreen(0x0000);
  47. M5.Speaker.tone(800);
  48. delay(100);
  49. M5.Speaker.tone(1200);
  50. delay(100);
  51. M5.Speaker.mute();
  52. delay(500);
  53. M5.Lcd.fillScreen(0xffff);
  54. }
  55.  
  56. void game() {
  57. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, 0x7bef);
  58. M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
  59. M5.Lcd.drawNumber(score, 100,20,2);
  60. M5.Lcd.drawNumber(score2, 100,20,2);
  61.  
  62. long previousMillis = 0;
  63. while (true)
  64. {
  65. int voltage = analogRead(36) * 3400 / 4096;
  66. int voltage2 = analogRead(35) * 3400 / 4096;
  67. int percentage = voltage * 100 / 3400;
  68. int percentage2 = voltage2 * 100 /3400;
  69. raket_position = map(percentage, 0, 100, 0, 5);
  70. raket2_position = map(percentage2, 0, 100, 0, 5);
  71. int raket_width = 20;
  72. int raket_height = 40;
  73. int raket_margin = 10;
  74. int x, y, x2, y2, color, raket_x, raket_y, raket2_x, raket2_y;
  75. for (int i = 0; i < 5; i++)
  76. {
  77. x = 0;
  78. y = i * (raket_height + raket_margin);
  79. color = (i == raket_position) ? RED : WHITE;
  80. M5.Lcd.fillRect(x, y, raket_width, raket_height, color);
  81. if (i == raket_position)
  82. {
  83. raket_x = x;
  84. raket_y = y;
  85. }
  86. }
  87. for (int i = 0; i < 5; i++)
  88. {
  89. x2 = screen_width - raket_width;
  90. y2 = i * (raket_height + raket_margin);
  91. color = (i == raket2_position) ? RED : WHITE;
  92. M5.Lcd.fillRect(x2, y2, raket_width, raket_height, color);
  93. if (i == raket2_position)
  94. {
  95. raket2_x = x2;
  96. raket2_y = y2;
  97. }
  98. }
  99. unsigned long currentMillis = millis();
  100. if (currentMillis - previousMillis > interval) {
  101. previousMillis = currentMillis;
  102. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, WHITE);
  103. ball_x += accel_x;
  104. ball_y += accel_y;
  105. M5.Lcd.fillCircle(ball_x, ball_y, ball_r, 0x7bef);
  106. if (ball_y <= 0)
  107. accel_y *= -1;
  108. if (ball_y >= 240)
  109. accel_y *= -1;
  110. if ((ball_x + ball_r >= raket2_x) && (ball_y + ball_r >= raket2_y) && (ball_y - ball_r <= raket2_y + raket_height))
  111. {
  112. accel_x *= -1;
  113. accel_y *= 1;
  114. score++;
  115. }
  116. if ((ball_x - ball_r <= raket_x + raket_width) && (ball_y + ball_r >= raket_y) && (ball_y - ball_r <= raket_y + raket_height))
  117. {
  118. accel_x *= -1;
  119. accel_y *= 1;
  120. score2++;
  121. }
  122. // if ((ball_x < 0) || (ball_x > screen_width) || (ball_y < 0) || (ball_y > screen_height))
  123. // {
  124. // return;
  125. // }
  126. if(ball_x <0 ){
  127. ball_x = screen_width;
  128. }
  129. if(ball_x > screen_width){
  130. ball_x = 0;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. void repulse() {
  137. // M5.Speaker.tone(1800);
  138. // delay(50);
  139. // M5.Speaker.mute();
  140. score++;
  141. interval -= (interval >= 25) ? 5 : 0;
  142. }
  143.  
  144. void repulse2() {
  145. // M5.Speaker.tone(1800);
  146. // delay(50);
  147. // M5.Speaker.mute();
  148. score2++;
  149. interval -= (interval >= 25) ? 5 : 0;
  150. }
  151.  
  152.  
  153. void setup(){
  154. M5.begin();
  155. pinMode(36, INPUT);
  156. pinMode(35, INPUT);
  157. }
  158.  
  159. void loop() {
  160. start();
  161. game();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement