Guest User

LedRacer

a guest
Mar 7th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.49 KB | None | 0 0
  1. #include <FastLED.h>;
  2.  
  3. //////////////////////////////////////// DEFINES
  4.  
  5. #define BUTTONPINP1LEFTY 27
  6. #define BUTTONPINP1RIGHTY 26
  7. #define BUTTONPINP2LEFTY 9
  8. #define BUTTONPINP2RIGHTY 8
  9. #define BUTTONPINP3LEFTY 11
  10. #define BUTTONPINP3RIGHTY 10
  11. #define BUTTONPINP4LEFTY 25
  12. #define BUTTONPINP4RIGHTY 24
  13.  
  14. #define LED1 5
  15. #define LED2 4
  16. #define LED3 3
  17. #define LED4 2
  18.  
  19. #define LEFTY 0
  20. #define RIGHTY 1
  21. #define LASTLEFTY 2
  22. #define LASTRIGHTY 3
  23.  
  24. #define VELOCITY 0
  25. #define FLOATPOS 1
  26. #define SPEED 2
  27. #define ACCELERATION 3
  28.  
  29. #define LANE 0
  30. #define POS 1
  31. #define OLDPOS 2
  32.  
  33. ///////////////////////////////////////// redOrbInfo
  34.  
  35. int redOrbAmount = 8;
  36. float redOrbMov[8][2]; // 0 VELOCITY  1 FLOATPOS
  37. int redOrbPos[8][2];   // 0 LANE      1 POS
  38. int direction = 1;
  39. const int orbminspeed= 15;
  40. const int orbmaxspeed= 30;
  41.  
  42. const int NUM_LEDS = 50;
  43.  
  44. ////////////////////////////////////////  playerInfo
  45.  
  46. const int playerButtonPins[4][2] = {
  47.     {BUTTONPINP1LEFTY, BUTTONPINP1RIGHTY},
  48.     {BUTTONPINP2LEFTY, BUTTONPINP2RIGHTY},
  49.     {BUTTONPINP3LEFTY, BUTTONPINP3RIGHTY},
  50.     {BUTTONPINP4LEFTY, BUTTONPINP4RIGHTY}};
  51.  
  52. int playerAmount = 4;
  53. boolean playerButtonState[4][4]; // 0 LEFTY      1 RIGHTY    2 LASTLEFTY   3 LASTRIGHTY
  54. float playerMov[4][4];           // 0 VELOCITY  1 FLOATPOS 2 SPEED   3 ACCELERATION
  55. int playerPos[4][3];             // 0 LANE      1 POS      2 OLDPOS
  56. float friction = 0.99;           // Not needed when fixed speed..
  57.  
  58. float knockback = -1;
  59. float defaultSpeed = 0.25;
  60. float defaultAcceleration = 0.025; //can be used for variable speed
  61. float boost = 0.05;
  62.  
  63. /////////////////////////////////////////  updateInfo
  64.  
  65. unsigned long previousMillis = 0;
  66. const int interval = 5;
  67.  
  68. /////////////////////////////////////////  ledInfo
  69. CRGB leds1[NUM_LEDS];
  70. CRGB leds2[NUM_LEDS];
  71. CRGB leds3[NUM_LEDS];
  72. CRGB leds4[NUM_LEDS];
  73.  
  74. /////////////////////////////////////////////////////////////////// SETUP
  75.  
  76. void setup()
  77. {
  78.     delay(3000);
  79.     //Serial.begin(9600);
  80.     setupLEDS();
  81.     setupInput();
  82.     createPlayers();
  83.     createOrbs();
  84. }
  85.  
  86. /////////////////////////////////////////////////////////////////// LOOP
  87.  
  88. void loop()
  89. {
  90.     FastLED.clear();
  91.     //readInput();  //Can also be added to gameLoop
  92.     updateGame();
  93.     FastLED.show();
  94. }
  95.  
  96. /////////////////////////////////////////////////////////////////// gameLoop
  97.  
  98. void updateGame()
  99. {
  100.     unsigned long currentMillis = micros();
  101.     if (currentMillis - previousMillis >= interval)
  102.     {
  103.         gameLoop();
  104.         previousMillis = currentMillis;
  105.     }
  106. }
  107.  
  108. void gameLoop() //MOVE DETECT RE-MOVE?
  109. {
  110.     for (int redOrbsX = 0; redOrbsX < redOrbAmount; redOrbsX++)
  111.     {
  112.         moveOrb(redOrbsX);
  113.         for (int redOrbsY = 0; redOrbsY < redOrbAmount; redOrbsY++)
  114.         {
  115.             hitDetectionOrb(redOrbsX, redOrbsY);
  116.         }
  117.         updateOrb(redOrbsX);
  118.     }
  119.     for (int playerX = 0; playerX < playerAmount; playerX++)
  120.     {
  121.         movePlayer(playerX);
  122.         for (int playerY = 0; playerY < playerAmount; playerY++)
  123.         {
  124.             hitDetectionPlayer(playerX, playerY);
  125.         }
  126.         for (int redOrbs = 0; redOrbs < redOrbAmount; redOrbs++)
  127.         {
  128.             hitDetectionPlayerOrb(playerX, redOrbs);
  129.         }
  130.         updatePlayer(playerX);
  131.     }
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////// INPUT
  135.  
  136. void setupInput()
  137. {
  138.     for (int i = 0; i < playerAmount; i++)
  139.     {
  140.         for (int j = 0; j < 2; j++)
  141.         {
  142.             pinMode(playerButtonPins[i][j], INPUT);
  143.         }
  144.     }
  145. }
  146.  
  147. void readInput()
  148. {
  149.     laneChangeInput();
  150. }
  151.  
  152. void laneChangeInput()
  153. {
  154.     for (int i = 0; i < playerAmount; i++) //0 LEFTY  1 RIGHTY 2 LASTLEFTY 3 LASTRIGHTY
  155.     {
  156.         playerButtonState[i][LEFTY] = digitalRead(playerButtonPins[i][LEFTY]);
  157.         playerButtonState[i][RIGHTY] = digitalRead(playerButtonPins[i][RIGHTY]);
  158.  
  159.         if (playerButtonState[i][LEFTY] != playerButtonState[i][LASTLEFTY])
  160.         {
  161.             if (playerButtonState[i][LEFTY] == HIGH)
  162.             {
  163.                 changePlayerLane(i, LEFTY);
  164.             }
  165.         }
  166.         playerButtonState[i][LASTLEFTY] = playerButtonState[i][LEFTY];
  167.         if (playerButtonState[i][RIGHTY] != playerButtonState[i][LASTRIGHTY])
  168.         {
  169.             if (playerButtonState[i][RIGHTY] == HIGH && playerPos[i][LANE] != 3)
  170.             {
  171.                 changePlayerLane(i, RIGHTY);
  172.             }
  173.         }
  174.         playerButtonState[i][LASTRIGHTY] = playerButtonState[i][RIGHTY];
  175.     }
  176. }
  177.  
  178. /////////////////////////////////////////////////////////////////// MOVEMENT
  179.  
  180. void moveOrb(int orbIndex)
  181. {
  182.     redOrbMov[orbIndex][FLOATPOS] += redOrbMov[orbIndex][VELOCITY];
  183.     if (redOrbMov[orbIndex][FLOATPOS] > NUM_LEDS)
  184.     {
  185.         redOrbMov[orbIndex][FLOATPOS] = 0.0;
  186.     };
  187.     redOrbPos[orbIndex][POS] = (int)(redOrbMov[orbIndex][FLOATPOS] + 0.5);
  188. }
  189.  
  190. void movePlayer(int index) // not perfect, debug and rework
  191. {
  192.     playerPos[index][OLDPOS] = playerPos[index][POS];
  193.  
  194.     if (playerMov[index][VELOCITY] < playerMov[index][SPEED])
  195.     {
  196.         playerMov[index][VELOCITY] += playerMov[index][ACCELERATION]; //Can use fixed acceleration to save memory
  197.     }
  198.     playerMov[index][VELOCITY] = playerMov[index][VELOCITY] * friction;
  199.     playerMov[index][FLOATPOS] += playerMov[index][VELOCITY];
  200.     if (playerMov[index][FLOATPOS] > NUM_LEDS)
  201.     {
  202.         playerMov[index][FLOATPOS] -= NUM_LEDS;
  203.         //playerMov[index][FLOATPOS] = 0.0;
  204.     }
  205.     else if (playerMov[index][FLOATPOS] < 0.0)
  206.     {
  207.         playerMov[index][FLOATPOS] += NUM_LEDS;
  208.     }
  209.     playerPos[index][POS] = (int)(playerMov[index][FLOATPOS] + 0.5);
  210. }
  211.  
  212. void changePlayerLane(int player, int direction) // fcking moe <- check again please
  213. {
  214.     if (direction == RIGHTY && playerPos[player][LANE] != 3)
  215.     {
  216.         playerPos[player][LANE]++;
  217.     }
  218.     else if (direction == LEFTY && playerPos[player][LANE] != 0)
  219.     {
  220.         playerPos[player][LANE]--;
  221.     }
  222. };
  223.  
  224. int isLaneEmpty(int player, int direction) //fcking moe <- check again please
  225. {
  226.     for (int i = 0; i < playerAmount; i++)
  227.     {
  228.         if (player != i)
  229.         {
  230.             if ((playerPos[player][LANE] + 1) != playerPos[i][LANE])
  231.             {
  232.                 return 4;
  233.             }else{
  234.                 return i;
  235.             }
  236.         }
  237.     }
  238. }
  239.  
  240. void changeOrbLane(int orb)
  241. {
  242.     if (redOrbPos[orb][LANE] == 0)
  243.     {
  244.         redOrbPos[orb][LANE]++;
  245.     }
  246.     else if (redOrbPos[orb][LANE] == 3)
  247.     {
  248.         redOrbPos[orb][LANE]--;
  249.     }
  250.     else
  251.     {
  252.         direction = direction * -1;
  253.         redOrbPos[orb][LANE] += direction;
  254.     }
  255. };
  256.  
  257. /////////////////////////////////////////////////////////////////// HIT DETECTION
  258.  
  259. void hitDetectionOrb(int orbX, int orbY)
  260. {
  261.     if (orbX != orbY)
  262.     {
  263.         if (redOrbPos[orbX][LANE] == redOrbPos[orbY][LANE] && redOrbPos[orbY][POS] == (redOrbPos[orbX][POS] - 1))
  264.         {
  265.             if (redOrbMov[orbX][VELOCITY] > redOrbMov[orbY][VELOCITY])
  266.             {
  267.                 redOrbPos[orbX][POS]++; //but why?
  268.             }
  269.             else
  270.             {
  271.                 changeOrbLane(orbY); //please debug this
  272.             }
  273.         }
  274.     }
  275. }
  276.  
  277. void hitDetectionPlayer(int pX, int pY)
  278. {
  279.     if (pX != pY)
  280.     {
  281.         if (playerPos[pX][OLDPOS] <= playerPos[pY][POS] && playerPos[pX][POS] > playerPos[pY][POS])
  282.         {
  283.             if (playerPos[pX][LANE] == playerPos[pY][LANE])
  284.             {
  285.                 playerMov[pX][VELOCITY] += knockback;
  286.                 playerMov[pX][SPEED] = defaultSpeed;
  287.                 playerMov[pY][SPEED] = defaultSpeed;
  288.                 movePlayer(pX); //?
  289.             }
  290.         }
  291.     }
  292. }
  293.  
  294. void hitDetectionPlayerOrb(int player, int orb) //Add detection from behind.
  295. {
  296.     if (playerPos[player][OLDPOS] <= redOrbPos[orb][POS] && playerPos[player][POS] > redOrbPos[orb][POS])
  297.     {
  298.         if (playerPos[player][LANE] != redOrbPos[orb][LANE])
  299.         {
  300.             playerMov[player][SPEED] += boost;
  301.         }
  302.         else
  303.         {
  304.             playerMov[player][VELOCITY] += knockback;
  305.             playerMov[player][SPEED] = defaultSpeed;
  306.             movePlayer(player); //?
  307.         }
  308.     }
  309. }
  310.  
  311. /////////////////////////////////////////////////////////////////// UPDATE LEDS
  312.  
  313. void updatePlayer(int index)
  314. {
  315.     int currentColor;
  316.     switch (index)
  317.     {
  318.     case 0:
  319.         currentColor = CRGB::Blue;
  320.         break;
  321.     case 1:
  322.         currentColor = CRGB::Purple;
  323.         break;
  324.     case 2:
  325.         currentColor = CRGB::Green;
  326.         break;
  327.     case 3:
  328.         currentColor = CRGB::Yellow;
  329.         break;
  330.     }
  331.  
  332.     switch (playerPos[index][LANE])
  333.     {
  334.     case 0:
  335.         leds1[playerPos[index][POS]] = currentColor;
  336.         break;
  337.     case 1:
  338.         leds2[playerPos[index][POS]] = currentColor;
  339.         break;
  340.     case 2:
  341.         leds3[playerPos[index][POS]] = currentColor;
  342.         break;
  343.     case 3:
  344.         leds4[playerPos[index][POS]] = currentColor;
  345.         break;
  346.     }
  347. }
  348.  
  349. void updateOrb(int index)
  350. {
  351.     switch (redOrbPos[index][LANE])
  352.     {
  353.     case 0:
  354.         leds1[redOrbPos[index][POS]] = CRGB::Red;
  355.         break;
  356.     case 1:
  357.         leds2[redOrbPos[index][POS]] = CRGB::Red;
  358.         break;
  359.     case 2:
  360.         leds3[redOrbPos[index][POS]] = CRGB::Red;
  361.         break;
  362.     case 3:
  363.         leds4[redOrbPos[index][POS]] = CRGB::Red;
  364.         break;
  365.     }
  366. }
  367.  
  368. /////////////////////////////////////////////////////////////////// CREATE
  369.  
  370. void createPlayers()
  371. {
  372.     for (int i = 0; i < playerAmount; i++)
  373.     {
  374.         playerMov[i][VELOCITY] = 0.0;
  375.         playerMov[i][FLOATPOS] = 0.0;
  376.         playerMov[i][SPEED] = defaultSpeed;
  377.         playerMov[i][ACCELERATION] = defaultAcceleration;
  378.         playerPos[i][LANE] = i;
  379.         playerPos[i][POS] = 0;
  380.         playerPos[i][OLDPOS] = 0;
  381.     }
  382. }
  383.  
  384. void createOrbs()
  385. {
  386.     for (int i = 0; i < redOrbAmount; i++)
  387.     {
  388.         int lane = random(0, 4);
  389.         float posFloat = random(0, NUM_LEDS);
  390.         float velocity = random(orbminspeed, orbmaxspeed);
  391.         velocity = velocity / 100;
  392.         redOrbPos[i][LANE] = lane;
  393.         redOrbMov[i][VELOCITY] = velocity;
  394.         redOrbMov[i][FLOATPOS] = posFloat;
  395.     }
  396. }
  397.  
  398. /////////////////////////////////////////////////////////////////// SETUP
  399.  
  400. void setupLEDS()
  401. {
  402.     FastLED.addLeds<NEOPIXEL, LED1>(leds1, NUM_LEDS);
  403.     FastLED.addLeds<NEOPIXEL, LED2>(leds2, NUM_LEDS);
  404.     FastLED.addLeds<NEOPIXEL, LED3>(leds3, NUM_LEDS);
  405.     FastLED.addLeds<NEOPIXEL, LED4>(leds4, NUM_LEDS);
  406. }
Advertisement
Add Comment
Please, Sign In to add comment