Project showcase
Arduino Game X/O on the TFT Display

Arduino Game X/O on the TFT Display

New Arduino game for two players.

  • 5,511 views
  • 6 comments
  • 37 respects

Components and supplies

Apps and online services

About this project

The project consists of the game X/O. Instead of character X, we put blue squares, and instead of character O, we put red ones, and thus play the game.

Rules of the Game, X / O:

Here is how we play this game. The first player touches one box and into it entered X, followed by the second player to do the same or entered your sign O.

The first player wins if he makes a combination of on any column or row or principal or secondary diagonal.

The same applies to the other player character O.

Project Description:

*The first thing that appears on the TFT display is the name of my team who helped accomplish this.

Upon pressing the screen, the background will change and display the rules and tutorials. As we have said, instead of X and O, we are using the colors red and blue. Blue player starts first.

Pressing the display again changes the background and then appear nine white squares that represent the playing field. Now we have started the game.

If a combination of Xs or Os, in our case red or blue squares, coincide, then a new screen displays "winner" such that the color of the screen indicates which player won.

Or...

Video:

Code

Arduino codeArduino
#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);
  }
 

Schematics

Comments

Similar projects you might like

GPS Location Display With GPS And TFT Display Shields

Project tutorial by Boian Mitov

  • 8,802 views
  • 5 comments
  • 23 respects

Arduino Pong Game - OLED Display

Project tutorial by Nick Koumaris

  • 3,950 views
  • 1 comment
  • 9 respects

Arduino Based IR Thermometer with TFT Display and TMP006

Project tutorial by antiElectron

  • 2,363 views
  • 0 comments
  • 9 respects

LCD Game

Project showcase by Team Iron_SalsaStudio

  • 8,476 views
  • 8 comments
  • 15 respects

MAX72XX LED matrix display asteroids game

Project tutorial by kreck2003

  • 1,965 views
  • 2 comments
  • 14 respects
Add projectSign up / Login