Advertisement
Astranome

Game MCUfriend

Oct 5th, 2020
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.21 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <MCUFRIEND_kbv.h>
  3. MCUFRIEND_kbv tft;
  4. #include <TouchScreen.h>
  5. #define MINPRESSURE 200
  6. #define MAXPRESSURE 1000
  7.  
  8. #define BLACK   0x0000
  9. #define BLUE    0x001F
  10. #define RED     0xF800
  11. #define GREEN   0x07E0
  12. #define CYAN    0x07FF
  13. #define MAGENTA 0xF81F
  14. #define YELLOW  0xFFE0
  15. #define WHITE   0xFFFF
  16. #define ORANGE  0xFD20
  17.  
  18. //const int XP = 7, XM = A1,YP = A2, YM = 6; //ID=0x9325
  19. //const int TS_LEFT = 136, TS_RT = 907, TS_TOP = 139, TS_BOT = 942;
  20. const int XP=7,XM=A1,YP=A2,YM=6;
  21. const int TS_LEFT=136,TS_RT=907,TS_TOP=139,TS_BOT=942;
  22.  
  23.  
  24. int s = 0;          //start var
  25. int a = 1;          //pause var
  26. int x = 95;         //paddle pos var
  27. int positionX = 120;//ball X position
  28. int positionY = 240;//ball Y position
  29. int speedX = 0;     //ball speed
  30. int speedY = 0;     //ball speed
  31. int k = 0;          //floor1
  32. int l = 0;          //floor2
  33. int i = 0;          //floor3
  34. int h = 0;          //floor4
  35. int B = 0;          //start breakout var
  36. int f = 0;          //exit var
  37. int P = 0;          //pong var
  38. int g = 0;          //score var
  39. int y2 = 250;       //the ball should not pass this
  40. int r = 1;          // pong start var
  41. int v = 0;          // pong start var
  42.  
  43.  
  44. //TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  45. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  46.  
  47. Adafruit_GFX_Button left_btn, right_btn, pause_btn, start_btn, breakout_btn, pong_btn, exit_btn;
  48.  
  49. int pixel_x, pixel_y;  
  50. bool Touch_getXY(void)
  51. {
  52.     TSPoint p = ts.getPoint();
  53.     pinMode(YP, OUTPUT);    
  54.     pinMode(XM, OUTPUT);
  55.     digitalWrite(YP, HIGH);
  56.     digitalWrite(XM, HIGH);
  57.     bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  58.     if (pressed) {
  59.         //pixel_x = map(p.y, TS_RT, TS_LEFT, 0, tft.width());
  60.        // pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
  61.         pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
  62.         pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  63.  
  64.        
  65.     }
  66.     return pressed;
  67. }
  68.  
  69. void setup()
  70. {
  71.     Serial.begin(9600);
  72.     uint16_t ID = tft.readID();
  73.     Serial.print("TFT ID = 0x");
  74.     Serial.println(ID, HEX);
  75.     Serial.println("Calibrate for your Touch Panel");
  76.     //if (ID == 0xD3D3) ID = 0x9486;
  77.     tft.begin(ID);
  78.     tft.setRotation(0);            
  79.     tft.fillScreen(BLACK);
  80.     left_btn.initButton(&tft, 40, 290, 75, 50, BLACK, BLACK, WHITE, "<-", 3);
  81.     right_btn.initButton(&tft, 200, 290, 75, 50, BLACK, BLACK, WHITE, "->", 3);
  82.     pause_btn.initButton(&tft, 220, 10, 40, 20, BLACK, BLACK, WHITE, "-", 3);
  83.     start_btn.initButton(&tft, 120, 150, 150, 50, BLACK, BLACK, BLACK, "", 3);
  84.     breakout_btn.initButton(&tft, 120, 50, 150, 50, BLACK, BLACK, BLACK, "", 3);
  85.     pong_btn.initButton(&tft, 120, 100, 100, 50, BLACK, BLACK, BLACK, "", 3);
  86.     exit_btn.initButton(&tft, 20, 10, 40, 20, BLACK, BLACK, WHITE, "x", 3);
  87.     exit_btn.drawButton(false);
  88.     pong_btn.drawButton(false);
  89.     breakout_btn.drawButton(false);
  90.     start_btn.drawButton(false);
  91.     pause_btn.drawButton(false);
  92.     left_btn.drawButton(false);
  93.     right_btn.drawButton(false);
  94. }
  95.  
  96. //---------------------------------------------drawrect----------------------------------
  97.  
  98.  
  99.  void drawrect() {                          // make the round rects
  100.   tft.fillRoundRect(0, 30, 59, 10, 3, RED);
  101.   tft.fillRoundRect(60, 30, 59, 10, 3, RED);
  102.   tft.fillRoundRect(120, 30, 59, 10, 3, RED);
  103.   tft.fillRoundRect(180, 30, 59, 10, 3, RED);
  104.  
  105.   tft.fillRoundRect(0, 41, 59, 10, 3, ORANGE);
  106.   tft.fillRoundRect(60, 41, 59, 10, 3, ORANGE);
  107.   tft.fillRoundRect(120, 41, 59, 10, 3, ORANGE);
  108.   tft.fillRoundRect(180, 41, 59, 10, 3, ORANGE);
  109.  
  110.   tft.fillRoundRect(0, 52, 59, 10, 3, YELLOW);
  111.   tft.fillRoundRect(60, 52, 59, 10, 3, YELLOW);
  112.   tft.fillRoundRect(120, 52, 59, 10, 3, YELLOW);
  113.   tft.fillRoundRect(180, 52, 59, 10, 3, YELLOW);
  114.  
  115.   tft.fillRoundRect(0, 63, 59, 10, 3, GREEN);
  116.   tft.fillRoundRect(60, 63, 59, 10, 3, GREEN);
  117.   tft.fillRoundRect(120, 63, 59, 10, 3, GREEN);
  118.   tft.fillRoundRect(180, 63, 59, 10, 3, GREEN);
  119.  }
  120.  
  121.  
  122.  
  123.  
  124. //---------------------------------------gameGUI--------------------------------------------------
  125.  
  126.  
  127. void gameGUI() {  // check if gameGUI already has run
  128.   if ( f == 0) {                        
  129.   tft.setCursor(0, 160);
  130.   tft.setTextSize(2);
  131.   tft.setTextColor(WHITE);
  132.   tft.print("PLEASE CHOOSE A GAME");
  133.   tft.setCursor(50, 40);
  134.   tft.setTextSize(3);
  135.   tft.setTextColor(RED);
  136.   tft.print("BREAKOUT");
  137.   tft.setCursor(90, 90);
  138.   tft.setTextSize(3);
  139.   tft.setTextColor(ORANGE);
  140.   tft.print("PONG");
  141.  
  142.   }
  143.  
  144.  
  145.     bool down = Touch_getXY();
  146.     breakout_btn.press(down && breakout_btn.contains(pixel_x, pixel_y));
  147.     pong_btn.press(down && pong_btn.contains(pixel_x, pixel_y));
  148.    
  149.  
  150.     if (breakout_btn.justReleased())
  151.         breakout_btn.drawButton();
  152.  
  153.         if (pong_btn.justReleased())
  154.             pong_btn.drawButton();
  155.  
  156.         if (breakout_btn.justPressed()) { // check if buttons pressed
  157.             breakout_btn.drawButton(true);
  158.             tft.fillScreen(BLACK);
  159.             B = 1;
  160.             f = 1;
  161.             }
  162.  
  163.         if (pong_btn.justPressed()) { // check if buttons pressed
  164.             pong_btn.drawButton(true);
  165.             tft.fillScreen(BLACK);
  166.             P = 1;
  167.             f = 1;
  168.             r = 1;
  169.            }
  170.        
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. //----------------------------------------------pong-------------------------------------------
  178.  
  179.  
  180.  
  181.  void pong() {
  182.    if (P == 1) { // if P = 1 start pong
  183.       B = 0;
  184.   if ( r == 1) { // if r = 1 dont start game till v = 1
  185.    speedX = 0;
  186.    speedY = 0;
  187.   tft.setCursor(60,140);
  188.   tft.setTextSize(4);
  189.   tft.setTextColor(GREEN);
  190.   tft.print("START");
  191.   }
  192.  }
  193.  
  194.   if ( v == 1) {  // if start is pressed, start game
  195.     tft.fillScreen(BLACK);
  196.     speedX = 1;
  197.     speedY = -1;
  198.     v = 0;
  199.     r = 0;
  200.     exit_btn.drawButton(true);
  201.     exit_btn.drawButton(false);
  202.     left_btn.drawButton(true);
  203.     right_btn.drawButton(true);
  204.     left_btn.drawButton(false);
  205.     right_btn.drawButton(false);
  206.   }
  207.  
  208.   tft.fillRect(0 ,50, 400, 3, WHITE); //make two lines for the game area
  209.   tft.fillRect(0 ,260, 400, 3, WHITE);
  210.   tft.fillCircle(positionX, positionY, 4, BLACK); //undraw the ball
  211.  
  212.  
  213.   if(positionX < 0){ //ball go right if positionX < 0
  214.     speedX = 1;
  215.   }
  216.   if(positionX > tft.width()){ //ball go left if positionY > screens width
  217.     speedX = -1;
  218.   }
  219.   if(positionY < 50){ //ball go down if positionY < 50
  220.     speedY = 1;
  221.   }
  222.   if(positionY > 260){ //ball go up if positionY > 260
  223.     speedY = -1;
  224.   }
  225.   if (positionY == 220 && (positionX >= x && positionX <= x + 50)) { //if ball hit paddle on y level 220 and between x level x and x+50 the ball bounces off the paddle
  226.     speedY = -1;
  227.    
  228.     if ( speedX == 1) { //check if the ball should go left or right after hitting the paddle
  229.          speedX = 1;
  230.     } else {
  231.           speedX = -1;
  232.     }
  233.    
  234.     g = g+1;                                //score count
  235.     tft.fillRect( 110 ,10, 40, 40, BLACK);
  236.     tft.setCursor(110, 20);
  237.     tft.setTextSize(3);
  238.     tft.setTextColor(WHITE);
  239.     if (positionY < 250 ) { // add score
  240.     tft.print(g);
  241.     } else {
  242.     tft.print(0);
  243.     }
  244.   }
  245.  
  246.   if (positionY >= y2 ) { // check if the ball has gone past the paddle and into y2
  247.     speedX = 0;
  248.     speedY = 0;
  249.     tft.setCursor(15, 100);
  250.     tft.setTextSize(4);
  251.     tft.setTextColor(RED);
  252.     tft.print("GAME OVER");
  253.     tft.setCursor(60, 150);
  254.     tft.setTextSize(3);
  255.     tft.setTextColor(GREEN);
  256.     tft.print("RESTART");
  257.   }
  258.  
  259.  
  260.   positionX = positionX + speedX;
  261.   positionY = positionY + speedY;
  262.   tft.fillCircle(positionX, positionY, 4, WHITE); //draw the ball
  263.   tft.fillRect( x ,220, 50, 10, WHITE);           //draw the paddle
  264.  
  265.     bool down = Touch_getXY();
  266.     left_btn.press(down && left_btn.contains(pixel_x, pixel_y));
  267.     right_btn.press(down && right_btn.contains(pixel_x, pixel_y));
  268.     start_btn.press(down && start_btn.contains(pixel_x, pixel_y));
  269.     exit_btn.press(down && exit_btn.contains(pixel_x, pixel_y));
  270.  
  271.    
  272.     if (left_btn.justReleased()) //check if buttons been pressed
  273.         left_btn.drawButton();
  274.  
  275.     if (right_btn.justReleased())
  276.         right_btn.drawButton();
  277.  
  278.     if (start_btn.justReleased())
  279.         start_btn.drawButton();
  280.  
  281.     if (exit_btn.justReleased())
  282.         exit_btn.drawButton();
  283.  
  284.     if (start_btn.justPressed()) {
  285.         start_btn.drawButton(true);
  286.     }
  287.    
  288.  
  289.         if (start_btn.justPressed()) { //if start_btn (restart button) had been pressed, start the game over again
  290.         start_btn.drawButton(true);
  291.         tft.fillScreen(BLACK);
  292.         speedX = 1;
  293.         speedY = 1;
  294.         g = g - g; // reset g
  295.         v = 1;
  296.         positionX = 35;
  297.         positionY = 35;
  298.         exit_btn.drawButton(true);
  299.         exit_btn.drawButton(false);
  300.         left_btn.drawButton(true);
  301.         right_btn.drawButton(true);
  302.         left_btn.drawButton(false);
  303.         right_btn.drawButton(false);
  304.        
  305.     }
  306.    
  307.     if (left_btn.justPressed()) { //if left button pressed, move the paddel 20pixels to the left
  308.         left_btn.drawButton(true);
  309.         tft.fillRect( x ,220, 50, 10, BLACK);
  310.         if (x < 0) {  //dont move the paddle if it is on the egde of the display
  311.              x = x + 0;
  312.        }     else {  // check if the paddle is in the screen
  313.              x = x - 20;
  314.        }
  315.     }
  316.    
  317.     if (right_btn.justPressed()) { //if right button pressed, move the paddel 20pixels to the right
  318.         right_btn.drawButton(true);
  319.         tft.fillRect( x ,220, 50, 10, BLACK);
  320.         if (x > 190) {  //dont move the paddle if it is on the egde of the display
  321.              x = x - 0;
  322.        }     else {  // check if the paddle is in the screen
  323.              x = x + 20;
  324.        }  
  325.     }
  326.    
  327.             if (exit_btn.justPressed()) { // check if buttons pressed
  328.              r = 0;
  329.              P = 0;
  330.              B = 0;
  331.              a = 1;
  332.             exit_btn.drawButton(true);
  333.             tft.fillScreen(BLACK);
  334.             tft.fillRect(0 ,50, 400, 3, BLACK); //make two lines for the game area
  335.             tft.fillRect(0 ,260, 400, 3, BLACK);
  336.             tft.fillCircle(positionX, positionY, 4, BLACK); //undraw the ball
  337.             tft.fillRect( x ,220, 50, 10, BLACK);           //draw the paddle
  338.             speedX = 0;
  339.             speedY = 0;
  340.             f = 0;
  341.             gameGUI();
  342.          
  343. }
  344.  }
  345.  
  346.  
  347.  
  348.  //-------------------------------------------------------BREAKOUT-------------------------------------------------------------
  349.  
  350. void breakout() {
  351.  
  352.   if (B == 1) { // if B = 1 start breakout
  353.       P = 0;
  354.  
  355.   if ( a == 1) { // if a = 1 pause game til start is pressed
  356.    speedX = 0;
  357.    speedY = 0;
  358.   tft.setCursor(60,140);
  359.   tft.setTextSize(4);
  360.   tft.setTextColor(GREEN);
  361.   tft.print("START");
  362.   }
  363.  }
  364.  
  365.   if ( s == 1) {    // if start is pressed start game
  366.     tft.fillScreen(BLACK);
  367.     drawrect();
  368.     speedX = 1;
  369.     speedY = -1;
  370.     s = 0;
  371.     a = 0;
  372.     P = 0;
  373.     pause_btn.drawButton(true);
  374.     pause_btn.drawButton(false);
  375.     exit_btn.drawButton(true);
  376.     exit_btn.drawButton(false);
  377.     left_btn.drawButton(true);
  378.     right_btn.drawButton(true);
  379.     left_btn.drawButton(false);
  380.     right_btn.drawButton(false);
  381.   }
  382.  
  383.   if (h <= 4)   {  
  384.   if ( positionX >= 0 && positionX <= 60 && positionY == 40 ) { //check if box has been hit
  385.     tft.fillRoundRect(0, 30, 59, 10, 3, BLACK);
  386.     speedY = 1;
  387.     h = h + 1;   //line four
  388.     if ( speedX == 1) {
  389.          speedX = 1;
  390.     } else {
  391.           speedX = -1;
  392.     }
  393.   }
  394.  
  395.   if ( positionX >= 60 && positionX <= 120 && positionY == 40 ) { //check if box has been hit
  396.     tft.fillRoundRect(60, 30, 59, 10, 3, BLACK);
  397.     speedY = 1;
  398.     h = h + 1;   //line four
  399.     if ( speedX == 1) {
  400.          speedX = 1;
  401.     } else {
  402.           speedX = -1;
  403.     }
  404.   }
  405.  
  406.   if ( positionX >= 120 && positionX <= 180 && positionY == 40 ) {  //check if box has been hit
  407.     tft.fillRoundRect(120, 30, 59, 10, 3, BLACK);
  408.     speedY = 1;
  409.     h = h + 1;   //line four
  410.     if ( speedX == 1) {
  411.          speedX = 1;
  412.     } else {
  413.           speedX = -1;
  414.     }
  415.   }
  416.  
  417.   if ( positionX >= 180 && positionX <= 240 && positionY == 40 ) { //check if box has been hit
  418.     tft.fillRoundRect(180, 30, 59, 10, 3, BLACK);
  419.     speedY = 1;
  420.     h = h + 1;   //line four
  421.     if ( speedX == 1) {
  422.          speedX = 1;
  423.     } else {
  424.           speedX = -1;
  425.     }
  426.   }
  427.                       //next row
  428.   }
  429.   if (i <= 4)   {
  430.   if ( positionX >= 0 && positionX <= 60 && positionY == 51 ) { //check if box has been hit
  431.     tft.fillRoundRect(0, 41, 59, 10, 3, BLACK);
  432.     speedY = 1;
  433.     i = i + 1;   //line three
  434.     if ( speedX == 1) {
  435.          speedX = 1;
  436.     } else {
  437.           speedX = -1;
  438.     }
  439.   }
  440.  
  441.   if ( positionX >= 60 && positionX <= 120 && positionY == 51 ) { //check if box has been hit
  442.     tft.fillRoundRect(60, 41, 59, 10, 3, BLACK);
  443.     speedY = 1;
  444.     i = i + 1;   //line three
  445.     if ( speedX == 1) {
  446.          speedX = 1;
  447.     } else {
  448.           speedX = -1;
  449.     }
  450.   }
  451.  
  452.   if ( positionX >= 120 && positionX <= 180 && positionY == 51 ) { //check if box has been hit
  453.     tft.fillRoundRect(120, 41, 59, 10, 3, BLACK);
  454.     speedY = 1;
  455.     i = i + 1;   //line three
  456.     if ( speedX == 1) {
  457.          speedX = 1;
  458.     } else {
  459.           speedX = -1;
  460.     }
  461.   }
  462.  
  463.   if ( positionX >= 180 && positionX <= 240 && positionY == 51 ) { //check if box has been hit
  464.     tft.fillRoundRect(180, 41, 59, 10, 3, BLACK);
  465.     speedY = 1;
  466.     i = i + 1;   //line three
  467.     if ( speedX == 1) {
  468.          speedX = 1;
  469.     } else {
  470.           speedX = -1;
  471.     }
  472.   }
  473.   }              //next row
  474.   if (l <= 4)   {              
  475.   if ( positionX >= 0 && positionX <= 60 && positionY == 62 ) { //check if box has been hit
  476.     tft.fillRoundRect(0, 52, 59, 10, 3, BLACK);
  477.     speedY = 1;
  478.     l = l + 1;   //line two
  479.     if ( speedX == 1) {
  480.          speedX = 1;
  481.     } else {
  482.           speedX = -1;
  483.     }
  484.   }
  485.  
  486.   if ( positionX >= 60 && positionX <= 120 && positionY == 62 ) { //check if box has been hit
  487.     tft.fillRoundRect(60, 52, 59, 10, 3, BLACK);
  488.     speedY = 1;
  489.     l = l + 1;   //line two
  490.     if ( speedX == 1) {
  491.          speedX = 1;
  492.     } else {
  493.           speedX = -1;
  494.     }
  495.   }
  496.  
  497.   if ( positionX >= 120 && positionX <= 180 && positionY == 62 ) { //check if box has been hit
  498.     tft.fillRoundRect(120, 52, 59, 10, 3, BLACK);
  499.     speedY = 1;
  500.     l = l + 1;   //line two
  501.     if ( speedX == 1) {
  502.          speedX = 1;
  503.     } else {
  504.           speedX = -1;
  505.     }
  506.   }
  507.  
  508.   if ( positionX >= 180 && positionX <= 240 && positionY == 62 ) { //check if box has been hit
  509.     tft.fillRoundRect(180, 52, 59, 10, 3, BLACK);
  510.     speedY = 1;
  511.     l = l + 1;   //line two
  512.     if ( speedX == 1) {
  513.          speedX = 1;
  514.     } else {
  515.           speedX = -1;
  516.     }
  517.   }
  518. }
  519.   if (k <= 4)   {                   // next row
  520.  
  521.     if ( positionX >= 0 && positionX <= 60 && positionY == 73 ) { //check if box has been hit
  522.     tft.fillRoundRect(0, 63, 59, 10, 3, BLACK);
  523.     speedY = 1;
  524.     k = k + 1;   //line one
  525.     if ( speedX == 1) {
  526.          speedX = 1;
  527.     } else {
  528.           speedX = -1;
  529.     }
  530.   }
  531.  
  532.   if ( positionX >= 60 && positionX <= 120 && positionY == 73 ) { //check if box has been hit
  533.     tft.fillRoundRect(60, 63, 59, 10, 3, BLACK);
  534.     speedY = 1;
  535.     k = k + 1;   //line one
  536.     if ( speedX == 1) {
  537.          speedX = 1;
  538.     } else {
  539.           speedX = -1;
  540.     }
  541.   }
  542.  
  543.    if ( positionX >= 120 && positionX <= 180 && positionY == 73 ) { //check if box has been hit
  544.     tft.fillRoundRect(120, 63, 59, 10, 3, BLACK);
  545.     speedY = 1;
  546.     k = k + 1;   //line one
  547.     if ( speedX == 1) {
  548.          speedX = 1;
  549.     } else {
  550.           speedX = -1;
  551.     }
  552.   }  
  553.  
  554.   if ( positionX >= 180 && positionX <= 240 && positionY == 73 ) { //check if box has been hit
  555.     tft.fillRoundRect(180, 63, 59, 10, 3, BLACK);
  556.     speedY = 1;
  557.     k = k + 1;   //line one
  558.     if ( speedX == 1) {
  559.          speedX = 1;
  560.     } else {
  561.           speedX = -1;
  562.     }
  563.   }                    
  564.   }
  565.  
  566.  
  567.   tft.fillRect(x, 260, 50, 10, WHITE); // draw paddle
  568.   tft.fillCircle(positionX, positionY, 4, BLACK); //undraw ball
  569.   positionX = positionX + speedX;
  570.   positionY = positionY + speedY;
  571.   tft.fillCircle(positionX, positionY, 4, WHITE); //draw the ball
  572.  
  573.   if(positionX < 0){ //ball go right if positionX < 0
  574.     speedX = 1;
  575.   }
  576.   if(positionX > tft.width()){ //ball go left if positionY > screens width
  577.     speedX = -1;
  578.   }
  579.   if(positionY < 0){ //ball go down if positionY < 0
  580.     speedY = 1;
  581.   }
  582.   if (positionY == 260 && (positionX >= x && positionX <= x + 50)) { //if ball hit paddle on y level 260 and between x level x and x+50 the ball bounces off the paddle
  583.     speedY = -1;
  584.    
  585.     if ( speedX == 1) { //check if the ball should go left or right after hitting the paddle
  586.          speedX = 1;
  587.     } else {
  588.           speedX = -1;
  589.     }
  590.   }
  591.   if (positionY == 280) {  // end game if ball is under y 280
  592.     tft.fillScreen(BLACK);
  593.     pause_btn.drawButton(true);
  594.     pause_btn.drawButton(false);
  595.     exit_btn.drawButton(true);
  596.     exit_btn.drawButton(false);
  597.     left_btn.drawButton(true);
  598.     right_btn.drawButton(true);
  599.     left_btn.drawButton(false);
  600.     right_btn.drawButton(false);
  601.     speedX = 0;
  602.     speedY = 0;
  603.     k = 0;
  604.     l = 0;
  605.     i = 0;
  606.     h = 0;
  607.     positionY = 240;
  608.     positionX = 120;
  609.     tft.setCursor(40,140);
  610.     tft.setTextSize(4);
  611.     tft.setTextColor(GREEN);
  612.     tft.print("RESTART");
  613.     tft.setCursor(15,80);
  614.     tft.setTextSize(4);
  615.     tft.setTextColor(RED);
  616.     tft.print("GAME OVER");
  617.   }
  618.   if ( h == 5) {           //if all boxes are hit, end game
  619.     tft.fillScreen(WHITE);
  620.     h = 0;
  621.   }
  622.  
  623.  
  624.   bool down = Touch_getXY();
  625.     left_btn.press(down && left_btn.contains(pixel_x, pixel_y));
  626.     right_btn.press(down && right_btn.contains(pixel_x, pixel_y));
  627.     pause_btn.press(down && pause_btn.contains(pixel_x, pixel_y));
  628.     start_btn.press(down && start_btn.contains(pixel_x, pixel_y));
  629.     exit_btn.press(down && exit_btn.contains(pixel_x, pixel_y));
  630.  
  631.     if (start_btn.justReleased())
  632.         start_btn.drawButton();
  633.  
  634.     if (pause_btn.justReleased())
  635.         pause_btn.drawButton();
  636.  
  637.     if (left_btn.justReleased())
  638.         left_btn.drawButton();
  639.  
  640.     if (right_btn.justReleased())
  641.         right_btn.drawButton();
  642.  
  643.     if (exit_btn.justReleased())
  644.         exit_btn.drawButton();
  645.        
  646.         if (start_btn.justPressed()) {
  647.             start_btn.drawButton(true); //check if buttons pressed
  648.               s = 1;    
  649.         }
  650.  
  651.         if (pause_btn.justPressed()) {
  652.             pause_btn.drawButton(true);
  653.            a = 1;  
  654.            
  655.        }
  656.        
  657.         if (left_btn.justPressed()) {
  658.             left_btn.drawButton(true);
  659.             tft.fillRect(x, 260, 50, 10, BLACK);
  660.              if (x < 0) { //if paddle is at end of display, dont move it
  661.              x = x + 0;
  662.        }     else { //move the paddle
  663.              x = x - 20;
  664.        }
  665.            
  666.    }
  667.         if (right_btn.justPressed()) {
  668.             right_btn.drawButton(true);
  669.             tft.fillRect(x, 260, 50, 10, BLACK);
  670.             if (x > 190) { //if paddle is at end of display, dont move it
  671.              x = x - 0;
  672.        }     else {  //move the paddle
  673.              x = x + 20;
  674.        }
  675.            
  676.    }
  677.  
  678.          if (exit_btn.justPressed()) { // check if buttons pressed
  679.             exit_btn.drawButton(true);
  680.             tft.fillScreen(BLACK);
  681.             P = 0;
  682.             r = 0;
  683.             B = 0;
  684.             k = 0;
  685.             l = 0;
  686.             i = 0;
  687.             h = 0;
  688.             a = 1;
  689.             f = 0;
  690.             speedX = 0;
  691.             speedY = 0;
  692.             tft.fillRect(x, 260, 50, 10, BLACK); // draw paddle
  693.             tft.fillCircle(positionX, positionY, 4, BLACK); //undraw ball
  694.             gameGUI();
  695.          
  696. }
  697. }
  698.  
  699.  
  700.   //-------------------------------------------void loop-------------------------------------------
  701.  
  702.  
  703. void loop() {
  704.  
  705.   gameGUI();
  706.  
  707.   if ( P == 0 && B == 0) { // if none of the buttons is pressed, keep gameGUI up.
  708.     f = 0;
  709.     gameGUI();
  710.   }
  711.  
  712.   if ( P == 1) { // if button P is pressed, start pong
  713.     pong();
  714.   }
  715.   if ( B == 1) { // if button B is pressed, Start breakout
  716.     breakout();
  717.   }
  718. }
  719.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement