Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.82 KB | None | 0 0
  1. /*******************************************************************
  2. *  ESP32 Tetris - Version 1.0 - April the 12th 2019                *
  3. *                                                                  *
  4. *  ESP32 Tetris is a Tetris clone for VGA monitors, written        *
  5. *  by Roberto Melzi, and based on the ESP32 VGA Library, written   *
  6. *  by bitluni.                                                     *
  7. *  For more datails see here:                                      *
  8. *                                                                  *
  9. *  https://www.arduinolibraries.info/authors/bitluni               *                                                                
  10. *  https://www.youtube.com/user/bitlunislab/videos                 *
  11. *                                                                  *
  12. *  See also my other projects on Instructables:                    *
  13. *                                                                  *
  14. *  http://www.instructables.com/member/Rob%20Cai/instructables/    *
  15. *                                                                  *
  16. *******************************************************************/
  17.  
  18. #include <ESP32Lib.h>
  19. #include <Ressources/Font6x8.h>
  20. #include <Ressources/Font8x8.h>
  21.  
  22. /*
  23. //----- NB: the game works just with the pins 22, 21 and 19 for RGB --------------------
  24. const int redPins[] = {22, 4, 12, 13, 14};
  25. const int greenPins[] = {21, 16, 17, 32, 15};
  26. const int bluePins[] = {19, 2, 23, 27};
  27. const int hsyncPin = 18;
  28. const int vsyncPin = 5;
  29. */
  30.  
  31. //------- original bitluni pin definition ----------------------------------------------
  32. const int redPins[] = {2, 4, 12, 13, 14};
  33. const int greenPins[] = {15, 16, 17, 18, 19};
  34. const int bluePins[] = {21, 22, 23, 27};
  35. const int hsyncPin = 32;
  36. const int vsyncPin = 33;
  37.  
  38. /*-------------------- button pin definitions -----------------------
  39. byte button1 = 26; //right
  40. byte button2 = 25; //rotate
  41. byte button3 = 34; //left
  42. byte button4 = 35; //down fast
  43. */
  44.  
  45. boolean button;
  46. boolean button_1;
  47. boolean button_2;
  48. boolean button_3;
  49. boolean button_4;
  50. boolean button_5;
  51. boolean button_1_old;
  52. boolean button_2_old;
  53. boolean button_3_old;
  54. boolean button_4_old;
  55. boolean button_5_old;
  56. int block[4][2]={{0,0},{0,0},{0,0},{0,0}};
  57. int blockExt[4][2]={{0,0},{0,0},{0,0},{0,0}};
  58. int blockOld[4][2]={{0,0},{0,0},{0,0},{0,0}};
  59. int blockTmp[4][2]={{0,0},{0,0},{0,0},{0,0}};
  60. int blockTr[4][2]={{0,0},{0,0},{0,0},{0,0}};
  61. int yLine[4] = {0,0,0,0};
  62. int yLineTmp[4] = {0,0,0,0};
  63. int yCounter = 0;
  64. int x = 60;
  65. int y = 6;
  66. int z = 10;
  67. int score, scoreNew;
  68. int noLoop = -1;
  69. int rotationDirection = 1;
  70. int delta = 0;  
  71. int color = 1;
  72. int colorOld;
  73. int blockN, blockNext;
  74. int busy;
  75. int noDelete = 0;
  76. int k = 0;
  77. int a = 40;
  78. int b = 10;
  79. int counterMenu = 0;
  80. unsigned long myTime = 0;
  81. int fast = 14; //14;
  82. int resX = 320;
  83. int resY = 200;
  84. int colA, colB, colC;
  85. int squareX = 7;
  86. int squareY = 6;
  87. int myColumns = 11;
  88. int myRaws = 26;
  89. int level = 1;
  90.  
  91. //VGA Device
  92. VG34Bit vga;
  93.  
  94. //initial setup
  95. void setup()
  96. {
  97.     //we need double buffering for smooth animations
  98.     //vga.setFrameBufferCount(2);
  99.   vga.setFrameBufferCount(2);
  100.     //initializing i2s vga (with only one framebuffer)
  101.   vga.init(vga.MODE320x200, 14, 27, 16, 17, 25);
  102.     //setting the font
  103.     vga.setFont(Font6x8);
  104.   randomSeed(analogRead(5));
  105. /*   pinMode(button1,INPUT);
  106.   pinMode(button2,INPUT);
  107.   pinMode(button3,INPUT);
  108.   pinMode(button4,INPUT); */
  109. }
  110.  
  111. void processInputs() {
  112.    button_1_old = button_1;
  113.    button_2_old = button_2;
  114.    button_3_old = button_3;
  115.    button_4_old = button_4;
  116.    if (button_1 == 0 ){
  117.       if (button_1_old == 0)
  118.          {//ajout keypad
  119.   if (( ( ( analogRead(34) ) < ( 3200 ) ) && ( ( analogRead(34) ) > ( 3000 ) ) ))
  120.   {
  121.     button_1 = HIGH ;
  122.   }
  123.   else
  124.   {
  125.     button_1 = LOW ;
  126.   }
  127.          }
  128.       else
  129.          {button_1_old = 0;}
  130.    }
  131.    else
  132.    {button_1 = 0;}
  133.    if (button_2 == 0 ){
  134.       if (button_2_old == 0)
  135.          {//ajout keypad
  136.   if (( ( ( analogRead(34) ) < ( 900 ) ) && ( ( analogRead(34) ) > ( 500 ) ) ))
  137.   {
  138.     button_2 = HIGH ;
  139.   }
  140.   else
  141.   {
  142.     button_2 = LOW ;
  143.   }
  144.              delay(50);}
  145.       else
  146.          {button_2_old = 0;}
  147.    }
  148.    else
  149.    {button_2 = 0;}
  150.    if (button_3 == 0 ){
  151.       if (button_3_old == 0)
  152.          {//ajout keypad
  153.   if (( ( analogRead(34) ) < ( 60 ) ))
  154.   {
  155.     button_3 = HIGH ;
  156.   }
  157.   else
  158.   {
  159.     button_3 = LOW ;
  160.   }
  161.          }
  162.       else
  163.          {button_3_old = 0;}
  164.    }
  165.    else
  166.    {button_3 = 0;}
  167.    /*if (button_4 == 0 ){
  168.       if (button_4_old == 0)
  169.          {button_4 = digitalRead(button4);}
  170.       else
  171.          {button_4_old = 0;}
  172.    }
  173.    else
  174.    {button_4 = 0;}
  175.    */
  176.    /*
  177.    if (button_4 == 0 ){digitalRead(button4);}
  178.    else {button_4 = 0;}
  179.    */
  180.    //ajout keypad
  181.      if (( ( ( analogRead(34) ) < ( 2100 ) ) && ( ( analogRead(34) ) > ( 1800 ) ) ))
  182.   {
  183.     button_4 = HIGH ;
  184.   }
  185.   else
  186.   {
  187.     button_4 = LOW ;
  188.   }
  189.    button = button_2 || button_4;
  190. }
  191.  
  192.  
  193. void clearInputs() {
  194.   button_1 = 0;
  195.   button_2 = 0;
  196.   button_3 = 0;
  197.   button_4 = 0;
  198.   button = 0;
  199. }
  200.  
  201. void drawMenu() {
  202.   while (button_1 == 0 && button_2 == 0 && button_3 == 0 && button_4 == 0) {
  203.      processInputs();
  204.      smoothRect(76, 52, 170, 80, 10, 7);
  205.      vga.setFont(Font8x8);
  206.      setPrint(110, 60, 4);
  207.      vga.print("ESP32 Tetris");
  208.      setPrint(95, 80, 5);
  209.      vga.print("by Roberto Melzi");
  210.      vga.setFont(Font6x8);
  211.      setPrint(93, 100, 2);
  212.      vga.print("with ESP32 VGA library");
  213.      setPrint(124, 118, 6);
  214.      vga.print("by bitluni");
  215.      delay(100);
  216.      counterMenu++;
  217.   }
  218.   vga.clear(0);
  219.   drawGameScreen();
  220.   drawScore(score);
  221. }
  222.  
  223. void drawScore(int i) {
  224.     if (i > 39){
  225.        level = level + 1;
  226.        score = 0;
  227.        i = 0;
  228.        if (fast > 5 ) {fast = fast - 3;}
  229.        else {fast = fast - 1;}
  230.        if (fast < 3) {fast = 2;}
  231.     }
  232.     smoothRect(206, 77, 62, 23, 8, 2);
  233.     setPrint(222, 80, 5);
  234.     vga.print("Level");
  235.     setPrint(234, 90, 4);
  236.     vga.print(level);
  237.     smoothRect(200, 107, 74, 22, 8, 3);
  238.     setPrint(222, 110, 5);
  239.     vga.print("Score");
  240.     setPrint(230, 120, 4);
  241.     vga.print(10*scoreNew);
  242. }
  243.    
  244. void drawBorder() {
  245.    myRect(120, 2, squareX*(myColumns - 1) - 2, squareY*myRaws + 23 - 2, 4);
  246.    myRect(117, 0, squareX*(myColumns - 1) + 4, squareY*myRaws + 23 + 2, 7);
  247.    setPrint(223, 16, 5);
  248.    vga.print("next");
  249.    smoothRect(210, 10, 50, 40, 10, 4);
  250. }
  251.  
  252. // --------------------- this is for the beginning game window ------------------------
  253. void drawStartScreen() {
  254.    drawBorder();
  255.    drawGameScreen();
  256.    button = 0;
  257.    delay(200);
  258. }
  259.  
  260. // ---------------------- this is the main function to draw the game screen -----------
  261. void drawGameScreen() {
  262.   drawBorder();
  263. }
  264.  
  265.  
  266. // ----------------------- Tetriminos definition --------------------------------------
  267. void blockDef(int i) {
  268.   if (i == 1){
  269.   // O
  270.   block[0][0] = 0;
  271.   block[0][1] = 0;
  272.   block[1][0] = 1;
  273.   block[1][1] = 0;
  274.   block[2][0] = 0;
  275.   block[2][1] = 1;
  276.   block[3][0] = 1;
  277.   block[3][1] = 1;
  278.   color = 1;
  279.   //colorNew = 1;
  280.   }
  281.   if (i == 2){
  282.   // L
  283.   block[0][0] = -1;
  284.   block[0][1] = 0;
  285.   block[1][0] = 0;
  286.   block[1][1] = 0;
  287.   block[2][0] = 1;
  288.   block[2][1] = 0;
  289.   block[3][0] = -1;
  290.   block[3][1] = 1;
  291.   color = 2;
  292.   //colorNew = 2;
  293.   }
  294.   if (i == 3){
  295.   // J
  296.   block[0][0] = -1;
  297.   block[0][1] = 0;
  298.   block[1][0] = 0;
  299.   block[1][1] = 0;
  300.   block[2][0] = 1;
  301.   block[2][1] = 0;
  302.   block[3][0] = 1;
  303.   block[3][1] = 1;
  304.   color = 3;
  305.   //colorNew = 3;
  306.   }
  307.   if (i == 4){
  308.   // I
  309.   block[0][0] = -1;
  310.   block[0][1] = 0;
  311.   block[1][0] = 0;
  312.   block[1][1] = 0;
  313.   block[2][0] = 1;
  314.   block[2][1] = 0;
  315.   block[3][0] = 2;
  316.   block[3][1] = 0;
  317.   color = 4;
  318.   //colorNew = 4;
  319.   }
  320.   if (i == 5){
  321.   // S
  322.   block[0][0] = -1;
  323.   block[0][1] = 0;
  324.   block[1][0] = 0;
  325.   block[1][1] = 0;
  326.   block[2][0] = 0;
  327.   block[2][1] = 1;
  328.   block[3][0] = 1;
  329.   block[3][1] = 1;
  330.   color = 5;
  331.   //colorNew = 5;
  332.   }
  333.   if (i == 6){
  334.   // Z
  335.   block[0][0] = -1;
  336.   block[0][1] = 1;
  337.   block[1][0] = 0;
  338.   block[1][1] = 1;
  339.   block[2][0] = 0;
  340.   block[2][1] = 0;
  341.   block[3][0] = 1;
  342.   block[3][1] = 0;
  343.   color = 6;
  344.   //colorNew = 6;
  345.   }
  346.   if (i == 7){
  347.   // T
  348.   block[0][0] = -1;
  349.   block[0][1] = 0;
  350.   block[1][0] = 0;
  351.   block[1][1] = 0;
  352.   block[2][0] = 0;
  353.   block[2][1] = 1;
  354.   block[3][0] = 1;
  355.   block[3][1] = 0;
  356.   color = 7;
  357.   //colorNew = 7;
  358.   }
  359. }
  360.  
  361. // -------------------------- expansion for 4:3 monitors ------------------------------
  362. void blockExtension() {
  363.    for (int i = 0; i < 4; i++){
  364.       blockExt[0][0] = block[0][0]*3;
  365.       blockExt[0][1] = block[0][1]*2;
  366.       blockExt[1][0] = block[1][0]*3;
  367.       blockExt[1][1] = block[1][1]*2;
  368.       blockExt[2][0] = block[2][0]*3;
  369.       blockExt[2][1] = block[2][1]*2;
  370.       blockExt[3][0] = block[3][0]*3;
  371.       blockExt[3][1] = block[3][1]*2;
  372.    }
  373. }
  374.  
  375. void blockRotation(int rotationDirection){
  376.   for (int i = 0; i < 4; i++){
  377.      blockOld[0][0] = block[0][0];
  378.      blockOld[0][1] = block[0][1];
  379.      blockOld[1][0] = block[1][0];
  380.      blockOld[1][1] = block[1][1];
  381.      blockOld[2][0] = block[2][0];
  382.      blockOld[2][1] = block[2][1];
  383.      blockOld[3][0] = block[3][0];
  384.      blockOld[3][1] = block[3][1];
  385.   }
  386.   for (int i = 0; i < 4; i++){
  387.      block[0][0] = blockOld[0][1]*rotationDirection;
  388.      block[0][1] = -blockOld[0][0]*rotationDirection;
  389.      block[1][0] = blockOld[1][1]*rotationDirection;
  390.      block[1][1] = -blockOld[1][0]*rotationDirection;
  391.      block[2][0] = blockOld[2][1]*rotationDirection;
  392.      block[2][1] = -blockOld[2][0]*rotationDirection;
  393.      block[3][0] = blockOld[3][1]*rotationDirection;
  394.      block[3][1] = -blockOld[3][0]*rotationDirection;
  395.   }
  396. }
  397. void blockTranslation(int x, int y) {
  398.    for (int i = 0; i < 4; i++){
  399.       blockTr[0][0] = blockExt[0][0] + x;
  400.       blockTr[0][1] = blockExt[0][1] + y;
  401.       blockTr[1][0] = blockExt[1][0] + x;
  402.       blockTr[1][1] = blockExt[1][1] + y;
  403.       blockTr[2][0] = blockExt[2][0] + x;
  404.       blockTr[2][1] = blockExt[2][1] + y;
  405.       blockTr[3][0] = blockExt[3][0] + x;
  406.       blockTr[3][1] = blockExt[3][1] + y;
  407.    }
  408. }
  409.  
  410. void delBlock(){
  411.   if (noDelete == 1) {noDelete = 0;}
  412.   else {
  413.       for (int i = 0; i < 4; i++){
  414.          mySquare(blockTr[i][0] * 2 + 31, blockTr[i][1] * 3 + 1, 0);
  415.       }
  416.    }
  417. }
  418.  
  419. void drawBlock(){
  420.   for (int i = 0; i < 4; i++){
  421.      mySquare(blockTr[i][0] * 2 + 31, blockTr[i][1] * 3 + 1, color);
  422.   }
  423.   for (int i = 0; i < 4; i++){
  424.      blockTmp[0][0] = blockTr[0][0];
  425.      blockTmp[0][1] = blockTr[0][1];
  426.      blockTmp[1][0] = blockTr[1][0];
  427.      blockTmp[1][1] = blockTr[1][1];
  428.      blockTmp[2][0] = blockTr[2][0];
  429.      blockTmp[2][1] = blockTr[2][1];
  430.      blockTmp[3][0] = blockTr[3][0];
  431.      blockTmp[3][1] = blockTr[3][1];
  432.   }
  433. }
  434.  
  435. void drawBlockTmp(){
  436.   for (int i = 0; i < 4; i++){
  437.      mySquare(blockTmp[i][0] * 2 + 31, blockTmp[i][1] * 3 + 1, color);
  438.   }
  439. }
  440.  
  441. void checkBlock(){
  442.   busy = 0;  
  443.   for (int i = 0; i < 4; i++){
  444.      busy = busy + vgaget(2*blockTr[i][0] + 31, 3*blockTr[i][1] + 1) + vgaget(2*(blockTr[i][0] + 2) + 32, 3*blockTr[i][1] + 1);
  445.   }
  446. }
  447.  
  448. void replaceBlock(){
  449.      blockExtension();
  450.      blockTranslation(x, y);
  451.      checkBlock();
  452.      if (busy == 0){
  453.         drawBlock();
  454.      }
  455.      else // ---------- else is run if the block cannot get down  -----------------
  456.      {
  457.         drawBlockTmp();
  458.         checkForFullLine(); // ---- check il the line is filled when the block cennot get down anymore ----------------------
  459.         noLoop = 0;
  460.         noDelete = 1;
  461.         if (y < 8) {
  462.            gameOver();
  463.         }
  464.      }
  465.      delay(1);
  466. }
  467.  
  468. void gameOver(){ // ------------------------------------------- Game Over ! --------------------------------------------------
  469.    noLoop = -1;
  470.    score = 0;
  471.    level = 1;
  472.    scoreNew = 0;
  473.    fast = 14;
  474.    clearInputs();
  475.    myTime = 0;
  476.    vga.setCursor(30, 100);
  477.    vga.print("Game Over!");
  478.    smoothRect(20, 91, 80, 25, 11, 1);
  479.    delay(300);
  480.    vgaTone(660, 200);
  481.    vgaTone(330, 200);
  482.    vgaTone(165, 200);
  483.    vgaTone(82, 200);
  484.    while (button_1 == 0 && button_2 == 0 && button_3 == 0 && button_4 == 0) {
  485.       processInputs();
  486.       delay(100);
  487.    }
  488.    clearInputs();  
  489.    vga.clear(0);  
  490. }
  491.  
  492. void drawBlockNext(){ // ----- draw next block on the right side --------------------------------
  493.      blockExtension();
  494.      blockTranslation(100, 10);
  495.      vga.fillRect(95*2 + 31, 8*3 + 1, 32, 7*3+1, vga.RGB(0, 0, 0));
  496.      drawBlock();
  497. }
  498.  
  499. void checkBlockTranslation(){
  500.      x = x + delta;
  501.      blockExtension();
  502.      blockTranslation(x, y);
  503.      checkBlock();
  504.      if (busy == 0){
  505.         drawBlock();
  506.      }
  507.      else
  508.      {
  509.         x = x - delta;
  510.         blockExtension();
  511.         blockTranslation(x, y);
  512.         drawBlock();
  513.      }
  514.      delay(50);
  515. }
  516.  
  517. void checkBlockRotation(){
  518.      //x = x + delta;
  519.      blockExtension();
  520.      blockTranslation(x, y);
  521.      checkBlock();
  522.      if (busy == 0){
  523.         drawBlock();
  524.      }
  525.      else
  526.      {
  527.         rotationDirection = -rotationDirection;
  528.         blockRotation(rotationDirection);
  529.         blockExtension();
  530.         blockTranslation(x, y);
  531.         drawBlock();
  532.      }
  533.      delay(50);
  534. }
  535.  
  536. void checkForFullLine() { // --------------------- check if the line is full and must be deleted --------------
  537.    for (int i = 0; i < 4; i++){
  538.       for (int j = 45; j < 76; j += 3) {
  539.          //if (vgaget(j, blockTmp[i][1]) >0){k++; }
  540.          if (vgaget(2*j + 32, 3*blockTmp[i][1] + 2) >0){k++; }
  541.       }
  542.       if (k == 11) { // ---- line is full and must be deleted ----------------------------------------------------------
  543.          vga.fillRect(45 * 2 + 31, blockTmp[i][1] * 3 + 1, squareX *11, squareY, vga.RGB(0, 0, 0));
  544.          yLineTmp[yCounter] = blockTmp[i][1];
  545.          yLine[yCounter] = blockTmp[i][1];
  546.          yCounter++;
  547.          vgaTone(660,30);
  548.       }
  549.       k = 0;
  550.     }
  551.     if (yLineTmp[yCounter - 1] < yLine[0]) { // ------------ qui va capito se va < o > (penso >) ----------------------
  552.        for (int i = 0; i < yCounter; i++) { // ------------- inversion ---------------------------------------
  553.           yLine[i] = yLineTmp[yCounter - i - 1];
  554.        }
  555.     }
  556.     for (int i = 0; i < yCounter; i++){   // ----------- block translation to lower position --------------
  557.       for (int y = yLine[i] - 2; y > 0; y = y - 2) {
  558.          for (int x = 45; x < 76; x += 3) {
  559.             colorOld = vgaget(2*x + 32, 3*y + 2);
  560.             if (colorOld > 0) {
  561.                mySquare(x * 2 + 31, y * 3 + 1, 0);
  562.                mySquare(x * 2 + 31, (y + 2)* 3 + 1, colorOld);
  563.                
  564.             }
  565.          }
  566.       }
  567.    }
  568.    if (yCounter != 0) {
  569.       score = score + 2*int(pow(2, yCounter));
  570.       scoreNew += 2*int(pow(2, yCounter));
  571.       vgaTone(990,30);
  572.    }
  573.    drawScore(score);
  574.    yCounter = 0;
  575. }
  576.  
  577.  
  578. //-----------------------------------------------------------------------------------------------
  579. //--------------------- This is the main loop of the game ---------------------------------------
  580. //-----------------------------------------------------------------------------------------------
  581. void loop()
  582. {
  583.   //vga.clear(0);
  584.   //delay(10);
  585.   //myBackground();
  586.     //myBall();
  587.   processInputs();
  588.  
  589.   if (noLoop < 1){ // --------------- to generate new Tetraminos --------------------------------
  590.      blockN = blockNext;
  591.      if (noLoop == -1 ) { // -------------- only at the game beginning  -------------------------
  592.         drawMenu();
  593.         while (button_1 == 0 && button_2 == 0 && button_3 == 0 && button_4 == 0) {
  594.            blockN = 2 + int(random(6)); // -------------- tetraminos "O" is excluded -----------------
  595.            processInputs();
  596.         }
  597.      }
  598.      drawGameScreen();
  599.      drawScore(score);
  600.      blockNext = 1 + int(random(7));
  601.      blockDef(blockNext);
  602.      drawBlockNext();
  603.      blockDef(blockN);
  604.      x = 57;
  605.      y = 5;
  606.      button_1 = 1;
  607.      noLoop = 1;
  608.   }
  609.   if (button_2 == 1){ // ------------------------ rotation -------------------------
  610.      //if (button_5 == 1){rotationDirection = -1;}
  611.      if (button_2 == 1){rotationDirection = 1;}
  612.      delBlock();
  613.      blockRotation(rotationDirection);
  614.      checkBlockRotation();
  615.   }
  616.   if (button_1 == 1 || button_3 == 1){ // ------- translation ----------------------
  617.      if (button_1 == 1){delta = 3;}
  618.      if (button_3 == 1){delta = -3;}
  619.      delBlock();
  620.      checkBlockTranslation();
  621.   }
  622.   myTime++;
  623.   if (myTime % fast > fast - 2 || button_4 == 1){ // --- Tetraminos falling ----------
  624.      if (fast < 3) {fast = 2;}
  625.      y = y + 2;
  626.      delBlock();
  627.      replaceBlock();
  628.   }
  629.   else
  630.   {delay(10 + 2*fast);}
  631.     //show the rendering
  632.     //vga.show();
  633. }
  634. //-----------------------------------------------------------------------------------------------
  635. //--------------------- This is the end of the main loop ----------------------------------------
  636. //-----------------------------------------------------------------------------------------------
  637.  
  638.  
  639.  
  640. void vgaTone(int freq, int myTime){ // -------- it does not work... yet! ------------------------
  641.    //vga.tone(freq);
  642.    delay(myTime);
  643.    //vga.noTone();
  644. }
  645.  
  646. void vgaline(int x0, int y0, int x1, int y1, int color){
  647.    int a, b, c;
  648.    if (color == 0){a = 0; b = 0; c = 0;}
  649.    if (color == 1){a = 1; b = 0; c = 0;}
  650.    if (color == 2){a = 0; b = 1; c = 0;}
  651.    if (color == 3){a = 0; b = 0; c = 1;}
  652.    if (color == 4){a = 1; b = 1; c = 0;}
  653.    if (color == 5){a = 1; b = 0; c = 1;}
  654.    if (color == 6){a = 0; b = 1; c = 1;}
  655.    if (color == 7){a = 1; b = 1; c = 1;}
  656.    vga.line(x0, y0, x1 - 1, y1, vga.RGB(a*255, b*255, c*255));
  657. }
  658.  
  659. void myLine(int x0, int y0, int x1, int y1, int color){
  660.    int a, b, c;
  661.    if (color == 0){a = 0; b = 0; c = 0;}
  662.    if (color == 1){a = 1; b = 0; c = 0;}
  663.    if (color == 2){a = 0; b = 1; c = 0;}
  664.    if (color == 3){a = 0; b = 0; c = 1;}
  665.    if (color == 4){a = 1; b = 1; c = 0;}
  666.    if (color == 5){a = 1; b = 0; c = 1;}
  667.    if (color == 6){a = 0; b = 1; c = 1;}
  668.    if (color == 7){a = 1; b = 1; c = 1;}
  669.    vga.line(x0, y0, x1, y1, vga.RGB(a*255, b*255, c*255));
  670. }
  671.  
  672. int vgaget(int x, int y){  
  673.   if(vga.get(x, y) == 0)     {return 0;}
  674.   if(vga.get(x, y) == 31)    {return 1;}
  675.   if(vga.get(x, y) == 992)   {return 2;}
  676.   if(vga.get(x, y) == 15360) {return 3;}
  677.   if(vga.get(x, y) == 1023)  {return 4;}
  678.   if(vga.get(x, y) == 15391) {return 5;}
  679.   if(vga.get(x, y) == 16352) {return 6;}
  680.   if(vga.get(x, y) == 16383) {return 7;}
  681. }
  682.  
  683. void myRect(int x0, int y0, int w, int h, int color){
  684.    myColor(color);
  685.    vga.rect(x0, y0, w, h, vga.RGB(colA*255, colB*255, colC*255));
  686. }
  687.  
  688. void mySquare(int x0, int y0, int color){
  689.    myColor(color);
  690.    vga.fillRect(x0 + 1, y0 + 1, squareX - 3, squareY - 2, vga.RGB(colA*255, colB*255, colC*255));
  691.    if (color != 0) { myColor(color + 1); }
  692.    if (color == 7) { myColor(2); }
  693.    vga.rect(x0, y0, squareX - 1, squareY, vga.RGB(colA*255, colB*255, colC*255));
  694. }
  695.  
  696. void mySquare2(int x0, int y0, int color){ //---------------- simple version -----------------------------
  697.    myColor(color);
  698.    vga.fillRect(x0, y0, squareX - 1, squareY, vga.RGB(colA*255, colB*255, colC*255));
  699. }
  700.  
  701. void myColor(int color){
  702.    if (color == 0){colA = 0; colB = 0; colC = 0;}
  703.    if (color == 1){colA = 1; colB = 0; colC = 0;}
  704.    if (color == 2){colA = 0; colB = 1; colC = 0;}
  705.    if (color == 3){colA = 0; colB = 0; colC = 1;}
  706.    if (color == 4){colA = 1; colB = 1; colC = 0;}
  707.    if (color == 5){colA = 1; colB = 0; colC = 1;}
  708.    if (color == 6){colA = 0; colB = 1; colC = 1;}
  709.    if (color == 7){colA = 1; colB = 1; colC = 1;}
  710. }
  711.  
  712. //void myPrint(int x, int y, String text, int color){
  713. void setPrint(int x, int y, int color){
  714.   myColor(color);
  715.   vga.setTextColor(vga.RGB(colA*255, colB*255, colC*255), vga.RGB(0, 0, 0));
  716.   vga.setCursor(x, y);
  717.   //vga.print(String(text));
  718. }
  719.  
  720. void smoothRect(int x0, int y0, int w, int h, int r, int color){  //----- 1.6 comes from the rsolution ratio - 320/200 --------------
  721.    myLine(x0 + 1.6*r, y0 - 1, x0 + w - 1.6*r, y0 - 1, color);
  722.    myLine(x0 + 1.6*r, y0 + h, x0 + w - 1.6*r, y0 + h, color);
  723.    myLine(x0 - 1, y0 + r, x0 - 1, y0 + h - r, color);
  724.    myLine(x0 + w, y0 + r, x0 + w, y0 + h - r, color);
  725.    myColor(color);
  726.    for (int i = 0; i <= 25; i++) {
  727.       vga.dot(x0 + w - r*1.6*(1 - cos(i/25.*3.1415/2.)), y0 + r*(1 - sin(i/25.*3.1415/2.)), vga.RGB(colA*255, colB*255, colC*255));
  728.       vga.dot(x0 + r*1.6*(1 - cos(i/25.*3.1415/2.)), y0 + r*(1 - sin(i/25.*3.1415/2.)), vga.RGB(colA*255, colB*255, colC*255));
  729.       vga.dot(x0 + w - r*1.6*(1 - cos(i/25.*3.1415/2.)), y0 + h - r*(1 - sin(i/25.*3.1415/2.)), vga.RGB(colA*255, colB*255, colC*255));
  730.       vga.dot(x0 + r*1.6*(1 - cos(i/25.*3.1415/2.)), y0 + h - r*(1 - sin(i/25.*3.1415/2.)), vga.RGB(colA*255, colB*255, colC*255));
  731.    }
  732. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement