Guest User

Untitled

a guest
Mar 27th, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.27 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <string>
  3. #include <random>
  4. //GREEN, RED, BLUE
  5. #define white CRGB(255,255,255)
  6. #define off CRGB(0,0,0);
  7. #define tronBlue CRGB(226,10,255)
  8. #define tronRed CRGB(65,255,13)
  9. #define tronYellow CRGB(157,247,30)
  10. int grid[24][24];
  11.  
  12. #define pin_left 2
  13. #define pin_mid 3
  14. #define pin_right 4
  15. #define num_leds 576
  16. #define len 24
  17.  
  18. CRGB leds[num_leds];
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   analogReadResolution(24);
  23.   randomSeed(analogRead(A0 + A1));
  24.   FastLED.addLeds<WS2812B, pin_left, RGB>(leds, 192);
  25.   FastLED.addLeds<WS2812B, pin_mid, RGB>(leds, 192, 192);
  26.   FastLED.addLeds<WS2812B, pin_right, RGB>(leds, 384, 192);
  27.   FastLED.setMaxPowerInVoltsAndMilliamps(5,500);
  28.   FastLED.clear();
  29.   FastLED.show();
  30.   FastLED.setBrightness(10);
  31. }
  32.  
  33. void loop() {
  34.   tron(30);
  35. }
  36.  
  37. struct tronPlayer {
  38.   CRGB color;
  39.   int direction;
  40.   int num;
  41.   int row;
  42.   int col;
  43.   bool alive = true;
  44.   void update() {
  45.     if (!alive) { //returns if player is dead
  46.       return;
  47.     }
  48.     if (colision()) { //clears grid of player's tail
  49.       alive = false;
  50.       for (int i = 0; i < len; i++) {
  51.         for (int j = 0; j < len; j++) {
  52.           if (grid[i][j] == num) {
  53.             grid[i][j] = 0;
  54.             leds[id(i,j)] = off;
  55.           }
  56.         }
  57.       }
  58.       return;
  59.     }
  60.     if (willColide(direction)) { //assigns a new direction if a collision is detected in current direction
  61.       int newDir = 0;
  62.       if (!willColide(rightTurn(direction)) && !willColide(leftTurn(direction))) { //if both left and right are free: random turn
  63.         int randomDir = rand() % 2;
  64.         switch (randomDir) {
  65.           case 0: newDir = leftTurn(direction);
  66.           break;
  67.           case 1: newDir = rightTurn(direction);
  68.           break;
  69.         }
  70.       } else if(!willColide(leftTurn(direction))) { //if left turn is free: turn left
  71.         newDir = leftTurn(direction);
  72.       } else if (!willColide(rightTurn(direction))) { //if right turn is free: turn right
  73.         newDir = rightTurn(direction);
  74.       }
  75.       direction = newDir;
  76.     } else if (rand() % 100 > 92) { //adds some random movents 92% of time for variety, same logic as collision detection
  77.       int newDir = 0;
  78.       if (!willColide(rightTurn(direction)) && !willColide(leftTurn(direction))) {
  79.         int randomDir = rand() % 2;
  80.         switch (randomDir) {
  81.           case 0: newDir = leftTurn(direction);
  82.           break;
  83.           case 1: newDir = rightTurn(direction);
  84.           break;
  85.         }
  86.       } else if(!willColide(leftTurn(direction))) {
  87.         newDir = leftTurn(direction);
  88.       } else if (!willColide(rightTurn(direction))) {
  89.         newDir = rightTurn(direction);
  90.       } else {
  91.         newDir = direction;
  92.       }
  93.       direction = newDir;
  94.     }
  95.     leds[id(row, col)] = color; //changes the current head to tail color
  96.     grid[row][col] = num; //updates grid
  97.     switch(direction) { //progresses player in specified direction
  98.       case 0: col++;
  99.       break;
  100.       case 1: row++;
  101.       break;
  102.       case 2: col--;
  103.       break;
  104.       case 3: row--;
  105.       break;
  106.     }
  107.     leds[id(row, col)] = white; //assigns new head to white
  108.   }
  109.   bool colision() { //collision occurs if grid reports another player's tail or if wall is reached
  110.     return grid[row][col] > 0 || row > len - 1 || row < 0 || col > len - 1 || col < 0;
  111.   }
  112.   bool willColide(int dir) { //detects collisions with similar logic from collision but by direction
  113.     switch (dir) {
  114.       case 0: return (grid[row][col + 1] > 0 || col + 1 > 23);
  115.       case 1: return (grid[row + 1][col] > 0 || row + 1 > 23);
  116.       case 2: return (grid[row][col - 1] > 0 || col - 1 < 0);
  117.       case 3: return (grid[row - 1][col] > 0 || row - 1 < 0);
  118.     }
  119.   }
  120.   int rightTurn(int dir) { //updates direction with right turn
  121.     if (dir == 0) {
  122.       return 3;
  123.     } else {
  124.       return (dir - 1);
  125.     }
  126.   }
  127.   int leftTurn(int dir) { //updates direction with left turn
  128.     if (dir == 3) {
  129.       return 0;
  130.     } else {
  131.       return (dir + 1);
  132.     }
  133.   }
  134. };
  135.  
  136. void tron(int speed) {
  137.   int playersAlive = 3; //initializes 3 players with random directions and grid positions
  138.   tronPlayer redPlayer;
  139.   redPlayer.color = tronRed;
  140.   redPlayer.num = 1;
  141.   redPlayer.direction = rand() % 3;
  142.   redPlayer.row = rand() % 23;
  143.   redPlayer.col = rand() % 23;
  144.   tronPlayer bluePlayer;
  145.   bluePlayer.color = tronBlue;
  146.   bluePlayer.num = 2;
  147.   bluePlayer.direction = rand() % 3;
  148.   bluePlayer.row = rand() % 23;
  149.   bluePlayer.col = rand() % 23;
  150.   tronPlayer greenPlayer;
  151.   greenPlayer.color = tronYellow;
  152.   greenPlayer.num = 3;
  153.   greenPlayer.direction = rand() % 3;
  154.   greenPlayer.row = rand() % 23;
  155.   greenPlayer.col = rand() % 23;
  156.   resetGrid();
  157.   while (playersAlive > 1) { //main game loop
  158.     redPlayer.update(); //updates each player, will do nothing if player is dead
  159.     bluePlayer.update();
  160.     greenPlayer.update();
  161.     delay(speed);
  162.     FastLED.show();
  163.     playersAlive = 0; //checks for current number of alive players.
  164.     if (redPlayer.alive) {
  165.       playersAlive += 1;
  166.     }
  167.     if (bluePlayer.alive) {
  168.       playersAlive += 1;
  169.     }
  170.     if (greenPlayer.alive) {
  171.       playersAlive += 1;
  172.     }
  173.   }
  174.   for (int i = 0; i < 20; i++) { //lets the winning player stay on the grid for a 20 more loops
  175.     if (redPlayer.alive || bluePlayer.alive || greenPlayer.alive) {
  176.       redPlayer.update();
  177.       bluePlayer.update();
  178.       greenPlayer.update();
  179.       delay(speed*2);
  180.       FastLED.show();
  181.     }
  182.   }
  183.   delay(1);
  184.   FastLED.clear();
  185. }
  186.  
  187. void resetGrid() { //resets grid for a new game
  188.   for (int i = 0; i < len; i++) {
  189.     for (int j = 0; j < len; j++) {
  190.       grid[i][j] = 0;
  191.     }
  192.   }
  193. }
  194.  
  195. int id(int rowId, int colId) { //mapping helper function for the current setup of matrices
  196.   int offset = 0;
  197.   int colFin = 0;
  198.   if (!outOfBounds(rowId, colId)) {
  199.     if (colId > 7 && colId < 16) {
  200.       colFin = colId - 8;
  201.       offset = 192;
  202.     } else if (colId > 15 && colId < 24) {
  203.       colFin = colId - 16;
  204.       offset = 384;
  205.     } else {
  206.       colFin = colId;
  207.     }
  208.     if (rowId % 2 == 0) {
  209.       return rowId * 8 + colFin + offset;
  210.     } else {
  211.       return rowId * 8 + (7 - colFin) + offset;
  212.     }
  213.   }
  214. }
  215.  
  216. bool outOfBounds(int row, int col) {
  217.   return (row < 0 && row > 23 && col < 0 && col > 23);
  218. }
Tags: FastLED tron
Advertisement
Add Comment
Please, Sign In to add comment