Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
- class Car
- {
- public :int posx;
- public :int posy;
- public :int mofspeed = 25000;
- public :bool canuse;
- public :bool canmove;
- float timer;
- public :void MakeCar()
- {
- lcd.setCursor(posx, posy);
- lcd.write("B");
- }
- public :void StartCar()
- {
- canmove = true;
- lcd.setCursor(posx, posy);
- lcd.write("B");
- }
- public :void StopCar()
- {
- canmove = false;
- lcd.setCursor(posx, posy);
- lcd.write(" ");
- }
- public :void Move()
- {
- timer++;
- if (timer >= mofspeed)
- {
- lcd.setCursor(posx, posy);
- lcd.write(" ");
- posx--;
- lcd.setCursor(posx, posy);
- lcd.write("B");
- timer = 0;
- }
- }
- };
- int LEFTBUTTON = 2;
- int RIGHTBUTTON = 3;
- int score = 0;
- int pos = 2;
- bool currentButton = false;
- bool currentButton2 = false;
- float timer = 0;
- float timermax = 150000;
- int previusYpos;
- float olddeltatime;
- Car cars[8];
- int carcount;
- int movementspeed = 25000;
- bool game;
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- lcd.begin(20, 4);
- pinMode(LEFTBUTTON, INPUT);
- pinMode(RIGHTBUTTON, INPUT);
- lcd.setCursor(2, pos);
- lcd.write("@");
- timer = timermax;
- game = true;
- }
- bool GetButton(int button)
- {
- return (digitalRead(button));
- }
- void MakeCar(int ypos)
- {
- cars[carcount].posx= 18;
- cars[carcount].posy = ypos;
- cars[carcount].canuse = false;
- cars[carcount].canmove = true;
- cars[carcount].MakeCar();
- carcount++;
- }
- void MakeCar(int ypos, int count)
- {
- cars[count].posx= 18;
- cars[count].posy = ypos;
- cars[count].canuse = false;
- cars[count].StartCar();
- }
- void loop()
- {
- if (game == true)
- {
- //movement
- if (GetButton(LEFTBUTTON) != currentButton)
- {
- if (GetButton(LEFTBUTTON) == LOW)
- {
- lcd.setCursor(2, pos);
- lcd.write(" ");
- pos++;
- if (pos >= 4)
- {
- pos = 4;
- }
- lcd.setCursor(2, pos);
- lcd.write("@");
- }
- }
- if (GetButton(RIGHTBUTTON) != currentButton2)
- {
- if (GetButton(RIGHTBUTTON) == LOW)
- {
- lcd.setCursor(2, pos);
- lcd.write(" ");
- pos--;
- if (pos <= 0)
- {
- pos = 0;
- }
- lcd.setCursor(2, pos);
- lcd.write("@");
- }
- }
- currentButton = GetButton(LEFTBUTTON);
- currentButton2 = GetButton(RIGHTBUTTON);
- //car
- timer ++;
- if (timer >= timermax)
- {
- if (carcount >= 8)
- {
- for (int i = 0; i < carcount; i++)
- {
- if (cars[i].canuse == true)
- {
- MakeCar(random(0, 4), i);
- break;
- }
- }
- }
- else
- {
- MakeCar(random(0, 4));
- }
- if (timermax - 1000 > 100)
- {
- timermax -= 1000;
- }
- if (movementspeed - 1000 > 100)
- {
- movementspeed -= 1000;
- }
- timer = 0;
- }
- for (int i = 0; i < carcount; i++)
- {
- //check collision
- if (cars[i].posx == 2 && cars[i].posy == pos)
- {
- game = false;
- }
- if (cars[i].canuse == false) {
- if (cars[i].canmove == true)
- {
- cars[i].Move();
- }
- cars[i].mofspeed = movementspeed;
- if (cars[i].posx <= 0)
- {
- cars[i].canuse = true;
- cars[i].StopCar();
- }
- }
- }
- }
- else
- {
- lcd.clear();
- lcd.setCursor(5, 2);
- lcd.write("game over!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement