Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <serialGLCD.h>
- int BOTS=3;
- unsigned char side[256] = {
- 0xFF, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x40, 0x40, 0x40,
- 0x40, 0x00, 0xC0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0xC0, 0x40, 0x40, 0x40,
- 0x40, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x1F, 0x10,
- 0x10, 0x10, 0x10, 0x00, 0x1F, 0x12, 0x12, 0x12, 0x10, 0x00, 0x01, 0x06,
- 0x18, 0x06, 0x01, 0x00, 0x1F, 0x12, 0x12, 0x12, 0x10, 0x00, 0x1F, 0x10,
- 0x10, 0x10, 0x10, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x18, 0x24, 0x24, 0x24, 0xC0,
- 0x00, 0xF8, 0x04, 0x04, 0x04, 0x00, 0xF8, 0x04, 0x04, 0xF8, 0x00, 0xFC,
- 0x64, 0xA4, 0x18, 0x00, 0xFC, 0x24, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00,
- 0xFF, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01,
- 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
- 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
- };
- int score = 0, tscore = 0;
- struct robot {
- uint8_t x,y;
- uint8_t ox,oy;
- uint8_t status;
- // uint8_t dirty;
- } robots[32];
- int px,py;
- serialGLCD s;
- void seedrand(){
- randomSeed(analogRead(A0) * analogRead(A5) + digitalRead(13) + digitalRead(5));
- }
- void setup(){
- Serial.begin(115200);
- delay(1000);
- s.resetLCD();
- delay(500);
- int i,k;
- tscore += score;
- score = 0;
- seedrand();
- for(i = 0;i < BOTS;i++){
- robots[i].x = random(0,32);
- robots[i].y = random(0,21);
- robots[i].status = 1;
- while(robots[i].x == 16 && robots[i].y == 10){
- robots[i].x = random(0,32);
- robots[i].y = random(0,21);
- }
- for(k = 0;k < BOTS;k++){
- if(i == k){continue;}
- while(robots[i].x == robots[k].x && robots[i].y == robots[k].y){
- robots[i].x = random(0,32);
- robots[i].y = random(0,21);
- }
- }
- }
- px = 16;
- py = 10;
- s.drawData(96, 0, 7, 32, 64, side);
- s.gotoPosition(108, 16);
- Serial.print(BOTS - 2);
- delay(100);
- s.gotoPosition(100, 50);
- Serial.print(tscore);
- delay(100);
- }
- void robotstep(){
- for(int r = 0;r < BOTS;r++){
- if(!robots[r].status) continue;
- s.togglePixel(robots[r].x*3, robots[r].y*3, 0);
- s.togglePixel(1+robots[r].x*3, 1+robots[r].y*3, 0);
- s.togglePixel(2+robots[r].x*3, 2+robots[r].y*3, 0);
- delay(10);
- if(robots[r].x > px){robots[r].x--;}
- if(robots[r].x < px){robots[r].x++;}
- if(robots[r].y < py){robots[r].y++;}
- if(robots[r].y > py){robots[r].y--;}
- }
- }
- int getdeadbots(){
- int a;
- int b=0;
- for(a = 0;a < BOTS;a++){
- if(!robots[a].status){b++;}
- }
- return b;
- }
- void loop(){
- int r,rl;
- if(score == (BOTS * 100)){
- s.resetLCD();
- Serial.print("ALL BOTS CLEARED!");
- s.gotoPosition(0,16);
- Serial.print("Press key=next level");
- while(!Serial.available());
- BOTS++;
- Serial.read();
- setup();
- return;
- }
- for(r = 0;r < BOTS;r++){
- for(rl = 0;rl < BOTS;rl++){
- if(robots[r].x == robots[rl].x && robots[r].y == robots[rl].y && r != rl){
- robots[r].status = robots[rl].status = 0;
- }
- }
- if(robots[r].status){
- s.togglePixel(robots[r].x*3, robots[r].y*3, 1);
- s.togglePixel(1+robots[r].x*3, 1+robots[r].y*3, 1);
- s.togglePixel(2+robots[r].x*3, 2+robots[r].y*3, 1);
- }
- else {
- int X,Y;
- for(X = 0;X < 3;X++){
- for(Y = 0;Y < 3;Y++){
- s.togglePixel(robots[r].x*3+X, robots[r].y*3+Y, 1);
- delay(3);
- }
- }
- // delay(25);
- }
- if(robots[r].x == px && robots[r].y == py){
- while(1){
- delay(1000);
- s.resetLCD();
- s.gotoPosition(0,10);
- Serial.print("Score: ");
- Serial.print(tscore+score);
- s.gotoPosition(0,0);
- delay(750);
- Serial.print("GAME OVER!");
- delay(1500);
- }
- }
- }
- if(Serial.available()){
- s.togglePixel(1+px*3,py*3,0);
- s.togglePixel(px*3,py*3+1,0);
- s.togglePixel(2+px*3,py*3+1,0);
- s.togglePixel(1+px*3,2+py*3,0);
- switch(Serial.read()){
- case '1': if(px>0&&py<20){px--; py++;} else {delay(100);return;} break;
- case '2': if(py<20)py++; else {delay(100);return;} break;
- case '3': if(px<31&&py<20){px++; py++;} else {delay(100);return;} break;
- case '4': if(px>0)px--; else {delay(100);return;} break;
- case '5': px = random(0,32); py = random(0,21);
- case '6': if(px<31)px++; else {delay(100);return;} break;
- case '7': if(px>0&&py>0){px--; py--;} else {delay(100);return;} break;
- case '8': if(py>0)py--; else {delay(100);return;} break;
- case '9': if(px<31&&py>0){px++; py--;} else {delay(100);return;} break;
- }
- robotstep();
- score = getdeadbots() * 100;
- s.gotoPosition(100, 50);
- Serial.print(tscore+score);
- }
- s.togglePixel(1+px*3,py*3,1);
- s.togglePixel(px*3,py*3+1,1);
- s.togglePixel(2+px*3,py*3+1,1);
- s.togglePixel(1+px*3,2+py*3,1);
- delay(150);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement