Guest User

.cpp

a guest
Aug 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.12 KB | None | 0 0
  1. #include "Games.h"
  2.  
  3. Utils UTILS;
  4. Splash SPLASH;
  5. //Data DAta;
  6. LiquidCrystal_I2C  lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);
  7. Keypad keypad = Keypad(makeKeymap(KPkeys), rowPins, colPins, ROWS, COLS);
  8. Keys keys;
  9.  
  10.  
  11. Games::search()
  12. {
  13.     Serial.print("tests");
  14.     return;
  15. }
  16.  
  17. Games::destroy()
  18. {
  19.     Serial.print("testD");
  20.     return;
  21. }
  22.  
  23. Games::sabotage()
  24. {
  25.     endGame = false;
  26.     refresh = true;
  27.     UTILS.cls();
  28.     digitalWrite(REDLED, LOW);
  29.     digitalWrite(GREENLED, LOW);
  30.     //SETUP INITIAL TIME
  31.     int minutos = GAMEMINUTES - 1;
  32.  
  33.  
  34.     if (start)
  35.     {
  36.         iTime = millis(); //  initialTime of the game, use this because sabotage mode goes can return to sabotage()
  37.         start = false;
  38.     }
  39.  
  40.     unsigned long aTime;
  41.  
  42.     //Starting Game Code
  43.     while (1)   // this is the important code, is a little messy but works good.
  44.     {
  45.  
  46.         if (endGame)
  47.         {
  48.             SPLASH.failSplash();
  49.         }
  50.         //Code for led blinking
  51.         timeCalcVar = (millis() - iTime) % 1000;
  52.         if (timeCalcVar >= 0 && timeCalcVar <= 50)
  53.         {
  54.             digitalWrite(GREENLED, HIGH);
  55.         }
  56.         if (timeCalcVar >= 90 && timeCalcVar <= 130)
  57.         {
  58.             digitalWrite(GREENLED, LOW);
  59.         }
  60.  
  61.         lcd.setCursor(3, 0);
  62.         lcd.print(GAME_TIME);
  63.         aTime = millis() - iTime;
  64.         lcd.setCursor(3, 1);
  65.  
  66.         //PRINT TIME ON LCD
  67.         UTILS.printTime(minutos, aTime);
  68.         //###########################CHECKINGS##################
  69.  
  70.         //Check If Game End
  71.         if (minutos - aTime / 60000 == 0 && 59 - ((aTime / 1000) % 60) == 0)
  72.         {
  73.             SPLASH.failSplash();
  74.         }
  75.  
  76.         //USED IN PASSWORD GAME
  77.         if ('*' == keypad.getKey() && passwordEnable)
  78.         {
  79.             lcd.clear();
  80.             lcd.setCursor(2, 0);
  81.             lcd.print(ARMING_BOMB);
  82.             delay(1000);//a little delay to think in the password
  83.             lcd.clear();
  84.             lcd.setCursor(0, 0);
  85.             lcd.print(ENTER_CODE);
  86.  
  87.             keys.setCodeTime();// we need to set the compare variable first
  88.  
  89.             //then compare :D
  90.  
  91.             if (keys.comparePassword())
  92.             {
  93.                 destroySabotage();
  94.             }
  95.             lcd.clear();
  96.             lcd.setCursor(2, 0);
  97.             lcd.print(CODE_ERROR);
  98.             if (soundEnable)
  99.             {
  100.                 tone(tonepin, errorTone, 200);
  101.             }
  102.             delay(500);
  103.             UTILS.cls();
  104.         }
  105.  
  106.         //Check If Is Activating
  107.         while (fusing && !passwordEnable)
  108.         {
  109.             keypad.getKey();
  110.             UTILS.cls();
  111.             digitalWrite(GREENLED, LOW);
  112.             lcd.clear();
  113.             lcd.setCursor(2, 0);
  114.             lcd.print(ARMING_BOMB);
  115.             lcd.setCursor(0, 1);
  116.             unsigned int percent = 0;
  117.             unsigned long xTime = millis(); //start disabling time
  118.             while (fusing)
  119.             {
  120.                 keypad.getKey();
  121.                 //check if game time runs out during the disabling
  122.                 aTime = millis() - iTime;
  123.                 if ((minutos - aTime / 60000 == 0 && 59 - ((aTime / 1000) % 60) == 0) || minutos - aTime / 60000 > 4000000000)
  124.                 {
  125.                     endGame = true;
  126.                 }
  127.  
  128.                 timeCalcVar = (millis() - xTime) % 1000;
  129.  
  130.                 if (timeCalcVar >= 0 && timeCalcVar <= 40)
  131.                 {
  132.                     digitalWrite(REDLED, HIGH);
  133.                     if (soundEnable)
  134.                     {
  135.                         tone(tonepin, alarmTone1, 200);
  136.                     }
  137.                 }
  138.                 if (timeCalcVar >= 480 && timeCalcVar <= 520)
  139.                 {
  140.                     if (soundEnable)
  141.                     {
  142.                         tone(tonepin, alarmTone2, 200);
  143.                     }
  144.                     digitalWrite(REDLED, LOW);
  145.                 }
  146.                 unsigned long seconds = millis() - xTime;
  147.                 percent = (seconds) / (ACTIVATESECONDS * 10);
  148.                 UTILS.drawBar(percent);
  149.  
  150.                 if (percent >= 100)
  151.                 {
  152.                     digitalWrite(GREENLED, LOW);
  153.                     destroySabotage();// jump to the next gamemode
  154.                 }
  155.             }
  156.             UTILS.cls();
  157.             digitalWrite(REDLED, LOW);
  158.         }
  159.     }
  160. }
  161.  
  162. Games::destroySabotage(){
  163.     endGame = false;
  164.     lcd.clear();
  165.     lcd.setCursor(3, 0);
  166.     lcd.print(BOMB_ARMED);
  167.     delay(1000);
  168.     int minutos = BOMBMINUTES - 1;
  169.     unsigned long iTime = millis();
  170.     unsigned long aTime;
  171.     int largoTono = 50;
  172.  
  173.     //MAIN LOOP
  174.     while (1)
  175.     {
  176.  
  177.         //If you fail disarm.
  178.         if (endGame)
  179.         {
  180.             SPLASH.explodeSplash();
  181.         }
  182.  
  183.         //Led Blink
  184.  
  185.         timeCalcVar = (millis() - iTime) % 1000;
  186.         if (timeCalcVar >= 0 && timeCalcVar <= 40)
  187.         {
  188.             digitalWrite(REDLED, HIGH);
  189.             if (soundEnable)
  190.             {
  191.                 tone(tonepin, activeTone, largoTono);
  192.             }
  193.         }
  194.         if (timeCalcVar >= 180 && timeCalcVar <= 220)
  195.         {
  196.             digitalWrite(REDLED, LOW);
  197.         }
  198.         //Sound
  199.  
  200.         timeCalcVar = (millis() - iTime) % 1000;
  201.         aTime = millis() - iTime;
  202.         if (timeCalcVar >= 245 && timeCalcVar <= 255 && minutos - aTime / 60000 < 2 && soundEnable)
  203.         {
  204.             tone(tonepin, activeTone, largoTono);
  205.         }
  206.         if (timeCalcVar >= 495 && timeCalcVar <= 510 && minutos - aTime / 60000 < 4 && soundEnable)
  207.         {
  208.             tone(tonepin, activeTone, largoTono);
  209.         }
  210.         if (timeCalcVar >= 745 && timeCalcVar <= 760 && minutos - aTime / 60000 < 2 && soundEnable)
  211.         {
  212.             tone(tonepin, activeTone, largoTono);
  213.         }
  214.         if (minutos - aTime / 60000 == 0 && 59 - ((aTime / 1000) % 60) < 10)
  215.         {
  216.             largoTono = 300;
  217.         }
  218.  
  219.         lcd.setCursor(1, 0);
  220.         lcd.print(DETONATION_IN);
  221.         //Passed Time
  222.  
  223.         lcd.setCursor(3, 1);
  224.  
  225.         ////////HERE ARE THE TWO OPTIONS THAT ENDS THE GAME///////////
  226.  
  227.         ////TIME PASED AWAY AND THE BOMB EXPLODES
  228.         if (minutos - aTime / 60000 == 0 && 59 - ((aTime / 1000) % 60) == 0) // Check if game ends
  229.         {
  230.             SPLASH.failSplash();
  231.         }
  232.         //print time
  233.         UTILS.printTime(minutos, aTime);
  234.  
  235.         //// SECOND OPTION: YOU PRESS DISARMING BUTTON
  236.  
  237.         //IF IS A PASSWORD GAME
  238.  
  239.         if ('*' == keypad.getKey() && passwordEnable)
  240.         {
  241.  
  242.             UTILS.cls();
  243.             digitalWrite(REDLED, LOW);
  244.             digitalWrite(GREENLED, HIGH);
  245.             lcd.print(DISARMING);
  246.             delay(1000);//a little delay to think in the password
  247.  
  248.             lcd.clear();
  249.             lcd.setCursor(0, 0);
  250.             lcd.print(ENTER_CODE);
  251.  
  252.             keys.setCodeTime();// we need to set the compare variable first
  253.  
  254.             //then compare :D
  255.  
  256.             if (keys.comparePassword())
  257.             {
  258.                 sabotage();
  259.             }
  260.             lcd.clear();
  261.             lcd.setCursor(2, 0);
  262.             lcd.print(CODE_ERROR);
  263.             if (soundEnable)
  264.             {
  265.                 tone(tonepin, errorTone, 200);
  266.             }
  267.             delay(500);
  268.             UTILS.cls();
  269.         }
  270.  
  271.         if (defusing && !passwordEnable) // disarming bomb
  272.         {
  273.             lcd.clear();
  274.             digitalWrite(REDLED, LOW);
  275.             lcd.setCursor(3, 0);
  276.             lcd.print(DISARM);
  277.             lcd.setCursor(0, 1);
  278.             unsigned int percent = 0;
  279.             unsigned long xTime = millis();
  280.             while (defusing)
  281.             {
  282.                 keypad.getKey();
  283.                 //check if game time runs out during the disabling
  284.                 aTime = millis() - iTime;
  285.                 if ((minutos - aTime / 60000 == 0 && 59 - ((aTime / 1000) % 60) == 0) || minutos - aTime / 60000 > 4000000000)
  286.                 {
  287.                     endGame = true;
  288.                 }
  289.  
  290.                 timeCalcVar = (millis() - xTime) % 1000;
  291.                 if (timeCalcVar >= 0 && timeCalcVar <= 20)
  292.                 {
  293.                     digitalWrite(GREENLED, HIGH);
  294.                     if (soundEnable)
  295.                     {
  296.                         tone(tonepin, alarmTone1, 200);
  297.                     }
  298.                 }
  299.                 if (timeCalcVar >= 480 && timeCalcVar <= 500)
  300.                 {
  301.                     if (soundEnable)
  302.                     {
  303.                         tone(tonepin, alarmTone2, 200);
  304.                     }
  305.                     digitalWrite(GREENLED, LOW);
  306.                 }
  307.                 unsigned long seconds = (millis() - xTime);
  308.                 percent = seconds / (ACTIVATESECONDS * 10);
  309.                 UTILS.drawBar(percent);
  310.  
  311.                 //BOMB DISARMED RETURN TO SABOTAGE
  312.                 if (percent >= 100)
  313.                 {
  314.                     UTILS.cls();
  315.                     lcd.print("Bomb Disarmed");
  316.                     delay(1000);
  317.                     sabotage();
  318.                 }
  319.             }
  320.             digitalWrite(REDLED, LOW);
  321.             digitalWrite(GREENLED, LOW);
  322.             UTILS.cls();
  323.         }
  324.     }
  325. }
  326.  
  327. Games::domination()
  328. {
  329.     Serial.print("test2");
  330.     return;
  331. }
  332.  
  333. Games::settings()
  334. {
  335.     Serial.print("test3");
  336.     return;
  337. }
Add Comment
Please, Sign In to add comment