Advertisement
nimajneb

Game of Maze v. 1.2.1

Mar 8th, 2021
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Game of Maze
  2. //Version 1.2.1
  3. //By Benjamin Osborne
  4. //March 8, 2021
  5. //Uses an Arduino Uno, LCD Display 20 char 4 lines, 8x8 LED display
  6. //LCD Display pins are SCL pin A5 and SDA pin A4
  7.  
  8. //Changelog
  9. //Added two rooms with coordinates 1,4 and 1,5
  10. //Added a lose parameter that isn't a button.
  11. //Took win animation out of the winning square to easily change which square wins.
  12. //1.2.1, changed the winning square
  13.  
  14. #include <LedControl.h>
  15. #include "LiquidCrystal_I2C.h"
  16. #include <Wire.h>
  17.  
  18. int DIN = 11;
  19. int CS = 7;
  20. int CLK = 13;
  21. const int onPin = 2;
  22. const int offPin = 3;
  23. const int upPin = 4;
  24. const int downPin = 5;
  25. const int restartPin = 8;
  26. int onState = 0;
  27. int offState = 0;
  28. int upState = 0;
  29. int downState = 0;
  30. byte RightorLeft = 0;
  31. byte UporDown = 1;
  32. boolean left = 0; // 1 can go left 0 can't
  33. boolean right = 1; // 1 can go right 0 can't
  34. boolean up = 1; // 1 can go up 0 can't
  35. boolean down = 0; // 1 can go down 0 can't
  36. boolean restart = 0; //reset program to initial conditions. Restart game
  37. boolean YouWon = 0; //Set winning bit to run win sequence
  38.  
  39. LedControl lc=LedControl(DIN, CLK, CS,0);
  40. LiquidCrystal_I2C lcd(0x27,20,4);
  41.  
  42. // This is the map of the maze.
  43. int room1 [8] ={B11111111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11100111 };
  44. int room2 [8] ={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
  45. int room3 [8] ={B11111111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111 };
  46. int room4 [8] ={B11100111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
  47. int room5 [8] ={B11111111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11100111 };
  48. int room6 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111};
  49. int room7 [8]={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111};
  50. int room8 [8]={B11100111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11100111};
  51. int room9 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11111111};
  52. int room11 [8]={B11100111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11100111};
  53. int room14 [8]={B11100111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11111111};
  54.  
  55.  
  56. //winning animation
  57. int youwin1 [8]={B11111111,B10000001,B10000001,B10011001,B10011001,B10000001,B10000001,B11111111};
  58. int youwin2 [8]={B11111111,B10000001,B10100101,B10000001,B10000001,B10100101,B10000001,B11111111};
  59. int youwin3 [8]={B11111111,B11000011,B10000001,B10000001,B10000001,B10000001,B11000011,B11111111};
  60. int youwin4 [8]={B10000001,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10000001};
  61.  
  62. void setup() {
  63. lcd.init();
  64. lcd.backlight();
  65. lcd.setCursor(0,0); // go to start of 2nd line
  66. lcd.print("Game of Maze");
  67.  
  68. pinMode(onPin, INPUT);
  69. pinMode(offPin, INPUT);
  70.  
  71.   Serial.begin(9600);
  72.   lc.shutdown(0,false);
  73.   lc.setIntensity(0,0);
  74.   lc.clearDisplay(0);
  75.  
  76. }
  77.  
  78. void loop(){
  79. onState = digitalRead(onPin);
  80. offState = digitalRead(offPin);
  81. upState = digitalRead(upPin);
  82. downState = digitalRead(downPin);
  83. restart = digitalRead(restartPin);
  84.  
  85.  
  86. if(onState == HIGH) {
  87.   if(RightorLeft < 4 && right == 1) { //set maximum
  88.   RightorLeft = RightorLeft += 1; //increment value up
  89.   delay(1000);
  90.   }
  91. }
  92. if(offState == HIGH) {
  93.   if(RightorLeft > 0 && left == 1) { //set minimum
  94.   RightorLeft = RightorLeft -= 1; //decrement value down
  95.   delay(1000);
  96.   }
  97. }
  98. if(upState == HIGH) {
  99.   if(UporDown < 4 && down == 1) { //set maximum
  100.   UporDown = UporDown += 1; //increment value up
  101.   delay(1000);
  102.   }
  103. }
  104. if(downState == HIGH) {
  105.   if(UporDown > 0 && up == 1) { //set minimum
  106.   UporDown = UporDown -= 1; //decrement value down
  107.   delay(1000);
  108.   }
  109. }
  110.  
  111. if(YouWon == 1) {
  112.   left = 0; // 1 can go left 0 can't
  113. right = 0; // 1 can go right 0 can't
  114. up = 0; // 1 can go up 0 can't
  115. down = 0; // 1 can go down 0 can't
  116.      lcd.setCursor(0,2); // this is the winning square
  117. lcd.print(restart);
  118. lcd.print(" Hold Restart");
  119.     lcd.setCursor(0,3); // this is the winning square
  120. lcd.print("     You Win!!!    ");
  121.  //for(int i=0;i<8;i++) lc.setRow(0,i,room2[i]);
  122.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin1[i]);
  123.  delay(250);
  124.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin2[i]);
  125.  delay(250);
  126.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin3[i]);
  127.   delay(250);
  128.  for(int i=0;i<8;i++) lc.setRow(0,i,youwin4[i]);
  129.  delay(250);
  130.  if(digitalRead(restartPin) == HIGH){
  131.   //RightorLeft = 0;
  132.   //UporDown = 1;
  133.   YouWon = 0;
  134. }
  135. }
  136.  
  137.  
  138.  
  139. if(RightorLeft == 0 && UporDown == 0) {
  140.  left = 0; // 1 can go left 0 can't
  141. right = 0; // 1 can go right 0 can't
  142. up = 0; // 1 can go up 0 can't
  143. down = 1; // 1 can go down 0 can't
  144. //YouWon = 1;
  145.  for(int i=0;i<8;i++) lc.setRow(0,i,room1[i]);
  146. }
  147. if(RightorLeft == 1 && UporDown == 0) {
  148.   left = 0; // 1 can go left 0 can't
  149. right = 1; // 1 can go right 0 can't
  150. up = 0; // 1 can go up 0 can't
  151. down = 0; // 1 can go down 0 can't
  152. //YouWon = 1;
  153. for(int i=0;i<8;i++) lc.setRow(0,i,room2[i]);
  154. }
  155. if(RightorLeft == 2 && UporDown == 0) {
  156.  for(int i=0;i<8;i++) lc.setRow(0,i,room3[i]);
  157.  left = 1; // 1 can go left 0 can't
  158. right = 0; // 1 can go right 0 can't
  159. up = 0; // 1 can go up 0 can't
  160. down = 1; // 1 can go down 0 can't
  161. }
  162. if(RightorLeft == 0 && UporDown == 1) {
  163. for(int i=0;i<8;i++) lc.setRow(0,i,room4[i]);
  164. left = 0; // 1 can go left 0 can't
  165. right = 1; // 1 can go right 0 can't
  166. up = 1; // 1 can go up 0 can't
  167. down = 0; // 1 can go down 0 can't
  168. }
  169. if(RightorLeft == 1 && UporDown == 1) {
  170.  for(int i=0;i<8;i++) lc.setRow(0,i,room5[i]);
  171.  left = 1; // 1 can go left 0 can't
  172. right = 1; // 1 can go right 0 can't
  173. up = 0; // 1 can go up 0 can't
  174. down = 1; // 1 can go down 0 can't
  175. }
  176. if(RightorLeft == 2 && UporDown == 1) {
  177.  for(int i=0;i<8;i++) lc.setRow(0,i,room6[i]);
  178.  left = 1; // 1 can go left 0 can't
  179. right = 0; // 1 can go right 0 can't
  180. up = 1; // 1 can go up 0 can't
  181. down = 1; // 1 can go down 0 can't
  182. }
  183. if(RightorLeft == 0 && UporDown == 2) {
  184.  YouWon = 1;
  185.  for(int i=0;i<8;i++) lc.setRow(0,i,room7[i]);
  186.  left = 0; // 1 can go left 0 can't
  187. right = 1; // 1 can go right 0 can't
  188. up = 0; // 1 can go up 0 can't
  189. down = 0; // 1 can go down 0 can't
  190. }
  191. if(RightorLeft == 1 && UporDown == 2) {
  192.  for(int i=0;i<8;i++) lc.setRow(0,i,room8[i]);
  193.  left = 1; // 1 can go left 0 can't
  194. right = 1; // 1 can go right 0 can't
  195. up = 1; // 1 can go up 0 can't
  196. down = 1; // 1 can go down 0 can't
  197. }
  198. if(RightorLeft == 2 && UporDown == 2) {
  199.  for(int i=0;i<8;i++) lc.setRow(0,i,room9[i]);
  200.  left = 1; // 1 can go left 0 can't
  201. right = 0; // 1 can go right 0 can't
  202. up = 1; // 1 can go up 0 can't
  203. down = 0; // 1 can go down 0 can't
  204. }
  205. if(RightorLeft == 1 && UporDown == 3) {
  206.  for(int i=0;i<8;i++) lc.setRow(0,i,room11[i]);
  207.  left = 0; // 1 can go left 0 can't
  208. right = 0; // 1 can go right 0 can't
  209. up = 1; // 1 can go up 0 can't
  210. down = 1; // 1 can go down 0 can't
  211. }
  212. if(RightorLeft == 1 && UporDown == 4) {
  213.  for(int i=0;i<8;i++) lc.setRow(0,i,room14[i]);
  214.  left = 0; // 1 can go left 0 can't
  215. right = 0; // 1 can go right 0 can't
  216. up = 0; // 1 can go up 0 can't
  217. down = 0; // 1 can go down 0 can't
  218. while (digitalRead(restartPin) == LOW) {
  219.   lcd.setCursor(0,2); // You lose when entering this square
  220. lcd.print("You Lose, press rese");
  221. lcd.setCursor(0,3);
  222. lcd.print("t to start over    ");
  223.  
  224. }
  225. }
  226.  
  227. lcd.setCursor(0,1); // go to start of 2nd line
  228. lcd.print(RightorLeft);
  229. lcd.setCursor(1,1); // go to start of 2nd line
  230. lcd.print(UporDown);
  231.  
  232. lcd.setCursor(0,3); // display which ways player can go.
  233. if (left == 1) {
  234.   lcd.print("Left");
  235. } else {
  236.   lcd.print("    ");
  237. }
  238. lcd.setCursor(6,3); // display which ways player can go.
  239. if (right == 1) {
  240.   lcd.print("Right");
  241. }else {
  242.   lcd.print("     ");
  243. }
  244. lcd.setCursor(12,3); // display which ways player can go.
  245. if (up == 1) {
  246.   lcd.print("Up");
  247. }else {
  248.   lcd.print("  ");
  249. }
  250.  
  251.   lcd.setCursor(15,3); // display which ways player can go.
  252. if (down == 1) {
  253.   lcd.print("Down");
  254. }else {
  255.   lcd.print("    ");
  256. }
  257.  
  258.   lcd.setCursor(0,2); //print restart bit status
  259.   lcd.print(restart);
  260.  
  261.    if(restart == 1){ //restart game after winning
  262.   lcd.setCursor(0,3);
  263.   lcd.print("     Game Over      ");
  264.   lcd.setCursor(1,2);
  265.   lcd.print("                   ");
  266.   delay(3000);
  267.   lcd.setCursor(0,3);
  268.   lcd.print("                    ");
  269.   restart = 0;
  270.   YouWon = 0;
  271.     RightorLeft = 0;
  272.   UporDown = 1;
  273.    }
  274. Serial.println(RightorLeft);
  275.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement