nitrodog96

tetris code

Dec 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. int leftOneLED = 8;
  4. int leftTwoLED = 9;
  5. int leftThreeLED = 2;
  6. int leftFourLED = 3;
  7. int rightOneLED = 4;
  8. int rightTwoLED = 5;
  9. int rightThreeLED = 6;
  10. int rightFourLED = 7;
  11.  
  12. int row01 = 52;
  13. int row02 = 42;
  14. int row03 = 53;
  15. int row04 = 46;
  16. int row05 = 39;
  17. int row06 = 51;
  18. int row07 = 41;
  19. int row08 = 47;
  20. int row09 = 36;
  21. int row10 = 26;
  22. int row11 = 37;
  23. int row12 = 30;
  24. int row13 = 23;
  25. int row14 = 35;
  26. int row15 = 25;
  27. int row16 = 31;
  28.  
  29. int colBot01 = 44;
  30. int colBot02 = 43;
  31. int colBot03 = 45;
  32. int colBot04 = 50;
  33. int colBot05 = 49;
  34. int colBot06 = 48;
  35. int colBot07 = 40;
  36. int colBot08 = 38;
  37. int colTop01 = 28;
  38. int colTop02 = 27;
  39. int colTop03 = 29;
  40. int colTop04 = 34;
  41. int colTop05 = 33;
  42. int colTop06 = 32;
  43. int colTop07 = 24;
  44. int colTop08 = 22;
  45.  
  46. bool pieceI[8] = {true, true, true, true, false, false, false, false};
  47. bool pieceJ[8] = {false, false, false, true, false, true, true, true};
  48. bool pieceL[8] = {false, true, true, true, false, false, false, true};
  49. bool pieceO[8] = {false, false, true, true, false, false, true, true};
  50. bool pieceS[8] = {false, true, true, false, false, false, true, true};
  51. bool pieceT[8] = {false, true, true, true, false, false, true, false};
  52. bool pieceZ[8] = {false, false, true, true, false, true, true, false};
  53.  
  54. const byte rowsBot[] = {row01,row02,row03,row04,row05,row06,row07,row08};
  55. const byte rowsTop[] = {row09,row10,row11,row12,row13,row14,row15,row16};
  56. const byte colsBot[] = {colBot08,colBot07,colBot06,colBot05,colBot04,colBot03,colBot02,colBot01};
  57. const byte colsTop[] = {colTop08,colTop07,colTop06,colTop05,colTop04,colTop03,colTop02,colTop01};
  58.  
  59. byte bytes[256] = {B11111111,B11111110,B11111101,B11111100,B11111011,B11111010,B11111001,B11111000,B11110111,B11110110,B11110101,B11110100,B11110011,B11110010,B11110001,B11110000,
  60. B11101111,B11101110,B11101101,B11101100,B11101011,B11101010,B11101001,B11101000,B11100111,B11100110,B11100101,B11100100,B11100011,B11100010,B11100001,B11100000,
  61. B11011111,B11011110,B11011101,B11011100,B11011011,B11011010,B11011001,B11011000,B11010111,B11010110,B11010101,B11010100,B11010011,B11010010,B11010001,B11010000,
  62. B11001111,B11001110,B11001101,B11001100,B11001011,B11001010,B11001001,B11001000,B11000111,B11000110,B11000101,B11000100,B11000011,B11000010,B11000001,B11000000,
  63. B10111111,B10111110,B10111101,B10111100,B10111011,B10111010,B10111001,B10111000,B10110111,B10110110,B10110101,B10110100,B10110011,B10110010,B10110001,B10110000,
  64. B10101111,B10101110,B10101101,B10101100,B10101011,B10101010,B10101001,B10101000,B10100111,B10100110,B10100101,B10100100,B10100011,B10100010,B10100001,B10100000,
  65. B10011111,B10011110,B10011101,B10011100,B10011011,B10011010,B10011001,B10011000,B10010111,B10010110,B10010101,B10010100,B10010011,B10010010,B10010001,B10010000,
  66. B10001111,B10001110,B10001101,B10001100,B10001011,B10001010,B10001001,B10001000,B10000111,B10000110,B10000101,B10000100,B10000011,B10000010,B10000001,B10000000,
  67. B01111111,B01111110,B01111101,B01111100,B01111011,B01111010,B01111001,B01111000,B01110111,B01110110,B01110101,B01110100,B01110011,B01110010,B01110001,B01110000,
  68. B01101111,B01101110,B01101101,B01101100,B01101011,B01101010,B01101001,B01101000,B01100111,B01100110,B01100101,B01100100,B01100011,B01100010,B01100001,B01100000,
  69. B01011111,B01011110,B01011101,B01011100,B01011011,B01011010,B01011001,B01011000,B01010111,B01010110,B01010101,B01010100,B01010011,B01010010,B01010001,B01010000,
  70. B01001111,B01001110,B01001101,B01001100,B01001011,B01001010,B01001001,B01001000,B01000111,B01000110,B01000101,B01000100,B01000011,B01000010,B01000001,B01000000,
  71. B00111111,B00111110,B00111101,B00111100,B00111011,B00111010,B00111001,B00111000,B00110111,B00110110,B00110101,B00110100,B00110011,B00110010,B00110001,B00110000,
  72. B00101111,B00101110,B00101101,B00101100,B00101011,B00101010,B00101001,B00101000,B00100111,B00100110,B00100101,B00100100,B00100011,B00100010,B00100001,B00100000,
  73. B00011111,B00011110,B00011101,B00011100,B00011011,B00011010,B00011001,B00011000,B00010111,B00010110,B00010101,B00010100,B00010011,B00010010,B00010001,B00010000,
  74. B00001111,B00001110,B00001101,B00001100,B00001011,B00001010,B00001001,B00001000,B00000111,B00000110,B00000101,B00000100,B00000011,B00000010,B00000001,B00000000
  75. };
  76.  
  77. byte topDisplay[8] = {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111};
  78. byte botDisplay[8] = {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111};
  79.  
  80. int topDisplayInts[8] = {0,0,0,0,0,0,0,0};
  81. int botDisplayInts[8] = {0,0,0,0,0,0,0,0};
  82.  
  83. bool topColsMatrix[8][8] = {
  84. {false, false, false, false, false, false, false, false},
  85. {false, false, false, false, false, false, false, false},
  86. {false, false, false, false, false, false, false, false},
  87. {false, false, false, false, false, false, false, false},
  88. {false, false, false, false, false, false, false, false},
  89. {false, false, false, false, false, false, false, false},
  90. {false, false, false, false, false, false, false, false},
  91. {false, false, false, false, false, false, false, false}
  92. };
  93.  
  94. bool botColsMatrix[8][8] = {
  95. {false, false, false, false, false, false, false, false},
  96. {false, false, false, false, false, false, false, false},
  97. {false, false, false, false, false, false, false, false},
  98. {false, false, false, false, false, false, false, false},
  99. {false, false, false, false, false, false, false, false},
  100. {false, false, false, false, false, false, false, false},
  101. {false, false, false, false, false, false, false, false},
  102. {false, false, false, false, false, false, false, false}
  103. };
  104.  
  105. String nextPiece = "";
  106.  
  107. void setup() {
  108. Serial.begin(9600);
  109. randomSeed(analogRead(A0));
  110.  
  111. // put your setup code here, to run once:
  112. int i;
  113. int a;
  114. for(i=2;i<10;i++)
  115. {
  116. pinMode(i, OUTPUT);
  117. }
  118. for(a=22;a<54;a++)
  119. {
  120. pinMode(a, OUTPUT);
  121. }
  122.  
  123. pinMode(A8, INPUT);
  124.  
  125. nextPiece = generateNextPiece();
  126. int failed = spawnPiece();
  127. }
  128.  
  129. void loop() {
  130. // put your main code here, to run repeatedly:
  131. //drawScreen(topDisplay, false);
  132. //nextPiece = generateNextPiece();
  133. //displayNextPiece(nextPiece);
  134. int b;
  135. for(b=0;b<8;b++)
  136. {
  137. topDisplay[b] = bytes[topDisplayInts[b]];
  138. botDisplay[b] = bytes[botDisplayInts[b]];
  139. }
  140. drawScreen(topDisplay, false);
  141. drawScreen(botDisplay, true);
  142.  
  143. displayNextPiece(nextPiece);
  144.  
  145. //Serial.println(analogRead(A8));
  146.  
  147.  
  148. /*if(failed == 1)
  149. {
  150. endGame();
  151. }*/
  152.  
  153. //delay(1000);
  154. }
  155.  
  156. void updateMatrices()
  157. {
  158. int i;
  159. int j;
  160. for(i=0;i<8;i++)
  161. {
  162. for(j=0;j<8;j++)
  163. {
  164. if(topColsMatrix[i][j])
  165. {
  166. topDisplayInts[i] += 128/ pow(2, j);
  167. }
  168. if(botColsMatrix[i][j])
  169. {
  170. botDisplayInts[i] += pow(2, j);
  171. }
  172. }
  173. topDisplay[i] = bytes[topDisplayInts[i]];
  174. botDisplay[i] = bytes[botDisplayInts[i]];
  175. }
  176. }
  177.  
  178. int spawnPiece()
  179. {
  180. int returnOut = 0;
  181.  
  182. Serial.println("Spawning " + nextPiece + " piece.");
  183. if(nextPiece == "I")
  184. {
  185. if(topColsMatrix[2][0] || topColsMatrix[3][0] || topColsMatrix[4][0] || topColsMatrix[5][0])
  186. {
  187. returnOut = 1;
  188. }
  189. topColsMatrix[2][0] = true;
  190. topColsMatrix[3][0] = true;
  191. topColsMatrix[4][0] = true;
  192. topColsMatrix[5][0] = true;
  193. }
  194. if(nextPiece == "J")
  195. {
  196. if(topColsMatrix[2][0] || topColsMatrix[2][1] || topColsMatrix[3][0] || topColsMatrix[4][0])
  197. {
  198. returnOut = 1;
  199. }
  200. topColsMatrix[2][0] = true;
  201. topColsMatrix[2][1] = true;
  202. topColsMatrix[3][0] = true;
  203. topColsMatrix[4][0] = true;
  204. }
  205. if(nextPiece == "L")
  206. {
  207. if(topColsMatrix[2][0] || topColsMatrix[3][0] || topColsMatrix[4][0] || topColsMatrix[4][1])
  208. {
  209. returnOut = 1;
  210. }
  211. topColsMatrix[2][0] = true;
  212. topColsMatrix[3][0] = true;
  213. topColsMatrix[4][0] = true;
  214. topColsMatrix[4][1] = true;
  215. }
  216. if(nextPiece == "O")
  217. {
  218. if(topColsMatrix[3][0] || topColsMatrix[3][1] || topColsMatrix[4][0] || topColsMatrix[4][1])
  219. {
  220. returnOut = 1;
  221. }
  222. topColsMatrix[3][0] = true;
  223. topColsMatrix[3][1] = true;
  224. topColsMatrix[4][0] = true;
  225. topColsMatrix[4][1] = true;
  226. }
  227. if(nextPiece == "S")
  228. {
  229. if(topColsMatrix[2][0] || topColsMatrix[3][0] || topColsMatrix[3][1]| topColsMatrix[4][1])
  230. {
  231. returnOut = 1;
  232. }
  233. topColsMatrix[2][0] = true;
  234. topColsMatrix[3][0] = true;
  235. topColsMatrix[3][1] = true;
  236. topColsMatrix[4][1] = true;
  237. }
  238. if(nextPiece == "T")
  239. {
  240. if(topColsMatrix[2][0] || topColsMatrix[3][0] || topColsMatrix[3][1] || topColsMatrix[4][0])
  241. {
  242. returnOut = 1;
  243. }
  244. topColsMatrix[2][0] = true;
  245. topColsMatrix[3][0] = true;
  246. topColsMatrix[3][1] = true;
  247. topColsMatrix[4][0] = true;
  248. }
  249. if(nextPiece == "Z")
  250. {
  251. if(topColsMatrix[2][1] || topColsMatrix[3][0] || topColsMatrix[3][1] || topColsMatrix[4][0])
  252. {
  253. returnOut = 1;
  254. }
  255. topColsMatrix[2][1] = true;
  256. topColsMatrix[3][0] = true;
  257. topColsMatrix[3][1] = true;
  258. topColsMatrix[4][0] = true;
  259. }
  260.  
  261. updateMatrices();
  262.  
  263. nextPiece = generateNextPiece();
  264.  
  265. return returnOut;
  266. }
  267.  
  268. void displayTestPiece()
  269. {
  270. int i;
  271. for(int i = 2; i < 10; i++)
  272. {
  273. digitalWrite(i, HIGH);
  274. }
  275. }
  276.  
  277. void drawScreen(byte buffer2[], bool bottom) // FIGURE OUT PINS FOR MATRICES
  278. {
  279. if(bottom)
  280. {
  281. // Turn on each row in series
  282. for (byte i = 0; i < 8; i++) // count next row
  283. {
  284. digitalWrite(rowsBot[i], HIGH); //initiate whole row
  285. for (byte a = 0; a < 8; a++) // count next row
  286. {
  287. // if You set (~buffer2[i] >> a) then You will have positive
  288. digitalWrite(colsBot[a], (buffer2[i] >> a) & 0x01); // initiate whole column
  289.  
  290. delayMicroseconds(100); // uncoment deley for diferent speed of display
  291. //delayMicroseconds(1000);
  292. //delay(10);
  293. //delay(100);
  294.  
  295. digitalWrite(colsBot[a], 1); // reset whole column
  296. }
  297. digitalWrite(rowsBot[i], LOW); // reset whole row
  298. // otherwise last row will intersect with next row
  299. }
  300. }
  301. else
  302. {
  303. // Turn on each row in series
  304. for (byte i = 0; i < 8; i++) // count next row
  305. {
  306. digitalWrite(rowsTop[i], HIGH); //initiate whole row
  307. for (byte a = 0; a < 8; a++) // count next row
  308. {
  309. // if You set (~buffer2[i] >> a) then You will have positive
  310. digitalWrite(colsTop[a], (buffer2[i] >> a) & 0x01); // initiate whole column
  311.  
  312. delayMicroseconds(100); // uncoment deley for diferent speed of display
  313. //delayMicroseconds(1000);
  314. //delay(10);
  315. //delay(100);
  316.  
  317. digitalWrite(colsTop[a], 1); // reset whole column
  318. }
  319. digitalWrite(rowsTop[i], LOW); // reset whole row
  320. // otherwise last row will intersect with next row
  321. }
  322. }
  323. }
  324.  
  325. String generateNextPiece()
  326. {
  327. String pieces[7] = {"I", "J", "L", "O", "S", "T", "Z"};
  328. long randNumber = random(0,714) / 102;
  329. int randIndex = (int) floor(randNumber);
  330. String nP = pieces[randIndex];
  331. return nP;
  332. }
  333.  
  334. void displayNextPiece(String piece)
  335. {
  336. int i;
  337.  
  338. if(piece == "I")
  339. {
  340. for(i=0;i<8;i++)
  341. {
  342. if(pieceI[i])
  343. {
  344. if(i == 0 || i == 1)
  345. {
  346. digitalWrite(i + 8, HIGH);
  347. }
  348. else
  349. {
  350. digitalWrite(i, HIGH);
  351. }
  352. }
  353. else
  354. {
  355. if(i == 0 || i == 1)
  356. {
  357. digitalWrite(i + 8, LOW);
  358. }
  359. else
  360. {
  361. digitalWrite(i, LOW);
  362. }
  363. }
  364. }
  365. }
  366. if(piece == "J")
  367. {
  368. for(i=0;i<8;i++)
  369. {
  370. if(pieceJ[i])
  371. {
  372. if(i == 0 || i == 1)
  373. {
  374. digitalWrite(i + 8, HIGH);
  375. }
  376. else
  377. {
  378. digitalWrite(i, HIGH);
  379. }
  380. }
  381. else
  382. {
  383. if(i == 0 || i == 1)
  384. {
  385. digitalWrite(i + 8, LOW);
  386. }
  387. else
  388. {
  389. digitalWrite(i, LOW);
  390. }
  391. }
  392. }
  393. }
  394. if(piece == "L")
  395. {
  396. for(i=0;i<8;i++)
  397. {
  398. if(pieceL[i])
  399. {
  400. if(i == 0 || i == 1)
  401. {
  402. digitalWrite(i + 8, HIGH);
  403. }
  404. else
  405. {
  406. digitalWrite(i, HIGH);
  407. }
  408. }
  409. else
  410. {
  411. if(i == 0 || i == 1)
  412. {
  413. digitalWrite(i + 8, LOW);
  414. }
  415. else
  416. {
  417. digitalWrite(i, LOW);
  418. }
  419. }
  420. }
  421. }
  422. if(piece == "O")
  423. {
  424. for(i=0;i<8;i++)
  425. {
  426. if(pieceO[i])
  427. {
  428. if(i == 0 || i == 1)
  429. {
  430. digitalWrite(i + 8, HIGH);
  431. }
  432. else
  433. {
  434. digitalWrite(i, HIGH);
  435. }
  436. }
  437. else
  438. {
  439. if(i == 0 || i == 1)
  440. {
  441. digitalWrite(i + 8, LOW);
  442. }
  443. else
  444. {
  445. digitalWrite(i, LOW);
  446. }
  447. }
  448. }
  449. }
  450. if(piece == "S")
  451. {
  452. for(i=0;i<8;i++)
  453. {
  454. if(pieceS[i])
  455. {
  456. if(i == 0 || i == 1)
  457. {
  458. digitalWrite(i + 8, HIGH);
  459. }
  460. else
  461. {
  462. digitalWrite(i, HIGH);
  463. }
  464. }
  465. else
  466. {
  467. if(i == 0 || i == 1)
  468. {
  469. digitalWrite(i + 8, LOW);
  470. }
  471. else
  472. {
  473. digitalWrite(i, LOW);
  474. }
  475. }
  476. }
  477. }
  478. if(piece == "T")
  479. {
  480. for(i=0;i<8;i++)
  481. {
  482. if(pieceT[i])
  483. {
  484. if(i == 0 || i == 1)
  485. {
  486. digitalWrite(i + 8, HIGH);
  487. }
  488. else
  489. {
  490. digitalWrite(i, HIGH);
  491. }
  492. }
  493. else
  494. {
  495. if(i == 0 || i == 1)
  496. {
  497. digitalWrite(i + 8, LOW);
  498. }
  499. else
  500. {
  501. digitalWrite(i, LOW);
  502. }
  503. }
  504. }
  505. }
  506. if(piece == "Z")
  507. {
  508. for(i=0;i<8;i++)
  509. {
  510. if(pieceZ[i])
  511. {
  512. if(i == 0 || i == 1)
  513. {
  514. digitalWrite(i + 8, HIGH);
  515. }
  516. else
  517. {
  518. digitalWrite(i, HIGH);
  519. }
  520. }
  521. else
  522. {
  523. if(i == 0 || i == 1)
  524. {
  525. digitalWrite(i + 8, LOW);
  526. }
  527. else
  528. {
  529. digitalWrite(i, LOW);
  530. }
  531. }
  532. }
  533. }
  534. }
Advertisement
Add Comment
Please, Sign In to add comment