Advertisement
mayorBanana

Untitled

May 11th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define WIDTH 32
  5. #define HEIGHT 8
  6. #define NUM_LEDS (WIDTH * HEIGHT)
  7. CRGB leds[NUM_LEDS];
  8.  
  9. // https://pastebin.com/UzSiw8yE
  10. int index(int x, int y) {
  11. int rv;
  12. if (x % 2 == 0)
  13. rv = x * HEIGHT + y;
  14. else
  15. rv = x * HEIGHT + HEIGHT - y - 1;
  16. return rv;
  17. }
  18.  
  19. float ball_x = 0;
  20. float ball_y = 0;
  21.  
  22. float ball_vx = 0;
  23. float ball_vy = 0;
  24.  
  25. int player_1 = 0;
  26. int player_2 = 0;
  27. int score_1 = 0;
  28. int score_2 = 0;
  29.  
  30. const int MAX_BR = 255;
  31. const int X_pos_1 = 1;
  32. const int X_pos_2 = WIDTH - 2;
  33. const int palca = 3;
  34.  
  35. #define rezistor_1 A0
  36. #define rezistor_2 A1
  37.  
  38. void setup() {
  39. Serial.begin(115200);
  40. FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
  41. FastLED.setBrightness(50);
  42. pinMode(rezistor_1, INPUT);
  43. pinMode(rezistor_2, INPUT);
  44. ball_x = WIDTH / 2;
  45. ball_y = HEIGHT / 2;
  46. ball_vx = 0.1;
  47. ball_vy = 0.05;
  48. }
  49.  
  50. bool intersects(float x1, float y1, float x2, float y2, float vert_x, float vert_y1, float vert_y2){
  51. bool case1 = !((x2 > x1) && (vert_x >= x1) && (vert_x <= x2));
  52. bool case2 = !((x1 > x2) && (vert_x >= x2) && (vert_x <= x1));
  53. if (case1 && case2)
  54. return false;
  55.  
  56. float K = (y2-y1) / (x2-x1);
  57. float B = y1 - K*x1;
  58. float vert_y = K * vert_x + B;
  59.  
  60. if((vert_y2 >= vert_y1) && (vert_y >= vert_y1) && (vert_y <= vert_y2))
  61. return true;
  62. if((vert_y1 > vert_y2) && (vert_y >= vert_y2) && (vert_y <= vert_y1))
  63. return true;
  64.  
  65. return false;
  66. }
  67.  
  68. void loop() {
  69. player_1 = map(analogRead(rezistor_1), 0, 1020, 0, HEIGHT - palca);
  70. player_2 = map(analogRead(rezistor_2), 0, 1020, 0, HEIGHT - palca);
  71. Serial.print("p1=");
  72. Serial.print(player_1);
  73. Serial.print(" p2=");
  74. Serial.print(player_2);
  75. Serial.println();
  76.  
  77. float ball_x_new = ball_x + ball_vx;
  78. float ball_y_new = ball_y + ball_vy;
  79. if (ball_y_new < 0 || ball_y_new >= HEIGHT) {
  80. ball_vy = -ball_vy;
  81. ball_y_new = ball_y + ball_vy;
  82. }
  83. else if (ball_x_new < 0){
  84. score_2 = score_2 + 1;
  85. ball_x_new = X_pos_1 + 2;
  86. ball_y_new = player_1 + palca / 2.0;
  87. ball_vx = 0.1;
  88. ball_vy = 0;
  89. }
  90. else if (ball_x_new >= WIDTH){
  91. score_1 = score_1 + 1;
  92. ball_x_new = X_pos_2 - 2;
  93. ball_y_new = player_2 + palca / 2.0;
  94. ball_vx = -0.1;
  95. ball_vy = 0;
  96. }
  97. else if(intersects(ball_x, ball_y, ball_x_new, ball_y_new, X_pos_1+1, player_1, player_1 + palca - 1)){
  98. ball_vx = -ball_vx;
  99. ball_x_new = ball_x + ball_vx;
  100. }
  101. else if(intersects(ball_x, ball_y, ball_x_new, ball_y_new, X_pos_2, player_2, player_2 + palca - 1)){
  102. ball_vx = -ball_vx;
  103. ball_x_new = ball_x + ball_vx;
  104. }
  105. ball_x = ball_x_new;
  106. ball_y = ball_y_new;
  107.  
  108. // Serial.print(ball_x);
  109. // Serial.print(" ");
  110. // Serial.print(ball_y);
  111. // Serial.println();
  112.  
  113. for (int i = 0; i < NUM_LEDS; i++)
  114. leds[i].setRGB(0, 0, 0);
  115.  
  116. for (int i = 0; i < palca; i++) {
  117. leds[index(X_pos_1, i + player_1)].setRGB(0, 0, MAX_BR);
  118. leds[index(X_pos_2, i + player_2)].setRGB(MAX_BR, 0, 0);
  119. }
  120.  
  121. leds[index(ball_x, ball_y)].setRGB(MAX_BR, MAX_BR, MAX_BR);
  122. FastLED.show();
  123. delay(10);
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement