Advertisement
mayorBanana

Untitled

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