Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Game of Maze
- //Version 1.2.1
- //By Benjamin Osborne
- //March 8, 2021
- //Uses an Arduino Uno, LCD Display 20 char 4 lines, 8x8 LED display
- //LCD Display pins are SCL pin A5 and SDA pin A4
- //Changelog
- //Added two rooms with coordinates 1,4 and 1,5
- //Added a lose parameter that isn't a button.
- //Took win animation out of the winning square to easily change which square wins.
- //1.2.1, changed the winning square
- #include <LedControl.h>
- #include "LiquidCrystal_I2C.h"
- #include <Wire.h>
- int DIN = 11;
- int CS = 7;
- int CLK = 13;
- const int onPin = 2;
- const int offPin = 3;
- const int upPin = 4;
- const int downPin = 5;
- const int restartPin = 8;
- int onState = 0;
- int offState = 0;
- int upState = 0;
- int downState = 0;
- byte RightorLeft = 0;
- byte UporDown = 1;
- boolean left = 0; // 1 can go left 0 can't
- boolean right = 1; // 1 can go right 0 can't
- boolean up = 1; // 1 can go up 0 can't
- boolean down = 0; // 1 can go down 0 can't
- boolean restart = 0; //reset program to initial conditions. Restart game
- boolean YouWon = 0; //Set winning bit to run win sequence
- LedControl lc=LedControl(DIN, CLK, CS,0);
- LiquidCrystal_I2C lcd(0x27,20,4);
- // This is the map of the maze.
- int room1 [8] ={B11111111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11100111 };
- int room2 [8] ={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
- int room3 [8] ={B11111111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111 };
- int room4 [8] ={B11100111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
- int room5 [8] ={B11111111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11100111 };
- int room6 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111};
- int room7 [8]={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111};
- int room8 [8]={B11100111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11100111};
- int room9 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11111111};
- int room11 [8]={B11100111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11100111};
- int room14 [8]={B11100111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11111111};
- //winning animation
- int youwin1 [8]={B11111111,B10000001,B10000001,B10011001,B10011001,B10000001,B10000001,B11111111};
- int youwin2 [8]={B11111111,B10000001,B10100101,B10000001,B10000001,B10100101,B10000001,B11111111};
- int youwin3 [8]={B11111111,B11000011,B10000001,B10000001,B10000001,B10000001,B11000011,B11111111};
- int youwin4 [8]={B10000001,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10000001};
- void setup() {
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0,0); // go to start of 2nd line
- lcd.print("Game of Maze");
- pinMode(onPin, INPUT);
- pinMode(offPin, INPUT);
- Serial.begin(9600);
- lc.shutdown(0,false);
- lc.setIntensity(0,0);
- lc.clearDisplay(0);
- }
- void loop(){
- onState = digitalRead(onPin);
- offState = digitalRead(offPin);
- upState = digitalRead(upPin);
- downState = digitalRead(downPin);
- restart = digitalRead(restartPin);
- if(onState == HIGH) {
- if(RightorLeft < 4 && right == 1) { //set maximum
- RightorLeft = RightorLeft += 1; //increment value up
- delay(1000);
- }
- }
- if(offState == HIGH) {
- if(RightorLeft > 0 && left == 1) { //set minimum
- RightorLeft = RightorLeft -= 1; //decrement value down
- delay(1000);
- }
- }
- if(upState == HIGH) {
- if(UporDown < 4 && down == 1) { //set maximum
- UporDown = UporDown += 1; //increment value up
- delay(1000);
- }
- }
- if(downState == HIGH) {
- if(UporDown > 0 && up == 1) { //set minimum
- UporDown = UporDown -= 1; //decrement value down
- delay(1000);
- }
- }
- if(YouWon == 1) {
- left = 0; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- lcd.setCursor(0,2); // this is the winning square
- lcd.print(restart);
- lcd.print(" Hold Restart");
- lcd.setCursor(0,3); // this is the winning square
- lcd.print(" You Win!!! ");
- //for(int i=0;i<8;i++) lc.setRow(0,i,room2[i]);
- for(int i=0;i<8;i++) lc.setRow(0,i,youwin1[i]);
- delay(250);
- for(int i=0;i<8;i++) lc.setRow(0,i,youwin2[i]);
- delay(250);
- for(int i=0;i<8;i++) lc.setRow(0,i,youwin3[i]);
- delay(250);
- for(int i=0;i<8;i++) lc.setRow(0,i,youwin4[i]);
- delay(250);
- if(digitalRead(restartPin) == HIGH){
- //RightorLeft = 0;
- //UporDown = 1;
- YouWon = 0;
- }
- }
- if(RightorLeft == 0 && UporDown == 0) {
- left = 0; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- //YouWon = 1;
- for(int i=0;i<8;i++) lc.setRow(0,i,room1[i]);
- }
- if(RightorLeft == 1 && UporDown == 0) {
- left = 0; // 1 can go left 0 can't
- right = 1; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- //YouWon = 1;
- for(int i=0;i<8;i++) lc.setRow(0,i,room2[i]);
- }
- if(RightorLeft == 2 && UporDown == 0) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room3[i]);
- left = 1; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- }
- if(RightorLeft == 0 && UporDown == 1) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room4[i]);
- left = 0; // 1 can go left 0 can't
- right = 1; // 1 can go right 0 can't
- up = 1; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- }
- if(RightorLeft == 1 && UporDown == 1) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room5[i]);
- left = 1; // 1 can go left 0 can't
- right = 1; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- }
- if(RightorLeft == 2 && UporDown == 1) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room6[i]);
- left = 1; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 1; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- }
- if(RightorLeft == 0 && UporDown == 2) {
- YouWon = 1;
- for(int i=0;i<8;i++) lc.setRow(0,i,room7[i]);
- left = 0; // 1 can go left 0 can't
- right = 1; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- }
- if(RightorLeft == 1 && UporDown == 2) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room8[i]);
- left = 1; // 1 can go left 0 can't
- right = 1; // 1 can go right 0 can't
- up = 1; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- }
- if(RightorLeft == 2 && UporDown == 2) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room9[i]);
- left = 1; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 1; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- }
- if(RightorLeft == 1 && UporDown == 3) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room11[i]);
- left = 0; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 1; // 1 can go up 0 can't
- down = 1; // 1 can go down 0 can't
- }
- if(RightorLeft == 1 && UporDown == 4) {
- for(int i=0;i<8;i++) lc.setRow(0,i,room14[i]);
- left = 0; // 1 can go left 0 can't
- right = 0; // 1 can go right 0 can't
- up = 0; // 1 can go up 0 can't
- down = 0; // 1 can go down 0 can't
- while (digitalRead(restartPin) == LOW) {
- lcd.setCursor(0,2); // You lose when entering this square
- lcd.print("You Lose, press rese");
- lcd.setCursor(0,3);
- lcd.print("t to start over ");
- }
- }
- lcd.setCursor(0,1); // go to start of 2nd line
- lcd.print(RightorLeft);
- lcd.setCursor(1,1); // go to start of 2nd line
- lcd.print(UporDown);
- lcd.setCursor(0,3); // display which ways player can go.
- if (left == 1) {
- lcd.print("Left");
- } else {
- lcd.print(" ");
- }
- lcd.setCursor(6,3); // display which ways player can go.
- if (right == 1) {
- lcd.print("Right");
- }else {
- lcd.print(" ");
- }
- lcd.setCursor(12,3); // display which ways player can go.
- if (up == 1) {
- lcd.print("Up");
- }else {
- lcd.print(" ");
- }
- lcd.setCursor(15,3); // display which ways player can go.
- if (down == 1) {
- lcd.print("Down");
- }else {
- lcd.print(" ");
- }
- lcd.setCursor(0,2); //print restart bit status
- lcd.print(restart);
- if(restart == 1){ //restart game after winning
- lcd.setCursor(0,3);
- lcd.print(" Game Over ");
- lcd.setCursor(1,2);
- lcd.print(" ");
- delay(3000);
- lcd.setCursor(0,3);
- lcd.print(" ");
- restart = 0;
- YouWon = 0;
- RightorLeft = 0;
- UporDown = 1;
- }
- Serial.println(RightorLeft);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement