MakerRobotics
Published

Arduino Game X/O on the TFT Display

New Arduino game for two players.

BeginnerShowcase (no instructions)1 hour12,451
Arduino Game X/O on the TFT Display

Things used in this project

Story

Read more

Schematics

parts

Code

Arduino code

Arduino
#include <SPFD5408_Adafruit_GFX.h>    
#include <SPFD5408_Adafruit_TFTLCD.h> 
#include <SPFD5408_TouchScreen.h>

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define LCD_CS A3 
#define LCD_CD A2 
#define LCD_WR A1 
#define LCD_RD A0 

#define LCD_RESET A4 

#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define WHITE   0xFFFF


#define YP A1  
#define XM A2  
#define YM 7   
#define XP 6   

#define TS_MINX 125
#define TS_MINY 120
#define TS_MAXX 965
#define TS_MAXY 905

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BOXSIZE 40

int color;
int max_X[3][3];
int max_O[3][3];

void setup(void) {
  for(int i=0;i<3;i++)
     for(int j=0;j<3;j++){
       max_X[i][j]=0;
       max_O[i][j]=0;
      }
  
  Serial.begin(9600);

  tft.reset();
  
  tft.begin(0x9341);

  tft.setRotation(3); 
tft.fillScreen(BLACK);
  /////////////////////////////////////
  tft.setCursor (65, 50);
  tft.setTextSize (4);
  tft.setTextColor(GREEN);
  tft.println("PoliArdo");
  tft.setCursor (125, 95);
  tft.setTextSize (3);
  tft.setTextColor(WHITE);
  tft.println("team");
  tft.setCursor (35, 130);
  tft.setTextSize (4);
  tft.setTextColor(RED);
  tft.println("POLITEHNIKA");
  waitOneTouch();
  ////////////////////////////////////
  tft.fillScreen(BLACK);
  tft.setRotation(3); 
  tft.fillCircle(80, 70, 40, BLUE);
  tft.fillCircle(80, 170, 40, RED);
  tft.setCursor (150, 65);
  tft.setTextSize (3);
  tft.setTextColor(WHITE);
  tft.println("PLAYER 1");
  tft.setCursor (150, 155);
  tft.setTextSize (3);
  tft.setTextColor(WHITE);
  tft.println("PLAYER 2");
  tft.setRotation(2);
  tft.setCursor (30, 10);
  tft.setTextSize (3);
  tft.setTextColor(WHITE);
  tft.println("GAME  X/O");
  waitOneTouch();
  ///////////////////////////////////
  
  tft.setRotation(3); 
  tft.fillScreen(BLACK);

  tft.fillRect(30, 10, 70, 70, WHITE);
  tft.fillRect(30, 90, 70, 70, WHITE);
  tft.fillRect(30, 170, 70, 70, WHITE);

  tft.fillRect(120, 10, 70, 70, WHITE);
  tft.fillRect(120, 90, 70, 70, WHITE);
  tft.fillRect(120, 170, 70, 70, WHITE);

  tft.fillRect(210, 10, 70, 70, WHITE);
  tft.fillRect(210, 90, 70, 70, WHITE);
  tft.fillRect(210, 170, 70, 70, WHITE);
  
int f=0;
while(f != 5){
  Serial.println(f);
 
   digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    if (p.y < (TS_MINY-5)) {
      Serial.println("erase");
      tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
    }
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
   if(color==0)color=1;
   else color=0;
    
   delay(100);
   ////////////////////////////////////////////////////////////////////////
    if(p.x >=30 && p.x <= 100 && p.y>= 10 && p.y<=80){
       if(color==0){
            tft.fillRect(30, 10, 70, 70, RED);
            max_X[0][0]=1;
           f= proveri(max_X,1);
       }
       else {
           tft.fillRect(30, 10, 70, 70, BLUE);
           max_O[0][0]=1;
          f= proveri(max_O,0);
       }
       delay(10);
      }
     ////////////////////////////////////////////1///////////////////////////////
     else if(p.x >=30 && p.x <= 100 && p.y>=90 && p.y<=160){
           if(color==0){
                tft.fillRect(120, 10, 70, 70, RED);
                max_X[0][1]=1;
                f=proveri(max_X,1);
           }
           else {
                tft.fillRect(120, 10, 70, 70, BLUE);
                max_O[0][1]=1;
               f= proveri(max_O,0);
           }
           delay(10);
      }
      ////////////////////////////////////////////2////////////////////////////////
      else if(p.x >=30 && p.x <=100 && p.y>=170 && p.y<=240){
           if(color==0){
               tft.fillRect(210, 10, 70, 70, RED);
               max_X[0][2]=1;
              f= proveri(max_X,1);
           }
           else {
                tft.fillRect(210, 10, 70, 70, BLUE);
                max_O[0][2]=1;
               f= proveri(max_O,0);
           }
           delay(10);
      }
      /////////////////////////////////////3////////////////////////////////////
      else if(p.x >=120 && p.x <= 190 && p.y>= 10 && p.y<=80){
           if(color==0) {
                tft.fillRect(30, 90, 70, 70, RED);
                max_X[1][0]=1;
                f=proveri(max_X,1);
           }
           else  {
                tft.fillRect(30, 90, 70, 70, BLUE);
                max_O[1][0]=1;
                f=proveri(max_O,0);
           }
           delay(10);
      }
      /////////////////////////////////////4////////////////////////////////////
      else if(p.x >=120 && p.x <= 190 && p.y>= 90 && p.y<=160){
          if(color==0) {
               tft.fillRect(120, 90, 70, 70, RED);
               max_X[1][1]=1;
               f=proveri(max_X,1);
          }
          else {
                tft.fillRect(120, 90, 70, 70, BLUE);
                max_O[1][1]=1;
               f= proveri(max_O,0);
          }
          delay(10);
      }
      ////////////////////////////////5////////////////////////////////////////
      else if(p.x >=120 && p.x <= 190 && p.y>= 170 && p.y<=240){
           if(color==0){
                 tft.fillRect(210, 90, 70, 70, RED);
                 max_X[1][2]=1;
                 f=proveri(max_X,1);
           }
           else {
                  tft.fillRect(210, 90, 70, 70, BLUE);
                  max_O[1][2]=1;
                  f=proveri(max_O,0);
           }
           delay(10);
      }
      ////////////////////////////////6////////////////////////////////////////
      else if(p.x >=210 && p.x <= 280 && p.y>= 10 && p.y<=80){
           if(color==0){
                tft.fillRect(30, 170, 70, 70, RED);
                max_X[2][0]=1;
               f= proveri(max_X,1);
           }
           else {
                tft.fillRect(30, 170, 70, 70, BLUE);
                max_O[2][0]=1;
               f= proveri(max_O,0);
           }
           delay(10);
      }
      //////////////////////////////////7///////////////////////////////////////
      else if(p.x >=210 && p.x <= 280 && p.y>= 90 && p.y<=160){
            if(color==0){
                   tft.fillRect(120, 170, 70, 70, RED);
                   max_X[2][1]=1;
                   f=proveri(max_X,1);
            }
            else {
                   tft.fillRect(120, 170, 70, 70, BLUE);
                   max_O[2][1]=1;
                   f=proveri(max_O,0);
            }
            delay(10);
      }
      /////////////////////////////////8//////////////////////////////////////////////
      else if(p.x >=210 && p.x <= 280 && p.y>= 170 && p.y<=240){
            if(color==0){
                   tft.fillRect(210, 170, 70, 70, RED);
                   max_X[2][2]=1;
                  f= proveri(max_X,1);
            }
            else  {
                  tft.fillRect(210, 170, 70, 70, BLUE);
                  max_O[2][2]=1;
                  f=proveri(max_O,0);
            }
            delay(10);
      }
      //////////////////////////////9/////////////////////////////////////////////////
      delay(100);
  }
  
  
  }//exit while

  tft.setRotation(3); 
  tft.setCursor (65, 95);
  tft.setTextSize (6);
  tft.setTextColor(WHITE);
  tft.println("WINNER");
}

void loop(void) {}

TSPoint waitOneTouch() {

  TSPoint p;
  
  do {
    p= ts.getPoint(); 
  
    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);
  
  } while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
  
  return p;
}


int proveri(int matrix[3][3],int k){
  
  if(matrix[0][0]== 1 && matrix[1][1]==1 && matrix[2][2]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[0][0]==1 && matrix[0][1]==1 && matrix[0][2]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[1][0]==1 && matrix[1][1]==1 && matrix[1][2]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[2][0]==1 && matrix[2][1]==1 && matrix[2][2]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[0][0]==1 && matrix[1][0]==1 && matrix[2][0]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[0][1]==1 && matrix[1][1]==1 && matrix[2][1]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[0][2]==1 && matrix[1][2]==1 && matrix[2][2]==1){
    pobednik(k);
    return 5;
  }
  else if (matrix[0][2]==1 && matrix[1][1]==1 && matrix[2][0]==1){
    pobednik(k);
    return 5;
  }
}
void pobednik(int k){
  delay(1000);
  if(k==0)tft.fillScreen(BLUE);
  else tft.fillScreen(RED);
  }
 

Credits

MakerRobotics

MakerRobotics

6 projects • 133 followers

Comments