Advertisement
Apparcane

TetrisMain

May 17th, 2025
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.96 KB | Software | 0 0
  1. #include <GameBoyFixVal.h>
  2.  
  3. extern byte I_Block_0[4][4];
  4. extern byte I_Block_90[4][4];
  5. extern byte I_Block_180[4][4];
  6. extern byte I_Block_270[4][4];
  7. extern byte L_Block_0[4][4];
  8. extern byte L_Block_90[4][4];
  9. extern byte L_Block_180[4][4];
  10. extern byte L_Block_270[4][4];
  11. extern byte J_Block_0[4][4];
  12. extern byte J_Block_90[4][4];
  13. extern byte J_Block_180[4][4];
  14. extern byte J_Block_270[4][4];
  15. extern byte Z_Block_0[4][4];
  16. extern byte Z_Block_90[4][4];
  17. extern byte Z_Block_180[4][4];
  18. extern byte Z_Block_270[4][4];
  19. extern byte S_Block_0[4][4];
  20. extern byte S_Block_90[4][4];
  21. extern byte S_Block_180[4][4];
  22. extern byte S_Block_270[4][4];
  23. extern byte T_Block_0[4][4];
  24. extern byte T_Block_90[4][4];
  25. extern byte T_Block_180[4][4];
  26. extern byte T_Block_270[4][4];
  27. extern byte O_Block_0[4][4];
  28. extern byte O_Block_90[4][4];
  29. extern byte O_Block_180[4][4];
  30. extern byte O_Block_270[4][4];
  31.  
  32. GameBoy gb;
  33. int x = 2, y = -1;
  34. int rot = 0;
  35.  
  36. int acc = 1;
  37. int speed = 200;
  38. int score = 0;
  39. int level = 0;
  40.  
  41. void setup() {
  42.   gb.begin(8);
  43.   randomSeed(analogRead(A5));
  44.   createBlock(random(0, 7));
  45. }
  46.  
  47. void drawBlock(byte arr[4][4], int x, int y) {
  48.   for(int i = 0; i < 4; i++) {
  49.     for(int j = 0; j < 4; j++) {
  50.       if(arr[i][j] == 1) {
  51.         gb.drawPoint(x + i, y + j);
  52.       }
  53.     }
  54.   }
  55. }
  56.  
  57. void createBlock(int num) {
  58.   x = 2;
  59.   y = -1;
  60.   rot = random(0, 4);
  61.   if(num == 0) gb.generateBlock(gb.block, I_Block_0, I_Block_90, I_Block_180, I_Block_270);
  62.   if(num == 1) gb.generateBlock(gb.block, Z_Block_0, Z_Block_90, Z_Block_180, Z_Block_270);
  63.   if(num == 2) gb.generateBlock(gb.block, S_Block_0, S_Block_90, S_Block_180, S_Block_270);
  64.   if(num == 3) gb.generateBlock(gb.block, L_Block_0, L_Block_90, L_Block_180, L_Block_270);
  65.   if(num == 4) gb.generateBlock(gb.block, J_Block_0, J_Block_90, J_Block_180, J_Block_270);
  66.   if(num == 5) gb.generateBlock(gb.block, T_Block_0, T_Block_90, T_Block_180, T_Block_270);
  67.   if(num == 6) gb.generateBlock(gb.block, O_Block_0, O_Block_90, O_Block_180, O_Block_270);
  68. }
  69.  
  70. void makeMove(){
  71.   if(gb.getKey() == 4) {
  72.     if(!gb.checkBlockCollision(gb.block[rot], x - 1, y)) {
  73.       x--;
  74.     }
  75.   }
  76.   if(gb.getKey() == 5) {
  77.     if(!gb.checkBlockCollision(gb.block[rot], x + 1, y)) {
  78.       x++;
  79.     }
  80.   }
  81.   if(gb.getKey() == 1) {
  82.     if(!gb.checkBlockCollision(gb.block[rot + 1], x + 1, y)) {
  83.       if(rot == 3) {
  84.         rot = 0;
  85.       }
  86.       else {
  87.         rot++;
  88.       }
  89.     }
  90.   }
  91.   if(gb.getKey() == 6) {
  92.     acc = 4;
  93.   }
  94.   else {
  95.     acc = 1;
  96.   }
  97. }
  98.  
  99. void loop() {
  100.   makeMove();
  101.   if(gb.checkBlockCollision(gb.block[rot], x, y + 1)) {
  102.     gb.memBlock(gb.block[rot], x, y);
  103.     int lines = gb.fullLine();
  104.     if(lines != 0) {
  105.       score += lines;
  106.       level += lines;
  107.     }
  108.     if(level >= 4) {
  109.       gb.sound(SCORE);
  110.       acc += 1;
  111.       level = 0;    
  112.     }
  113.     createBlock(random(0, 7));
  114.   }
  115.   else {
  116.     y++;
  117.   }
  118.   gb.drawDisplay();
  119.   drawBlock(gb.block[rot], x, y);
  120.   delay(speed / acc);
  121. }
  122.  
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement