Advertisement
mayorBanana

Untitled

Apr 13th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define WIDTH 8
  5. #define HEIGHT 8
  6. #define NUM_LEDS (WIDTH * HEIGHT)
  7. CRGB leds[NUM_LEDS];
  8.  
  9.  
  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.  
  18. class keyboard_t {
  19. private:
  20. int row_pins[4];
  21. int col_pins[4];
  22.  
  23. public:
  24. keyboard_t(int rows[4], int cols[4]) {
  25. for (int i = 0; i < 4; i++) {
  26. int row_pin = rows[i];
  27. row_pins[i] = row_pin;
  28. pinMode(row_pin, INPUT_PULLUP);
  29. }
  30.  
  31. for (int i = 0; i < 4; i++) {
  32. int col_pin = cols[i];
  33. col_pins[i] = col_pin;
  34. pinMode(col_pin, INPUT);
  35. }
  36. }
  37.  
  38. bool isPressed(int x, int y) {
  39. int col_pin_no = this->col_pins[x];
  40. pinMode(col_pin_no, OUTPUT);
  41. digitalWrite(col_pin_no, LOW);
  42.  
  43. int row_pin_no = this->row_pins[y];
  44. int return_value = digitalRead(row_pin_no);
  45.  
  46. pinMode(col_pin_no, INPUT);
  47. if (return_value != 0)
  48. return 0;
  49. else
  50. return 1;
  51. }
  52. };
  53.  
  54. int row_pins[] = { 2, 3, 4, 5 };
  55. int col_pins[] = { A0, A1, A2, A3 };
  56. keyboard_t kbd(row_pins, col_pins);
  57.  
  58.  
  59. void setup() {
  60. Serial.begin(115200);
  61. FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS);
  62. }
  63.  
  64. #define SNAKE_CAPACITY 20
  65. int snake_size = 2;
  66.  
  67. int pixel_x[SNAKE_CAPACITY] = { 0 };
  68. int pixel_y[SNAKE_CAPACITY] = { 0 };
  69. int pixel_vx = 0;
  70. int pixel_vy = 1;
  71. int tick = 0;
  72. int deadline = 0;
  73. int snakecheck ()
  74. {
  75. for(int i = 0; i < snake_size; i++)
  76. {
  77. for(int j = 0; j < snake_size; j++)
  78. {
  79. if (i == j)
  80. continue;
  81. if (pixel_x[i] == pixel_x[j] && pixel_y[i] == pixel_y[j])
  82. return 1;
  83.  
  84. }
  85. }
  86. return 0;
  87. }
  88.  
  89. // https://pastebin.com/PkyizR0X
  90. void loop() {
  91. if (kbd.isPressed(1, 0) && pixel_vy >= 0) { //вверх
  92. pixel_vx = 0;
  93. pixel_vy = 1;
  94. } else if (kbd.isPressed(0, 1) && pixel_vx <= 0) {
  95. pixel_vx = -1;
  96. pixel_vy = 0;
  97. } else if (kbd.isPressed(2, 1) && pixel_vx >= 0) {
  98. pixel_vx = +1;
  99. pixel_vy = 0;
  100. } else if (kbd.isPressed(1, 2) && pixel_vy <= 0) {
  101. pixel_vx = 0;
  102. pixel_vy = -1;
  103. }
  104.  
  105. int now = millis();
  106. if (now >= deadline) {
  107. for (int i = 0; i < NUM_LEDS; i++)
  108. leds[i].setRGB(0, 0, 0);
  109.  
  110. for (int i = snake_size - 1; i > 0; i--) {
  111. pixel_x[i] = pixel_x[i - 1];
  112. pixel_y[i] = pixel_y[i - 1];
  113. }
  114. pixel_x[0] = pixel_x[1] + pixel_vx;
  115. pixel_y[0] = pixel_y[1] + pixel_vy;
  116. if (pixel_x[0] < 0)
  117. pixel_x[0] = WIDTH - 1;
  118. if (pixel_x[0] >= WIDTH)
  119. pixel_x[0] = 0;
  120. if (pixel_y[0] < 0)
  121. pixel_y[0] = HEIGHT - 1;
  122. if (pixel_y[0] >= HEIGHT)
  123. pixel_y[0] = 0;
  124.  
  125. if (snakecheck() && tick >= snake_size)
  126. {
  127. for (int i = 0; i < NUM_LEDS; i++)
  128. leds[i].setRGB(50, 0, 0);
  129.  
  130. FastLED.show();
  131. unsigned int x;
  132. while (true){
  133. x = 0;
  134. }
  135. }
  136. for (int i = 0; i < snake_size; i++)
  137. if (i == 0) {
  138. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 0, 0);
  139. } else if(i == snake_size - 1) {
  140. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 255, 0);
  141. }
  142. else{
  143. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 255, 0);
  144. }
  145. FastLED.show();
  146. if (tick %10 == 0 && snake_size < SNAKE_CAPACITY - 1)
  147. snake_size += 1;
  148.  
  149.  
  150. deadline = now + 500;
  151. tick++;
  152. }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement