Advertisement
CaptainSpaceCat

Maze Runner

Apr 2nd, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include "Maze.h"
  2. #include "LedControl.h"
  3. #define VRx A0
  4. #define VRy A1
  5. #define SW 0
  6. #define BUZZ 1
  7. #define DIN 11
  8. #define CS 12
  9. #define CLK 13
  10.  
  11. int joy[2];
  12. int pos[2];
  13. Maze maze = Maze(4, 4);
  14. LedControl lc = LedControl(11, 13, 12, 1);
  15.  
  16. void setup() {
  17.   pinMode(SW, INPUT);
  18.   digitalWrite(SW, 1);
  19.   //Serial.begin(9600);
  20.   pos[0] = 1;
  21.   pos[1] = 1;
  22.   lc.shutdown(0, false);
  23.   lc.setIntensity(0,8);
  24.   lc.clearDisplay(0);
  25. }
  26.  
  27. void loop() {
  28.   int displayControl = 0;
  29.   while(digitalRead(SW) == 0) {}
  30.   while (!winCheck()) {
  31.     if (digitalRead(SW) == 0) {
  32.       restartGame();
  33.       break;
  34.     }
  35.     parseJoy(joy);
  36.     if (displayControl == 0) {
  37.       if (!(maze.bmap[pos[0]+joy[0]][pos[1]] || pos[0]+joy[0]>7)) {
  38.         pos[0] += joy[0];
  39.       }
  40.       if (!(maze.bmap[pos[0]][pos[1]+joy[1]] || pos[1]+joy[1]>7)) {
  41.         pos[1] += joy[1];
  42.       }
  43.     }
  44.     //debugJoy();
  45.     printDotMatrix(displayControl);
  46.     delay(20);
  47.     displayControl = (displayControl+1)%2;
  48.   }
  49.   displayVictory();
  50. }
  51.  
  52. void restartGame(){
  53.   maze.generateNewMaze();
  54.   pos[0] = 1;
  55.   pos[1] = 1;
  56. }
  57.  
  58. int tones[] = {400, 400, 503, 503, 599, 599, 800, 800, 0, 0, 599, 599, 800, 800, 800, 800};
  59.  
  60. void displayVictory() {
  61.   restartGame();
  62.   boolean broken = false;
  63.   boolean first = true;
  64.   while (digitalRead(SW)==1) {
  65.     for (int i = 0; i < 16; i++) {
  66.       if (first) {
  67.         if (tones[i] == 0) {
  68.           noTone(BUZZ);
  69.         } else {
  70.           tone(BUZZ, tones[i]);
  71.         }
  72.       }
  73.       if (i < 8) {
  74.         lc.setRow(0, i, B00000000);
  75.       } else {
  76.         lc.setRow(0, i-8, B11111111);
  77.       }
  78.       delay(100);
  79.       if (digitalRead(SW)==0) {
  80.         broken = true;
  81.         break;
  82.       }
  83.     }
  84.     noTone(BUZZ);
  85.     first = false;
  86.     if (broken) {break;}
  87.   }
  88. }
  89.  
  90. boolean winCheck() {
  91.   return pos[0] == 7 && pos[1] == 7;
  92. }
  93.  
  94. void printDotMatrix(int show) {
  95.   //lc.clearDisplay(0);
  96.   for (int i = 0; i < 8; i++) {
  97.     byte copy = maze.bitmap[i];
  98.     if (i == pos[0] && show == 0) {
  99.       bitSet(copy, pos[1]);
  100.     }
  101.     lc.setRow(0, i, copy);
  102.   }
  103. }
  104.  
  105. void debugJoy() {
  106.   Serial.print("Switch: ");
  107.   Serial.print(digitalRead(SW));
  108.   Serial.print(" X: ");
  109.   Serial.print(analogRead(VRx));
  110.   Serial.print(" Y: ");
  111.   Serial.print(analogRead(VRy));
  112.   Serial.print(" Pos: ");
  113.   Serial.print(pos[0]);
  114.   Serial.print(":");
  115.   Serial.println(pos[1]);
  116.   delay(500);
  117. }
  118.  
  119. void parseJoy(int j[]) {
  120.   int x = 0;
  121.   int y = 0;
  122.   int joy_x = analogRead(VRx);
  123.   int joy_y = analogRead(VRy);
  124.   if (joy_x < 500) {
  125.     x = 1;
  126.   } else if (joy_x > 550) {
  127.     x = -1;
  128.   }
  129.   if (joy_y < 500) {
  130.     y = -1;
  131.   } else if (joy_y > 550) {
  132.     y = 1;
  133.   }
  134.   j[0] = x;
  135.   j[1] = y;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement