Advertisement
mayorBanana

Untitled

Apr 13th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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. #define SNAKE_CAPACITY 20
  60. int snake_size = 2;
  61. int food_x;
  62. int food_y;
  63. CRGB food_color;
  64.  
  65. int pixel_x[SNAKE_CAPACITY] = { 0 };
  66. int pixel_y[SNAKE_CAPACITY] = { 0 };
  67. int pixel_vx = 0;
  68. int pixel_vy = 1;
  69. int tick = 0;
  70. int deadline = 0;
  71.  
  72. void spawnfood()
  73. {
  74. while(1)
  75. {
  76. food_x = random(0, WIDTH);
  77. food_y = random(0, HEIGHT);
  78. food_color.setRGB(random(50, 256), random(50, 256), random(50, 256));
  79.  
  80. bool in_snake = false;
  81. for (int i = 0; i < snake_size; i++)
  82. {
  83. if (pixel_x[i] == food_x && pixel_y[i] == food_y)
  84. in_snake = true;
  85. }
  86.  
  87. if (in_snake)
  88. continue;
  89. else
  90. break;
  91. }
  92. }
  93.  
  94. void setup() {
  95. Serial.begin(115200);
  96. FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS);
  97. srand(analogRead(A5));
  98. spawnfood();
  99. }
  100.  
  101. int snakecheck ()
  102. {
  103. for(int i = 0; i < snake_size; i++)
  104. {
  105. for(int j = 0; j < snake_size; j++)
  106. {
  107. if (i == j)
  108. continue;
  109. if (pixel_x[i] == pixel_x[j] && pixel_y[i] == pixel_y[j])
  110. return 1;
  111.  
  112. }
  113. }
  114. return 0;
  115. }
  116.  
  117. // https://pastebin.com/sJ8EgyH1
  118. void loop() {
  119. if (kbd.isPressed(1, 0) && pixel_vy >= 0) { //вверх
  120. pixel_vx = 0;
  121. pixel_vy = 1;
  122. } else if (kbd.isPressed(0, 1) && pixel_vx <= 0) {
  123. pixel_vx = -1;
  124. pixel_vy = 0;
  125. } else if (kbd.isPressed(2, 1) && pixel_vx >= 0) {
  126. pixel_vx = +1;
  127. pixel_vy = 0;
  128. } else if (kbd.isPressed(1, 2) && pixel_vy <= 0) {
  129. pixel_vx = 0;
  130. pixel_vy = -1;
  131. }
  132.  
  133. int now = millis();
  134. if (now >= deadline) {
  135. for (int i = 0; i < NUM_LEDS; i++)
  136. leds[i].setRGB(0, 0, 0);
  137.  
  138. for (int i = snake_size - 1; i > 0; i--) {
  139. pixel_x[i] = pixel_x[i - 1];
  140. pixel_y[i] = pixel_y[i - 1];
  141. }
  142. pixel_x[0] = pixel_x[1] + pixel_vx;
  143. pixel_y[0] = pixel_y[1] + pixel_vy;
  144. if (pixel_x[0] < 0)
  145. pixel_x[0] = WIDTH - 1;
  146. if (pixel_x[0] >= WIDTH)
  147. pixel_x[0] = 0;
  148. if (pixel_y[0] < 0)
  149. pixel_y[0] = HEIGHT - 1;
  150. if (pixel_y[0] >= HEIGHT)
  151. pixel_y[0] = 0;
  152.  
  153. if (snakecheck() && tick >= snake_size)
  154. {
  155. for (int i = 0; i < NUM_LEDS; i++)
  156. leds[i].setRGB(50, 0, 0);
  157.  
  158. FastLED.show();
  159. unsigned int x;
  160. while (true){
  161. x = 0;
  162. }
  163. }
  164. for (int i = 0; i < snake_size; i++) {
  165. if (i == 0) {
  166. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 0, 0);
  167. } else if(i == snake_size - 1) {
  168. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 255, 0);
  169. }
  170. else{
  171. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 255, 0);
  172. }
  173. }
  174. leds[index(food_x, food_y)] = food_color;
  175.  
  176. FastLED.show();
  177. if (tick %10 == 0 && snake_size < SNAKE_CAPACITY - 1)
  178. {
  179. //snake_size += 1;
  180. spawnfood();
  181. }
  182.  
  183. deadline = now + 500;
  184. tick++;
  185. }
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement