Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Maze.h"
- #include "LedControl.h"
- #define VRx A0
- #define VRy A1
- #define SW 0
- #define BUZZ 1
- #define DIN 11
- #define CS 12
- #define CLK 13
- int joy[2];
- int pos[2];
- Maze maze = Maze(4, 4);
- LedControl lc = LedControl(11, 13, 12, 1);
- void setup() {
- pinMode(SW, INPUT);
- digitalWrite(SW, 1);
- //Serial.begin(9600);
- pos[0] = 1;
- pos[1] = 1;
- lc.shutdown(0, false);
- lc.setIntensity(0,8);
- lc.clearDisplay(0);
- }
- void loop() {
- int displayControl = 0;
- while(digitalRead(SW) == 0) {}
- while (!winCheck()) {
- if (digitalRead(SW) == 0) {
- restartGame();
- break;
- }
- parseJoy(joy);
- if (displayControl == 0) {
- if (!(maze.bmap[pos[0]+joy[0]][pos[1]] || pos[0]+joy[0]>7)) {
- pos[0] += joy[0];
- }
- if (!(maze.bmap[pos[0]][pos[1]+joy[1]] || pos[1]+joy[1]>7)) {
- pos[1] += joy[1];
- }
- }
- //debugJoy();
- printDotMatrix(displayControl);
- delay(20);
- displayControl = (displayControl+1)%2;
- }
- displayVictory();
- }
- void restartGame(){
- maze.generateNewMaze();
- pos[0] = 1;
- pos[1] = 1;
- }
- int tones[] = {400, 400, 503, 503, 599, 599, 800, 800, 0, 0, 599, 599, 800, 800, 800, 800};
- void displayVictory() {
- restartGame();
- boolean broken = false;
- boolean first = true;
- while (digitalRead(SW)==1) {
- for (int i = 0; i < 16; i++) {
- if (first) {
- if (tones[i] == 0) {
- noTone(BUZZ);
- } else {
- tone(BUZZ, tones[i]);
- }
- }
- if (i < 8) {
- lc.setRow(0, i, B00000000);
- } else {
- lc.setRow(0, i-8, B11111111);
- }
- delay(100);
- if (digitalRead(SW)==0) {
- broken = true;
- break;
- }
- }
- noTone(BUZZ);
- first = false;
- if (broken) {break;}
- }
- }
- boolean winCheck() {
- return pos[0] == 7 && pos[1] == 7;
- }
- void printDotMatrix(int show) {
- //lc.clearDisplay(0);
- for (int i = 0; i < 8; i++) {
- byte copy = maze.bitmap[i];
- if (i == pos[0] && show == 0) {
- bitSet(copy, pos[1]);
- }
- lc.setRow(0, i, copy);
- }
- }
- void debugJoy() {
- Serial.print("Switch: ");
- Serial.print(digitalRead(SW));
- Serial.print(" X: ");
- Serial.print(analogRead(VRx));
- Serial.print(" Y: ");
- Serial.print(analogRead(VRy));
- Serial.print(" Pos: ");
- Serial.print(pos[0]);
- Serial.print(":");
- Serial.println(pos[1]);
- delay(500);
- }
- void parseJoy(int j[]) {
- int x = 0;
- int y = 0;
- int joy_x = analogRead(VRx);
- int joy_y = analogRead(VRy);
- if (joy_x < 500) {
- x = 1;
- } else if (joy_x > 550) {
- x = -1;
- }
- if (joy_y < 500) {
- y = -1;
- } else if (joy_y > 550) {
- y = 1;
- }
- j[0] = x;
- j[1] = y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement