Advertisement
mayorBanana

Untitled

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