Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  4.  
  5. class Car
  6.   {
  7.     public :int posx;
  8.     public :int posy;
  9.     public :int mofspeed = 25000;
  10.     public :bool canuse;
  11.     public :bool canmove;
  12.  
  13. float timer;
  14.  
  15. public :void MakeCar()
  16. {
  17.             lcd.setCursor(posx, posy);
  18.           lcd.write("B");
  19.   }
  20.  
  21.   public :void StartCar()
  22.   {
  23.           canmove = true;
  24.            lcd.setCursor(posx, posy);
  25.           lcd.write("B");
  26.     }
  27.  
  28.     public :void StopCar()
  29.     {
  30.       canmove = false;
  31.        lcd.setCursor(posx, posy);
  32.           lcd.write(" ");
  33.       }
  34.  
  35.     public :void Move()
  36.     {
  37.       timer++;
  38.       if (timer >= mofspeed)
  39.       {
  40.           lcd.setCursor(posx, posy);
  41.           lcd.write(" ");
  42.           posx--;
  43.           lcd.setCursor(posx, posy);
  44.           lcd.write("B");
  45.           timer = 0;
  46.         }
  47.       }
  48.     };
  49.  
  50. int LEFTBUTTON = 2;
  51. int RIGHTBUTTON = 3;
  52.  
  53. int score = 0;
  54. int pos = 2;
  55.  
  56. bool currentButton = false;
  57. bool currentButton2 = false;
  58.  
  59. float timer = 0;
  60. float timermax = 150000;
  61. int previusYpos;
  62. float olddeltatime;
  63. Car cars[8];
  64. int carcount;
  65. int movementspeed = 25000;
  66.  
  67. bool game;
  68. void setup()
  69. {
  70.   // put your setup code here, to run once:
  71.     Serial.begin(9600);
  72.     lcd.begin(20, 4);
  73.  
  74.     pinMode(LEFTBUTTON, INPUT);
  75.     pinMode(RIGHTBUTTON, INPUT);
  76.  
  77.     lcd.setCursor(2, pos);
  78.     lcd.write("@");
  79.     timer = timermax;
  80.     game = true;
  81. }
  82.  
  83. bool GetButton(int button)
  84. {
  85.   return (digitalRead(button));
  86.   }
  87.  
  88. void MakeCar(int ypos)
  89. {
  90.   cars[carcount].posx= 18;
  91.   cars[carcount].posy = ypos;
  92.    cars[carcount].canuse = false;
  93.       cars[carcount].canmove = true;
  94.   cars[carcount].MakeCar();
  95.   carcount++;
  96. }
  97.  
  98. void MakeCar(int ypos, int count)
  99. {
  100.   cars[count].posx= 18;
  101.   cars[count].posy = ypos;
  102.   cars[count].canuse = false;
  103.    cars[count].StartCar();      
  104. }
  105.  
  106. void loop()
  107. {
  108.   if (game == true)
  109.   {
  110.     //movement
  111.      if (GetButton(LEFTBUTTON) != currentButton)
  112.      {
  113.      if (GetButton(LEFTBUTTON) == LOW)
  114.      {
  115.           lcd.setCursor(2, pos);
  116.           lcd.write(" ");
  117.           pos++;
  118.           if (pos >= 4)
  119.           {
  120.             pos = 4;
  121.             }
  122.              lcd.setCursor(2, pos);
  123.           lcd.write("@");
  124.       }
  125.      }
  126.  
  127.      if (GetButton(RIGHTBUTTON) != currentButton2)
  128.      {
  129.      if (GetButton(RIGHTBUTTON) == LOW)
  130.      {
  131.         lcd.setCursor(2, pos);
  132.         lcd.write(" ");
  133.         pos--;
  134.         if (pos <= 0)
  135.         {
  136.         pos = 0;
  137.         }
  138.         lcd.setCursor(2, pos);
  139.           lcd.write("@");
  140.       }
  141.      }
  142.      
  143.     currentButton = GetButton(LEFTBUTTON);
  144.     currentButton2 = GetButton(RIGHTBUTTON);
  145.    
  146.     //car
  147.     timer ++;
  148.     if (timer >= timermax)
  149.     {
  150.       if (carcount >= 8)
  151.       {        
  152.         for (int i = 0; i < carcount; i++)
  153.         {
  154.           if (cars[i].canuse == true)
  155.           {
  156.                 MakeCar(random(0, 4), i);
  157.                 break;
  158.           }
  159.         }      
  160.       }
  161.       else
  162.       {
  163.           MakeCar(random(0, 4));
  164.       }
  165.       if (timermax - 1000 > 100)
  166.       {
  167.           timermax -= 1000;
  168.       }
  169.  
  170.       if (movementspeed - 1000 > 100)
  171.       {
  172.         movementspeed -= 1000;
  173.       }
  174.       timer = 0;
  175.     }
  176.  
  177.     for (int i = 0; i < carcount; i++)
  178.     {
  179.           //check collision
  180.           if (cars[i].posx == 2 && cars[i].posy == pos)
  181.           {
  182.             game = false;
  183.             }
  184.            
  185.       if (cars[i].canuse == false) {
  186.         if (cars[i].canmove == true)
  187.         {
  188.           cars[i].Move();
  189.         }
  190.         cars[i].mofspeed = movementspeed;
  191.         if (cars[i].posx <= 0)
  192.         {
  193.           cars[i].canuse = true;
  194.           cars[i].StopCar();
  195.         }
  196.       }
  197.     }
  198. }
  199. else
  200. {
  201.   lcd.clear();
  202.   lcd.setCursor(5, 2);
  203.   lcd.write("game over!");
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement