Advertisement
mayorBanana

Untitled

Apr 20th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 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. int rarefood_x;
  61. int rarefood_y;
  62. int rare_time = 0;
  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 immortal = snake_size;
  71. int deadline = 0;
  72.  
  73. void spawnfood() {
  74. while (1) {
  75. food_x = random(0, WIDTH);
  76. food_y = random(0, HEIGHT);
  77. food_color.setRGB(random(50, 256), random(50, 256), random(50, 256));
  78.  
  79. bool in_snake = false;
  80. for (int i = 0; i < snake_size; i++) {
  81. if (pixel_x[i] == food_x && pixel_y[i] == food_y)
  82. in_snake = true;
  83. }
  84.  
  85. if (in_snake)
  86. continue;
  87. else
  88. break;
  89. }
  90. int super_chance = random(0, 101);
  91. if (super_chance >= 30){
  92. while (1) {
  93. rarefood_x = random(0, WIDTH);
  94. rarefood_y = random(0, HEIGHT);
  95. rare_time = random(10, 50);
  96.  
  97. bool in_snake = false;
  98. for (int i = 0; i < snake_size; i++) {
  99. if (pixel_x[i] == rarefood_x && pixel_y[i] == rarefood_y)
  100. in_snake = true;
  101. }
  102.  
  103. if (in_snake)
  104. continue;
  105. else
  106. break;
  107. }
  108. }
  109. }
  110.  
  111. void setup() {
  112. Serial.begin(115200);
  113. FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
  114. FastLED.setBrightness(50);
  115. srand(analogRead(A5));
  116. spawnfood();
  117. }
  118.  
  119. int snakecheck() {
  120. if (immortal > 0)
  121. return 0;
  122. for (int i = 0; i < snake_size; i++) {
  123. for (int j = 0; j < snake_size; j++) {
  124. if (i == j)
  125. continue;
  126. if (pixel_x[i] == pixel_x[j] && pixel_y[i] == pixel_y[j])
  127. return 1;
  128. }
  129. }
  130. return 0;
  131. }
  132.  
  133. // https://pastebin.com/ybrwtZH0
  134. void loop() {
  135. if (kbd.isPressed(1, 0) && pixel_vy >= 0) { //вверх
  136. pixel_vx = 0;
  137. pixel_vy = 1;
  138. } else if (kbd.isPressed(0, 1) && pixel_vx <= 0) {
  139. pixel_vx = -1;
  140. pixel_vy = 0;
  141. } else if (kbd.isPressed(2, 1) && pixel_vx >= 0) {
  142. pixel_vx = +1;
  143. pixel_vy = 0;
  144. } else if (kbd.isPressed(1, 2) && pixel_vy <= 0) {
  145. pixel_vx = 0;
  146. pixel_vy = -1;
  147. }
  148.  
  149. int now = millis();
  150. if (now >= deadline) {
  151. for (int i = 0; i < NUM_LEDS; i++)
  152. leds[i].setRGB(0, 0, 0);
  153.  
  154. for (int i = snake_size - 1; i > 0; i--) {
  155. pixel_x[i] = pixel_x[i - 1];
  156. pixel_y[i] = pixel_y[i - 1];
  157. }
  158. pixel_x[0] = pixel_x[1] + pixel_vx;
  159. pixel_y[0] = pixel_y[1] + pixel_vy;
  160. if (pixel_x[0] < 0)
  161. pixel_x[0] = WIDTH - 1;
  162. if (pixel_x[0] >= WIDTH)
  163. pixel_x[0] = 0;
  164. if (pixel_y[0] < 0)
  165. pixel_y[0] = HEIGHT - 1;
  166. if (pixel_y[0] >= HEIGHT)
  167. pixel_y[0] = 0;
  168.  
  169. if (snakecheck()) {
  170. for (int i = 0; i < NUM_LEDS; i++)
  171. leds[i].setRGB(50, 0, 0);
  172.  
  173. FastLED.show();
  174. unsigned int x;
  175. while (true) {
  176. x = 0;
  177. }
  178. }
  179.  
  180. for (int i = 0; i < snake_size; i++) {
  181. if (i == 0) {
  182. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 0, 0);
  183. } else if (i == snake_size - 1) {
  184. leds[index(pixel_x[i], pixel_y[i])].setRGB(255, 255, 0);
  185. } else if (i % 2 == 0) {
  186. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 0, 255);
  187. } else {
  188. leds[index(pixel_x[i], pixel_y[i])].setRGB(0, 255, 0);
  189. }
  190. }
  191.  
  192. leds[index(food_x, food_y)] = food_color;
  193. if (food_x == pixel_x[0] && food_y == pixel_y[0]) {
  194. if (snake_size + 1 <= SNAKE_CAPACITY)
  195. snake_size += 1;
  196. spawnfood();
  197. }
  198.  
  199. if (rare_time > 0)
  200. {
  201. leds[index(rarefood_x, rarefood_y)].setRGB(255, 0, 0);
  202. if (rarefood_x == pixel_x[0] && rarefood_y == pixel_y[0]) {
  203. int max_growth = SNAKE_CAPACITY - snake_size;
  204. if (max_growth > 3)
  205. max_growth = 3;
  206.  
  207. if (max_growth > 0)
  208. {
  209. for (int i = snake_size; i < snake_size + max_growth; i++){
  210. pixel_x[i] = pixel_x[i - 1];
  211. pixel_y[i] = pixel_y[i - 1];
  212. }
  213. immortal = max_growth;
  214. snake_size += max_growth;
  215. }
  216. rare_time = 0;
  217. spawnfood();
  218. }
  219. }
  220. FastLED.show();
  221.  
  222. deadline = now + 250;
  223. Serial.println(snake_size);
  224. tick++;
  225. if (immortal > 0)
  226. immortal--;
  227. if (rare_time > 0)
  228. rare_time--;
  229. }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement