#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9//PL1
#define TFT_CS 10//PL0
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int tx=50;
int ty=0;
int x=3;
int y=0;
int d=0;
int cords[10][21];//making array used for collisons
int LBUTTON=2;
int RBUTTON=3;
bool falling=true;
#define WHITE 0xFFFF
#define BLUE 0x00FF
#define BLACK 0x0000
#define RED 0xF000
#define YELLOW 0xFF00
int L[1][4][2]={{{0,0},{0,1},{1,1},{2,1}}};
int COLOR = BLUE;
void setup() {
pinMode(LBUTTON, INPUT);
pinMode(RBUTTON, INPUT);
tft.begin();
Serial.begin(9600);
while (tx<190){//making grid
while(ty<(14*21)){
tft.drawRect(tx,ty,14,14,WHITE);
ty+=14;
}
tx+=14;
ty=0;
}
tx=50;
while (falling==true){
delay(500);
for (int i=0;i<=3;i++){
tx=14*(L[0][i][0]+x)+50;
ty=14*(L[0][i][1]+y);
tft.fillRect(tx,ty,14,14,BLACK);
tft.drawRect(tx,ty,14,14,WHITE);
}
y+=1;
for(int i=0;i<=3;i++){
tx=14*(L[0][i][0]+x)+50;
ty=14*(L[0][i][1]+y);
if((cords[(L[0][i][0]+x-1)][L[0][i][1]+y]=1)){
xmove=1;
}
tft.fillRect(tx,ty,14,14,0xFFFF);
if ((L[0][i][1]+y+1)==21){
falling=false;
for(int b=0;b<=3;b++){
cords[L[0][b][0]+x][L[0][b][1]+y]=1;
}
}
}
int LBUTTONSTATE=digitalRead(LBUTTON);
int RBUTTONSTATE=digitalRead(RBUTTON);
if ((LBUTTONSTATE==HIGH)&&(x>0)&&(cords[(L[0][i][0]+x-1)][L[0][i][1]+y]!=1)){
x+=1;
}
if ((RBUTTONSTATE==HIGH)&&(x<9)&&(cords[(L[0][i][0]+x+1)][L[0][i][1]+y]!=1)){
x+=1;
}
}
}
void loop() {
x=5;
tx=14*x+50;
ty=0;
y=ty/14;
while ((ty<(14*20))&&(cords[x][y+1]!=1)){
tft.fillRect(tx,ty,14,14,BLACK);
tft.drawRect(tx,ty,14,14,WHITE);
ty+=14;
int LBUTTONSTATE=digitalRead(LBUTTON);
int RBUTTONSTATE=digitalRead(RBUTTON);
if ((LBUTTONSTATE==HIGH)&&(x>0)&&(cords[x-1][y]!=1)){
x-=1;
tx=14*x+50;
}
if ((RBUTTONSTATE==HIGH)&&(x<9)&&(cords[x+1][y]!=1)){
x+=1;
tx=14*x+50;
}
tft.fillRect(tx,ty,14,14,COLOR);
y=ty/14;
delay(100);
}
cords[x][y]=1;
for(int i=20;i>0;i--){
d=0;
for(int c=0;c<10;c++){
d=cords[c][i]+d;
if (d==10){
for(int h=0;h<10;h++){
tx=tx=14*h+50;
ty=14*i;
tft.fillRect(tx,ty,14,14,BLACK);
tft.drawRect(tx,ty,14,14,WHITE);
cords[h][i]=0;
falling = true;
}
for (int b=i;b>0;b--){
for(int h=0;h<10;h++){
if (cords[h][b]==1){
cords[h][b]=0;
tx=14*h+50;
ty=14*b;
tft.fillRect(tx,ty,14,14,BLACK);
tft.drawRect(tx,ty,14,14,WHITE);
cords[h][b+1]=1;
ty=14*(b+1);
tft.fillRect(tx,ty,14,14,WHITE);
i=21;
}
}
}
}
}
}
}