Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <string>
- #include <random>
- //GREEN, RED, BLUE
- #define white CRGB(255,255,255)
- #define off CRGB(0,0,0);
- #define tronBlue CRGB(226,10,255)
- #define tronRed CRGB(65,255,13)
- #define tronYellow CRGB(157,247,30)
- int grid[24][24];
- #define pin_left 2
- #define pin_mid 3
- #define pin_right 4
- #define num_leds 576
- #define len 24
- CRGB leds[num_leds];
- void setup() {
- Serial.begin(9600);
- analogReadResolution(24);
- randomSeed(analogRead(A0 + A1));
- FastLED.addLeds<WS2812B, pin_left, RGB>(leds, 192);
- FastLED.addLeds<WS2812B, pin_mid, RGB>(leds, 192, 192);
- FastLED.addLeds<WS2812B, pin_right, RGB>(leds, 384, 192);
- FastLED.setMaxPowerInVoltsAndMilliamps(5,500);
- FastLED.clear();
- FastLED.show();
- FastLED.setBrightness(10);
- }
- void loop() {
- tron(30);
- }
- struct tronPlayer {
- CRGB color;
- int direction;
- int num;
- int row;
- int col;
- bool alive = true;
- void update() {
- if (!alive) { //returns if player is dead
- return;
- }
- if (colision()) { //clears grid of player's tail
- alive = false;
- for (int i = 0; i < len; i++) {
- for (int j = 0; j < len; j++) {
- if (grid[i][j] == num) {
- grid[i][j] = 0;
- leds[id(i,j)] = off;
- }
- }
- }
- return;
- }
- if (willColide(direction)) { //assigns a new direction if a collision is detected in current direction
- int newDir = 0;
- if (!willColide(rightTurn(direction)) && !willColide(leftTurn(direction))) { //if both left and right are free: random turn
- int randomDir = rand() % 2;
- switch (randomDir) {
- case 0: newDir = leftTurn(direction);
- break;
- case 1: newDir = rightTurn(direction);
- break;
- }
- } else if(!willColide(leftTurn(direction))) { //if left turn is free: turn left
- newDir = leftTurn(direction);
- } else if (!willColide(rightTurn(direction))) { //if right turn is free: turn right
- newDir = rightTurn(direction);
- }
- direction = newDir;
- } else if (rand() % 100 > 92) { //adds some random movents 92% of time for variety, same logic as collision detection
- int newDir = 0;
- if (!willColide(rightTurn(direction)) && !willColide(leftTurn(direction))) {
- int randomDir = rand() % 2;
- switch (randomDir) {
- case 0: newDir = leftTurn(direction);
- break;
- case 1: newDir = rightTurn(direction);
- break;
- }
- } else if(!willColide(leftTurn(direction))) {
- newDir = leftTurn(direction);
- } else if (!willColide(rightTurn(direction))) {
- newDir = rightTurn(direction);
- } else {
- newDir = direction;
- }
- direction = newDir;
- }
- leds[id(row, col)] = color; //changes the current head to tail color
- grid[row][col] = num; //updates grid
- switch(direction) { //progresses player in specified direction
- case 0: col++;
- break;
- case 1: row++;
- break;
- case 2: col--;
- break;
- case 3: row--;
- break;
- }
- leds[id(row, col)] = white; //assigns new head to white
- }
- bool colision() { //collision occurs if grid reports another player's tail or if wall is reached
- return grid[row][col] > 0 || row > len - 1 || row < 0 || col > len - 1 || col < 0;
- }
- bool willColide(int dir) { //detects collisions with similar logic from collision but by direction
- switch (dir) {
- case 0: return (grid[row][col + 1] > 0 || col + 1 > 23);
- case 1: return (grid[row + 1][col] > 0 || row + 1 > 23);
- case 2: return (grid[row][col - 1] > 0 || col - 1 < 0);
- case 3: return (grid[row - 1][col] > 0 || row - 1 < 0);
- }
- }
- int rightTurn(int dir) { //updates direction with right turn
- if (dir == 0) {
- return 3;
- } else {
- return (dir - 1);
- }
- }
- int leftTurn(int dir) { //updates direction with left turn
- if (dir == 3) {
- return 0;
- } else {
- return (dir + 1);
- }
- }
- };
- void tron(int speed) {
- int playersAlive = 3; //initializes 3 players with random directions and grid positions
- tronPlayer redPlayer;
- redPlayer.color = tronRed;
- redPlayer.num = 1;
- redPlayer.direction = rand() % 3;
- redPlayer.row = rand() % 23;
- redPlayer.col = rand() % 23;
- tronPlayer bluePlayer;
- bluePlayer.color = tronBlue;
- bluePlayer.num = 2;
- bluePlayer.direction = rand() % 3;
- bluePlayer.row = rand() % 23;
- bluePlayer.col = rand() % 23;
- tronPlayer greenPlayer;
- greenPlayer.color = tronYellow;
- greenPlayer.num = 3;
- greenPlayer.direction = rand() % 3;
- greenPlayer.row = rand() % 23;
- greenPlayer.col = rand() % 23;
- resetGrid();
- while (playersAlive > 1) { //main game loop
- redPlayer.update(); //updates each player, will do nothing if player is dead
- bluePlayer.update();
- greenPlayer.update();
- delay(speed);
- FastLED.show();
- playersAlive = 0; //checks for current number of alive players.
- if (redPlayer.alive) {
- playersAlive += 1;
- }
- if (bluePlayer.alive) {
- playersAlive += 1;
- }
- if (greenPlayer.alive) {
- playersAlive += 1;
- }
- }
- for (int i = 0; i < 20; i++) { //lets the winning player stay on the grid for a 20 more loops
- if (redPlayer.alive || bluePlayer.alive || greenPlayer.alive) {
- redPlayer.update();
- bluePlayer.update();
- greenPlayer.update();
- delay(speed*2);
- FastLED.show();
- }
- }
- delay(1);
- FastLED.clear();
- }
- void resetGrid() { //resets grid for a new game
- for (int i = 0; i < len; i++) {
- for (int j = 0; j < len; j++) {
- grid[i][j] = 0;
- }
- }
- }
- int id(int rowId, int colId) { //mapping helper function for the current setup of matrices
- int offset = 0;
- int colFin = 0;
- if (!outOfBounds(rowId, colId)) {
- if (colId > 7 && colId < 16) {
- colFin = colId - 8;
- offset = 192;
- } else if (colId > 15 && colId < 24) {
- colFin = colId - 16;
- offset = 384;
- } else {
- colFin = colId;
- }
- if (rowId % 2 == 0) {
- return rowId * 8 + colFin + offset;
- } else {
- return rowId * 8 + (7 - colFin) + offset;
- }
- }
- }
- bool outOfBounds(int row, int col) {
- return (row < 0 && row > 23 && col < 0 && col > 23);
- }
Advertisement
Add Comment
Please, Sign In to add comment