Advertisement
Guest User

Arduino robots game

a guest
Nov 5th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <serialGLCD.h>
  2. int BOTS=3;
  3. unsigned char side[256] = {
  4.     0xFF, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x40, 0x40, 0x40,
  5.     0x40, 0x00, 0xC0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0xC0, 0x40, 0x40, 0x40,
  6.     0x40, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x1F, 0x10,
  7.     0x10, 0x10, 0x10, 0x00, 0x1F, 0x12, 0x12, 0x12, 0x10, 0x00, 0x01, 0x06,
  8.     0x18, 0x06, 0x01, 0x00, 0x1F, 0x12, 0x12, 0x12, 0x10, 0x00, 0x1F, 0x10,
  9.     0x10, 0x10, 0x10, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  11.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12.     0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  15.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17.     0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x18, 0x24, 0x24, 0x24, 0xC0,
  18.     0x00, 0xF8, 0x04, 0x04, 0x04, 0x00, 0xF8, 0x04, 0x04, 0xF8, 0x00, 0xFC,
  19.     0x64, 0xA4, 0x18, 0x00, 0xFC, 0x24, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00,
  20.     0xFF, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01,
  21.     0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
  22.     0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  23.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25.     0x00, 0x00, 0x00, 0x00
  26. };
  27. int score = 0, tscore = 0;
  28. struct robot {
  29.   uint8_t x,y;
  30.   uint8_t ox,oy;
  31.   uint8_t status;
  32. //  uint8_t dirty;
  33. } robots[32];
  34. int px,py;
  35. serialGLCD s;
  36. void seedrand(){
  37.   randomSeed(analogRead(A0) * analogRead(A5) + digitalRead(13) + digitalRead(5));
  38. }
  39. void setup(){
  40.     Serial.begin(115200);
  41.     delay(1000);
  42.     s.resetLCD();
  43.     delay(500);
  44.     int i,k;
  45.     tscore += score;
  46.     score = 0;
  47.     seedrand();
  48.     for(i = 0;i < BOTS;i++){
  49.         robots[i].x = random(0,32);
  50.         robots[i].y = random(0,21);
  51.         robots[i].status = 1;
  52.         while(robots[i].x == 16 && robots[i].y == 10){
  53.           robots[i].x = random(0,32);
  54.           robots[i].y = random(0,21);
  55.         }  
  56.         for(k = 0;k < BOTS;k++){
  57.           if(i == k){continue;}
  58.           while(robots[i].x == robots[k].x && robots[i].y == robots[k].y){
  59.             robots[i].x = random(0,32);
  60.             robots[i].y = random(0,21);
  61.           }
  62.         }
  63.     }
  64.     px = 16;
  65.     py = 10;
  66.     s.drawData(96, 0, 7, 32, 64, side);
  67.     s.gotoPosition(108, 16);
  68.     Serial.print(BOTS - 2);
  69.     delay(100);
  70.     s.gotoPosition(100, 50);
  71.     Serial.print(tscore);
  72.     delay(100);
  73. }
  74. void robotstep(){
  75.   for(int r = 0;r < BOTS;r++){
  76.     if(!robots[r].status) continue;
  77.     s.togglePixel(robots[r].x*3, robots[r].y*3, 0);
  78.     s.togglePixel(1+robots[r].x*3, 1+robots[r].y*3, 0);
  79.     s.togglePixel(2+robots[r].x*3, 2+robots[r].y*3, 0);
  80.     delay(10);
  81.     if(robots[r].x > px){robots[r].x--;}
  82.     if(robots[r].x < px){robots[r].x++;}
  83.     if(robots[r].y < py){robots[r].y++;}
  84.     if(robots[r].y > py){robots[r].y--;}
  85.   }
  86. }
  87. int getdeadbots(){
  88.   int a;
  89.   int b=0;
  90.   for(a = 0;a < BOTS;a++){
  91.     if(!robots[a].status){b++;}
  92.   }
  93.   return b;
  94. }
  95. void loop(){
  96.   int r,rl;
  97.   if(score == (BOTS * 100)){
  98.     s.resetLCD();
  99.     Serial.print("ALL BOTS CLEARED!");
  100.     s.gotoPosition(0,16);
  101.     Serial.print("Press key=next level");
  102.     while(!Serial.available());
  103.     BOTS++;
  104.     Serial.read();
  105.     setup();
  106.     return;
  107.   }
  108.   for(r = 0;r < BOTS;r++){
  109.     for(rl = 0;rl < BOTS;rl++){
  110.       if(robots[r].x == robots[rl].x && robots[r].y == robots[rl].y && r != rl){
  111.         robots[r].status = robots[rl].status = 0;
  112.       }
  113.     }
  114.     if(robots[r].status){
  115.       s.togglePixel(robots[r].x*3, robots[r].y*3, 1);
  116.       s.togglePixel(1+robots[r].x*3, 1+robots[r].y*3, 1);
  117.       s.togglePixel(2+robots[r].x*3, 2+robots[r].y*3, 1);
  118.     }
  119.     else {
  120.       int X,Y;
  121.       for(X = 0;X < 3;X++){
  122.         for(Y = 0;Y < 3;Y++){
  123.           s.togglePixel(robots[r].x*3+X, robots[r].y*3+Y, 1);
  124.           delay(3);
  125.         }
  126.       }
  127.     //  delay(25);
  128.     }
  129.     if(robots[r].x == px && robots[r].y == py){
  130.       while(1){  
  131.         delay(1000);
  132.         s.resetLCD();
  133.         s.gotoPosition(0,10);
  134.         Serial.print("Score: ");
  135.         Serial.print(tscore+score);
  136.         s.gotoPosition(0,0);
  137.         delay(750);
  138.         Serial.print("GAME OVER!");
  139.         delay(1500);
  140.       }
  141.     }
  142.      
  143.   }
  144.   if(Serial.available()){
  145.     s.togglePixel(1+px*3,py*3,0);
  146.     s.togglePixel(px*3,py*3+1,0);
  147.     s.togglePixel(2+px*3,py*3+1,0);
  148.     s.togglePixel(1+px*3,2+py*3,0);
  149.     switch(Serial.read()){
  150.       case '1': if(px>0&&py<20){px--; py++;} else {delay(100);return;} break;
  151.       case '2': if(py<20)py++; else {delay(100);return;} break;
  152.       case '3': if(px<31&&py<20){px++; py++;} else {delay(100);return;} break;
  153.       case '4': if(px>0)px--; else {delay(100);return;} break;
  154.       case '5': px = random(0,32); py = random(0,21);
  155.       case '6': if(px<31)px++; else {delay(100);return;} break;
  156.       case '7': if(px>0&&py>0){px--; py--;} else {delay(100);return;} break;
  157.       case '8': if(py>0)py--; else {delay(100);return;} break;    
  158.       case '9': if(px<31&&py>0){px++; py--;} else {delay(100);return;} break;
  159.      
  160.     }
  161.     robotstep();    
  162.     score = getdeadbots() * 100;
  163.     s.gotoPosition(100, 50);
  164.     Serial.print(tscore+score);
  165.   }
  166.   s.togglePixel(1+px*3,py*3,1);
  167.   s.togglePixel(px*3,py*3+1,1);
  168.   s.togglePixel(2+px*3,py*3+1,1);
  169.   s.togglePixel(1+px*3,2+py*3,1);
  170.   delay(150);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement