Advertisement
Snailbob12765

_8x8ledMatrixConsole

Jun 19th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.23 KB | None | 0 0
  1. const int ledH[] = {2,3,4,5,6,7,8,9};
  2. const int ledV[] = {10,11,12,13,A0,A1,A2,A3};
  3. const int button[] = {A4,A5};
  4.  
  5. int dataVar[] = {0,0,0,0,0,0,0,0};
  6. int counter = 0;
  7. int buttonVar[] = {0,0};
  8. int buttonLock[] = {0,0};
  9.  
  10. int game = -1;
  11. int score = 0;
  12. int storageVar = 0;
  13. int death = 0;
  14. int sped = 500;
  15.  
  16. int pipes[] = {9,13};
  17. int pipeSpace[] = {0,0};
  18. int playerY = -1;
  19.  
  20. int paddle = 0;
  21. int ballX = 0;
  22. int ballY = 0;
  23. int ballDir = 0;
  24.  
  25. int headX = 0;
  26. int headY = 0;
  27. int headDir = 0;
  28. int tailX = 0;
  29. int tailY = 0;
  30. int tailDir = 0;
  31. int eat = 0;
  32. int snakeMap0[] = {0,0,0,0,0,0,0,0};
  33. int snakeMap1[] = {0,0,0,0,0,0,0,0};
  34. int snakeMap2[] = {0,0,0,0,0,0,0,0};
  35. int snakeMap3[] = {0,0,0,0,0,0,0,0};
  36. int snakeMap4[] = {0,0,0,0,0,0,0,0};
  37. int snakeMap5[] = {0,0,0,0,0,0,0,0};
  38. int snakeMap6[] = {0,0,0,0,0,0,0,0};
  39. int snakeMap7[] = {0,0,0,0,0,0,0,0};
  40. int snakeMap = 0;
  41. int foodPos[] = {0,0};
  42. int foodBlink = 0;
  43. int test = 0;
  44.  
  45. void setup() {
  46. for(int i = 0; i < 8; i++) {
  47. pinMode(ledV[i], OUTPUT);
  48. pinMode(ledH[i], OUTPUT);
  49. }
  50. pinMode(button[0], INPUT);
  51. pinMode(button[1], INPUT);
  52. digitalWrite(button[0], HIGH);
  53. digitalWrite(button[1], HIGH);
  54. randomSeed(1);
  55. }
  56. void loop() {
  57. playerY = -1;
  58. if(game == 0) {
  59. dataVar[0] = 24;
  60. dataVar[1] = 36;
  61. dataVar[2] = 66;
  62. dataVar[3] = 255;
  63. dataVar[4] = 66;
  64. dataVar[5] = 66;
  65. dataVar[6] = 66;
  66. dataVar[7] = 126;
  67. }
  68. if(game == 1) {
  69. dataVar[0] = 96;
  70. dataVar[1] = 0;
  71. dataVar[2] = 0;
  72. dataVar[3] = 6;
  73. dataVar[4] = 102;
  74. dataVar[5] = 96;
  75. dataVar[6] = 96;
  76. dataVar[7] = 96;
  77. }
  78. if(game == 2) {
  79. dataVar[0] = 0;
  80. dataVar[1] = 96;
  81. dataVar[2] = 96;
  82. dataVar[3] = 0;
  83. dataVar[4] = 0;
  84. dataVar[5] = 62;
  85. dataVar[6] = 62;
  86. dataVar[7] = 0;
  87. }
  88. if(game == 3) {
  89. dataVar[0] = 0;
  90. dataVar[1] = 126;
  91. dataVar[2] = 66;
  92. dataVar[3] = 66;
  93. dataVar[4] = 18;
  94. dataVar[5] = 2;
  95. dataVar[6] = 2;
  96. dataVar[7] = 2;
  97. }
  98. buttonVar[0] = digitalRead(button[0]);
  99. if(buttonVar[0] == HIGH) {
  100. if(buttonLock[0] == 0) {
  101. if(game == 3) {
  102. game = 0;
  103. } else {
  104. game = game + 1;
  105. }
  106. buttonLock[0] = 1;
  107. }
  108. } else {
  109. buttonLock[0] = 0;
  110. }
  111. buttonVar[1] = digitalRead(button[1]);
  112. if(buttonVar[1] == HIGH) {
  113. if(buttonLock[1] == 0) {
  114. for(int a = 0; a < 8; a++) {
  115. dataVar[a] = 0;
  116. }
  117. Game();
  118. buttonLock[1] = 1;
  119. }
  120. } else {
  121. buttonLock[1] = 0;
  122. }
  123. counter = 0;
  124. for(int a = 0; a < 8; a++) {
  125. display();
  126. }
  127. }
  128.  
  129. void display() {
  130. for(int i = 0; i < 8; i++) {
  131. if(counter == foodPos[1] && i == foodPos[0] && test == 1) {
  132. if(foodBlink == 1) {
  133. digitalWrite(ledV[i], LOW);
  134. } else {
  135. digitalWrite(ledV[i], HIGH);
  136. }
  137. } else {
  138. if(bitRead(dataVar[counter], i) == 0) {
  139. digitalWrite(ledV[i], HIGH);
  140. } else {
  141. digitalWrite(ledV[i], LOW);
  142. }
  143. }
  144. }
  145. if(counter == playerY) {
  146. if(bitRead(dataVar[counter], 1) == 1) {
  147. death = 1;
  148. } else {
  149. digitalWrite(ledV[1], LOW);
  150. }
  151. }
  152. digitalWrite(ledH[counter], HIGH);
  153. digitalWrite(ledH[counter], LOW);
  154. counter = counter + 1;
  155. }
  156.  
  157. void Death() {
  158. dataVar[0] = 165;
  159. dataVar[1] = 66;
  160. dataVar[2] = 165;
  161. dataVar[3] = 0;
  162. dataVar[4] = 60;
  163. dataVar[5] = 66;
  164. dataVar[6] = 0;
  165. playerY = -1;
  166. for(int l = 0; l < 1700; l++) {
  167. counter = 0;
  168. for(int a = 0; a < 8; a++) {
  169. display();
  170. }
  171. }
  172. for(int l = 0; l < 6; l++) {
  173. dataVar[l] = 0;
  174. }
  175. storageVar = floor((score - 1) / 10);
  176. testA();
  177. storageVar = (score - 1) % 10;
  178. testB();
  179. for(int l = 0; l < 1; l+= 0) {
  180. counter = 0;
  181. for(int a = 0; a < 8; a++) {
  182. display();
  183. }
  184. buttonVar[0] = digitalRead(button[0]);
  185. if(buttonVar[0] == HIGH) {
  186. if(buttonLock[0] == 0) {
  187. Game();
  188. buttonLock[0] = 1;
  189. }
  190. } else {
  191. buttonLock[0] = 0;
  192. }
  193. buttonVar[1] = digitalRead(button[1]);
  194. if(buttonVar[1] == HIGH) {
  195. if(buttonLock[1] == 0) {
  196. game = 0;
  197. l = 1;
  198. buttonLock[1] = 1;
  199. }
  200.  
  201. } else {
  202. buttonLock[1] = 0;
  203. }
  204. }
  205. }
  206.  
  207. void Game() {
  208. if(game == 1) {
  209. Pipes();
  210. }
  211. if(game == 2) {
  212. Pong();
  213. }
  214. if(game == 3) {
  215. Snake();
  216. }
  217. }
  218.  
  219. void testA() {
  220. if(storageVar == 0) {
  221. for(int a = 1; a < 6; a++) {
  222. dataVar[a] = dataVar[a] + 5;
  223. }
  224. dataVar[1] = dataVar[1] + 2;
  225. dataVar[5] = dataVar[5] + 2;
  226. }
  227. if(storageVar == 1) {
  228. for(int a = 1; a < 6; a++) {
  229. dataVar[a] = dataVar[a] + 4;
  230. }
  231. }
  232. if(storageVar == 2) {
  233. barsA();
  234. dataVar[2] = dataVar[2] + 4;
  235. dataVar[4] = dataVar[4] + 1;
  236. }
  237. if(storageVar == 3) {
  238. barsA();
  239. dataVar[2] = dataVar[2] + 4;
  240. dataVar[4] = dataVar[4] + 4;
  241. }
  242. if(storageVar == 4) {
  243. dataVar[1] = dataVar[1] + 5;
  244. dataVar[2] = dataVar[2] + 5;
  245. dataVar[3] = dataVar[3] + 7;
  246. dataVar[4] = dataVar[4] + 4;
  247. dataVar[5] = dataVar[5] + 4;
  248. }
  249. if(storageVar == 5) {
  250. barsA();
  251. dataVar[2] = dataVar[2] + 1;
  252. dataVar[4] = dataVar[4] + 4;
  253. }
  254. if(storageVar == 6) {
  255. barsA();
  256. dataVar[2] = dataVar[2] + 1;
  257. dataVar[4] = dataVar[4] + 5;
  258. }
  259. if(storageVar == 7) {
  260. dataVar[1] = dataVar[1] + 7;
  261. for(int a = 2; a < 6; a++) {
  262. dataVar[a] = dataVar[a] + 4;
  263. }
  264. }
  265. if(storageVar == 8) {
  266. barsA();
  267. dataVar[2] = dataVar[2] + 5;
  268. dataVar[4] = dataVar[4] + 5;
  269. }
  270. if(storageVar == 9) {
  271. dataVar[1] = dataVar[1] + 7;
  272. dataVar[2] = dataVar[2] + 5;
  273. dataVar[3] = dataVar[3] + 7;
  274. dataVar[4] = dataVar[4] + 4;
  275. dataVar[5] = dataVar[5] + 4;
  276. }
  277. }
  278. void barsA() {
  279. for(int a = 1; a < 6; a+= 2) {
  280. dataVar[a] = dataVar[a] + 7;
  281. }
  282. }
  283. void testB() {
  284. if(storageVar == 0) {
  285. for(int a = 1; a < 6; a++) {
  286. dataVar[a] = dataVar[a] + 80;
  287. }
  288. dataVar[1] = dataVar[1] + 32;
  289. dataVar[5] = dataVar[5] + 32;
  290. }
  291. if(storageVar == 1) {
  292. for(int a = 1; a < 6; a++) {
  293. dataVar[a] = dataVar[a] + 64;
  294. }
  295. }
  296. if(storageVar == 2) {
  297. barsB();
  298. dataVar[2] = dataVar[2] + 64;
  299. dataVar[4] = dataVar[4] + 16;
  300. }
  301. if(storageVar == 3) {
  302. barsB();
  303. dataVar[2] = dataVar[2] + 64;
  304. dataVar[4] = dataVar[4] + 64;
  305. }
  306. if(storageVar == 4) {
  307. dataVar[1] = dataVar[1] + 80;
  308. dataVar[2] = dataVar[2] + 80;
  309. dataVar[3] = dataVar[3] + 112;
  310. dataVar[4] = dataVar[4] + 64;
  311. dataVar[5] = dataVar[5] + 64;
  312. }
  313. if(storageVar == 5) {
  314. barsB();
  315. dataVar[2] = dataVar[2] + 16;
  316. dataVar[4] = dataVar[4] + 64;
  317. }
  318. if(storageVar == 6) {
  319. barsB();
  320. dataVar[2] = dataVar[2] + 16;
  321. dataVar[4] = dataVar[4] + 80;
  322. }
  323. if(storageVar == 7) {
  324. dataVar[1] = dataVar[1] + 112;
  325. for(int a = 2; a < 6; a++) {
  326. dataVar[a] = dataVar[a] + 64;
  327. }
  328. }
  329. if(storageVar == 8) {
  330. barsB();
  331. dataVar[2] = dataVar[2] + 80;
  332. dataVar[4] = dataVar[4] + 80;
  333. }
  334. if(storageVar == 9) {
  335. dataVar[1] = dataVar[1] + 112;
  336. dataVar[2] = dataVar[2] + 80;
  337. dataVar[3] = dataVar[3] + 112;
  338. dataVar[4] = dataVar[4] + 64;
  339. dataVar[5] = dataVar[5] + 64;
  340. }
  341. }
  342. void barsB() {
  343. for(int a = 1; a < 6; a+= 2) {
  344. dataVar[a] = dataVar[a] + 112;
  345. }
  346. }
  347.  
  348. void Pipes() {
  349. sped = 500;
  350. playerY = 0;
  351. score = 0;
  352. pipes[0] = 9;
  353. pipes[1] = 13;
  354. for(death = 0; death == 0;) {
  355. for(int a = 0; a < 4; a++) {
  356. if(pipes[0] == 0) {
  357. pipes[0] = 7;
  358. pipeSpace[0] = random(0,3);
  359. } else {
  360. pipes[0] = pipes[0] - 1;
  361. }
  362. if(pipes[1] == 0) {
  363. pipes[1] = 7;
  364. pipeSpace[1] = random(1,4);
  365. } else {
  366. pipes[1] = pipes[1] - 1;
  367. }
  368. for(int l = 0; l < 6; l++) {
  369. dataVar[l] = pow(2,pipes[0]) + pow(2,pipes[1]) + 1;
  370. }
  371. dataVar[pipeSpace[0]] = dataVar[pipeSpace[0]] - pow(2,pipes[0]);
  372. dataVar[pipeSpace[0] + 1] = dataVar[pipeSpace[0] + 1] - pow(2,pipes[0]);
  373. dataVar[pipeSpace[1]] = dataVar[pipeSpace[1]] - pow(2,pipes[1]);
  374. dataVar[pipeSpace[1] + 1] = dataVar[pipeSpace[1] + 1] - pow(2,pipes[1]);
  375. dataVar[7] = score;
  376. for(int j = 0; j < sped; j++) {
  377. buttonVar[0] = digitalRead(button[0]);
  378. if(buttonVar[0] == HIGH) {
  379. if(buttonLock[0] == 0) {
  380. if(playerY < 5) {
  381. playerY = playerY + 1;
  382. buttonLock[0] = 1;
  383. }
  384. }
  385. } else {
  386. buttonLock[0] = 0;
  387. }
  388. buttonVar[1] = digitalRead(button[1]);
  389. if(buttonVar[1] == HIGH) {
  390. if(buttonLock[1] == 0) {
  391. if(playerY > 0) {
  392. playerY = playerY - 1;
  393. buttonLock[1] = 1;
  394. }
  395. }
  396. } else {
  397. buttonLock[1] = 0;
  398. }
  399. counter = 0;
  400. for(int a = 0; a < 8; a++) {
  401. display();
  402. }
  403. }
  404. }
  405. score = score + 1;
  406. sped = sped - 5;
  407. }
  408. Death();
  409. }
  410.  
  411. void Pong() {
  412. sped = 400;
  413. score = 0;
  414. ballX = 8;
  415. ballY = 3;
  416. ballDir = 2;
  417. paddle = 8;
  418. for(death = 0; death == 0;) {
  419. for(int a = 0; a < 8; a++) {
  420. if(ballX == 128) {
  421. ballDir = ballDir + 2;
  422. }
  423. if(ballY == 0) {
  424. ballDir = ballDir + 1;
  425. }
  426. if(ballX == 1) {
  427. ballDir = ballDir - 2;
  428. }
  429. if(ballY == 5) {
  430. if(ballX == paddle || ballX == paddle / 2 || ballX == paddle * 2) {
  431. ballDir = ballDir - 1;
  432. }
  433. if(ballDir == 1 && ballX * 2 == paddle / 2) {
  434. ballDir = 2;
  435. }
  436. if(ballDir == 3 && ballX / 2 == paddle * 2) {
  437. ballDir = 0;
  438. }
  439. }
  440. if(ballY == 7) {
  441. death = 1;
  442. a = 8;
  443. }
  444. if(ballDir == 0) {
  445. ballX = ballX * 2;
  446. ballY = ballY - 1;
  447. }
  448. if(ballDir == 1) {
  449. ballX = ballX * 2;
  450. ballY = ballY + 1;
  451. }
  452. if(ballDir == 2) {
  453. ballX = ballX / 2;
  454. ballY = ballY - 1;
  455. }
  456. if(ballDir == 3) {
  457. ballX = ballX / 2;
  458. ballY = ballY + 1;
  459. }
  460. for(int l = 0; l < 6; l++) {
  461. dataVar[l] = 0;
  462. }
  463. dataVar[ballY] = ballX;
  464. dataVar[7] = score;
  465. for(int j = 0; j < sped; j++) {
  466. buttonVar[0] = digitalRead(button[0]);
  467. if(buttonVar[0] == HIGH) {
  468. if(buttonLock[0] == 0) {
  469. if(paddle < 64) {
  470. paddle = paddle * 2;
  471. buttonLock[0] = 1;
  472. }
  473. }
  474. } else {
  475. buttonLock[0] = 0;
  476. }
  477. buttonVar[1] = digitalRead(button[1]);
  478. if(buttonVar[1] == HIGH) {
  479. if(buttonLock[1] == 0) {
  480. if(paddle > 2) {
  481. paddle = paddle / 2;
  482. buttonLock[1] = 1;
  483. }
  484. }
  485. } else {
  486. buttonLock[1] = 0;
  487. }
  488. dataVar[6] = paddle + paddle / 2 + paddle * 2;
  489. if(ballY == 6) {
  490. if(ballX == paddle || ballX == paddle / 2 || ballX == paddle * 2) {
  491. } else {
  492. dataVar[6] = dataVar[6] + ballX;
  493. }
  494. }
  495. counter = 0;
  496. for(int a = 0; a < 8; a++) {
  497. display();
  498. }
  499. }
  500. }
  501. score = score + 1;
  502. sped = sped - 10;
  503. }
  504. Death();
  505. }
  506.  
  507. void Snake() {
  508. score = 1;
  509. eat = 1;
  510. test = 1;
  511. headX = 4;
  512. headY = 1;
  513. headDir = 2;
  514. tailX = 2;
  515. tailY = 1;
  516. tailDir = 0;
  517. FoodPos();
  518. for(int j = 0; j < 8; j++) {
  519. dataVar[j] = 0;
  520. snakeMap0[j] = 0;
  521. snakeMap1[j] = 0;
  522. snakeMap2[j] = 0;
  523. snakeMap3[j] = 0;
  524. snakeMap4[j] = 0;
  525. snakeMap5[j] = 0;
  526. snakeMap6[j] = 0;
  527. snakeMap7[j] = 0;
  528. }
  529. for(death = 0; death == 0;) {
  530. storageVar = log(headX) / log(2);
  531. if(storageVar == foodPos[0] && headY == foodPos[1]) {
  532. FoodPos();
  533. eat = 1;
  534. score = score + 1;
  535. }
  536. storageVar = log(tailX) / log(2);
  537. if(eat < 1) {
  538. if(tailDir == 1) {
  539. tailY = tailY - 1;
  540. }
  541. if(tailDir == 2) {
  542. tailX = tailX / 2;
  543. }
  544. if(tailDir == 3) {
  545. tailY = tailY + 1;
  546. }
  547. if(tailDir == 4) {
  548. tailX = tailX * 2;
  549. }
  550. dataVar[tailY] = dataVar[tailY] - tailX;
  551. }
  552. eat = eat - 1;
  553. SnakeMap(headX, headY, headDir);
  554. SnakeMap(tailX, tailY, 5);
  555. if(!snakeMap == 0) {
  556. tailDir = snakeMap;
  557. }
  558. SnakeMap(tailX, tailY, 0);
  559. if(headDir == 1) {
  560. headY = headY - 1;
  561. }
  562. if(headDir == 2) {
  563. headX = headX / 2;
  564. }
  565. if(headDir == 3) {
  566. headY = headY + 1;
  567. }
  568. if(headDir == 4) {
  569. headX = headX * 2;
  570. }
  571. storageVar = log(headX) / log(2);
  572. if(bitRead(dataVar[headY], storageVar) == 1 || headX == 256 || headY == -1 || headX == 256 || headY == 8) {
  573. death = 1;
  574. } else {
  575. dataVar[headY] = dataVar[headY] + headX;
  576. }
  577. for(int j = 0; j < 5; j++) {
  578. for(int k = 0; k < 75; k++) {
  579. buttonVar[0] = digitalRead(button[0]);
  580. if(buttonVar[0] == HIGH) {
  581. if(buttonLock[0] == 0) {
  582. if(headDir == 1) {
  583. headDir = 4;
  584. } else {
  585. headDir = headDir - 1;
  586. }
  587. buttonLock[0] = 1;
  588. }
  589. } else {
  590. buttonLock[0] = 0;
  591. }
  592. buttonVar[1] = digitalRead(button[1]);
  593. if(buttonVar[1] == HIGH) {
  594. if(buttonLock[1] == 0) {
  595. if(headDir == 4) {
  596. headDir = 1;
  597. } else {
  598. headDir = headDir + 1;
  599. }
  600. buttonLock[1] = 1;
  601. }
  602. } else {
  603. buttonLock[1] = 0;
  604. }
  605. counter = 0;
  606. for(int a = 0; a < 8; a++) {
  607. display();
  608. }
  609. }
  610. if(foodBlink == 1){
  611. foodBlink = 0;
  612. } else {
  613. foodBlink = 1;
  614. }
  615. }
  616. }
  617. test = 0;
  618. dataVar[7] = 0;
  619. Death();
  620. }
  621. void SnakeMap(int x, int y, int m) {
  622. storageVar = log(x) / log(2);
  623. if(m == 5) {
  624. if(y == 0) {
  625. snakeMap = snakeMap0[storageVar];
  626. }
  627. if(y == 1) {
  628. snakeMap = snakeMap1[storageVar];
  629. }
  630. if(y == 2) {
  631. snakeMap = snakeMap2[storageVar];
  632. }
  633. if(y == 3) {
  634. snakeMap = snakeMap3[storageVar];
  635. }
  636. if(y == 4) {
  637. snakeMap = snakeMap4[storageVar];
  638. }
  639. if(y == 5) {
  640. snakeMap = snakeMap5[storageVar];
  641. }
  642. if(y == 6) {
  643. snakeMap = snakeMap6[storageVar];
  644. }
  645. if(y == 7) {
  646. snakeMap = snakeMap7[storageVar];
  647. }
  648. } else {
  649. if(y == 0) {
  650. snakeMap0[storageVar] = m;
  651. }
  652. if(y == 1) {
  653. snakeMap1[storageVar] = m;
  654. }
  655. if(y == 2) {
  656. snakeMap2[storageVar] = m;
  657. }
  658. if(y == 3) {
  659. snakeMap3[storageVar] = m;
  660. }
  661. if(y == 4) {
  662. snakeMap4[storageVar] = m;
  663. }
  664. if(y == 5) {
  665. snakeMap5[storageVar] = m;
  666. }
  667. if(y == 6) {
  668. snakeMap6[storageVar] = m;
  669. }
  670. if(y == 7) {
  671. snakeMap7[storageVar] = m;
  672. }
  673. }
  674. }
  675.  
  676. void FoodPos() {
  677. for(int b = 0; b == 0; b+= 0) {
  678. foodPos[0] = random(0,7);
  679. foodPos[1] = random(0,7);
  680. if(!bitRead(dataVar[foodPos[1]], foodPos[0]) == 1) {
  681. b = 1;
  682. }
  683. }
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement