Advertisement
mayorBanana

Untitled

Apr 13th, 2024
40
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 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_SIZE 9
  65. int pixel_x[SNAKE_SIZE] = { 0 };
  66. int pixel_y[SNAKE_SIZE] = { 0 };
  67. int pixel_vx = 0;
  68. int pixel_vy = 1;
  69. int tick = 0;
  70. int deadline = 0;
  71. int snakecheck ()
  72. {
  73. for(int i = 0; i < SNAKE_SIZE; i++)
  74. {
  75. for(int j = 0; j < SNAKE_SIZE; j++)
  76. {
  77. if (i == j)
  78. continue;
  79. if (pixel_x[i] == pixel_x[j] && pixel_y[i] == pixel_y[j])
  80. return 1;
  81.  
  82. }
  83. }
  84. return 0;
  85. }
  86. // https://pastebin.com/N6L2xhnZ
  87. void loop() {
  88. if (kbd.isPressed(1, 0) && pixel_vy >= 0) { //вверх
  89. pixel_vx = 0;
  90. pixel_vy = 1;
  91. } else if (kbd.isPressed(0, 1) && pixel_vx <= 0) {
  92. pixel_vx = -1;
  93. pixel_vy = 0;
  94. } else if (kbd.isPressed(2, 1) && pixel_vx >= 0) {
  95. pixel_vx = +1;
  96. pixel_vy = 0;
  97. } else if (kbd.isPressed(1, 2) && pixel_vy <= 0) {
  98. pixel_vx = 0;
  99. pixel_vy = -1;
  100. }
  101.  
  102. int now = millis();
  103. if (now >= deadline) {
  104. for (int i = 0; i < NUM_LEDS; i++)
  105. leds[i].setRGB(0, 0, 0);
  106.  
  107. for (int i = SNAKE_SIZE - 1; i > 0; i--) {
  108. pixel_x[i] = pixel_x[i - 1];
  109. pixel_y[i] = pixel_y[i - 1];
  110. }
  111. pixel_x[0] = pixel_x[1] + pixel_vx;
  112. pixel_y[0] = pixel_y[1] + pixel_vy;
  113. if (pixel_x[0] < 0)
  114. pixel_x[0] = WIDTH - 1;
  115. if (pixel_x[0] >= WIDTH)
  116. pixel_x[0] = 0;
  117. if (pixel_y[0] < 0)
  118. pixel_y[0] = HEIGHT - 1;
  119. if (pixel_y[0] >= HEIGHT)
  120. pixel_y[0] = 0;
  121.  
  122. if (snakecheck() && tick >= SNAKE_SIZE)
  123. {
  124. for (int i = 0; i < NUM_LEDS; i++)
  125. leds[i].setRGB(50, 0, 0);
  126.  
  127. FastLED.show();
  128. unsigned int x;
  129. while (true){
  130. x = 0;
  131. }
  132. }
  133. for (int i = 0; i < SNAKE_SIZE; i++)
  134. if (i == 0) {
  135. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 0, 0);
  136. } else if(i == SNAKE_SIZE - 1) {
  137. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 255, 0);
  138. }
  139. else{
  140. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 255, 0);
  141. }
  142. FastLED.show();
  143.  
  144. deadline = now + 500;
  145. tick++;
  146. }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement