Advertisement
mayorBanana

Untitled

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