Hello all! I am making a Tetris remake with a Elegoo Mega 2560, A Elegoo 2.8 Inch Toushcreen TFT(works with Adafruit_GFX and MCUFRIEND_KBV). I will add a Keypad in the (Near) future.
But for now I started on this code and I am wondering why it won't work. (I explain what doesn't work in the end)
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <MyDelay.h>
#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 GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xC019
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define CubeSize 24
MyDelay fall (500);
MCUFRIEND_kbv tft;
bool init1 = false;
const int startposW = 5;
const int startposH = 10;
int posW;
int posH;
int Layer;
int S[16] = // A simple "S" Piece
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 1, 1,
0, 1, 1, 0
};
int S1[16] = // A "S" piece rotated 90 degrees
{
0, 0, 0, 0,
0, 0, 1, 0,
0, 0, 1, 1,
0, 0, 0, 1
};
int SI[16] = // A inverted "S" piece
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 1, 1, 0,
0, 0, 1, 1
};
int SI1[16] = // A inverted "S" piece rotated 90 degrees
{
0, 0, 0, 0,
0, 0, 0, 1,
0, 0, 1, 1,
0, 0, 1, 0
};
int Layer1[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer2[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer3[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer4[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer5[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer6[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer7[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer8[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer9[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int Layer10[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
void Block(int Width, int Hight, int color)
{
Width--;
if (color != BLACK)
{
tft.fillRect(CubeSize * Width, CubeSize * Hight, CubeSize, CubeSize, WHITE);
tft.fillRect(Width * CubeSize + 1, Hight * CubeSize + 1, CubeSize - 2, CubeSize - 2, color);
}
else if (color == BLACK)
{
tft.fillRect(CubeSize * Width, CubeSize * Hight, CubeSize, CubeSize, BLACK);
}
}
void makepiece(int color, int piece[], int Width, int Hight)
{
if (piece[15] != 0)
{
Block(Width, Hight, color);
}
if (piece[14] != 0)
{
Block(Width + 1, Hight, color);
}
if (piece[13] != 0)
{
Block(Width + 2, Hight, color);
}
if (piece[12] != 0)
{
Block(Width + 3, Hight, color);
}
if (piece[11] != 0)
{
Block(Width, Hight + 1, color);
}
if (piece[10] != 0)
{
Block(Width + 1, Hight + 1, color);
}
if (piece[9] != 0)
{
Block(Width + 2, Hight + 1, color);
}
if (piece[8] != 0)
{
Block(Width + 3, Hight + 1, color);
}
if (piece[7] != 0)
{
Block(Width, Hight + 2, color);
}
if (piece[6] != 0)
{
Block(Width + 1, Hight + 2, color);
}
if (piece[5] != 0)
{
Block(Width + 2, Hight + 2, color);
}
if (piece[4] != 0)
{
Block(Width + 3, Hight + 2, color);
}
if (piece[3] != 0)
{
Block(Width, Hight + 3, color);
}
if (piece[2] != 0)
{
Block(Width + 1, Hight + 3, color);
}
if (piece[1] != 0)
{
Block(Width + 2, Hight + 3, color);
}
if (piece[0] != 0)
{
Block(Width + 3, Hight + 3, color);
}
switch (Layer)
{
case 0:
if (piece[15] != 0)
{
switch (color)
{
case RED:
Layer1[posW] = 1;
break;
case GREEN:
Layer1[posW] = 2;
break;
case BLUE:
Layer1[posW] = 3;
break;
case YELLOW:
Layer1[posW] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[14] != 0)
{
switch (color)
{
case RED:
Layer1[posW++] = 1;
break;
case GREEN:
Layer1[posW++] = 2;
break;
case BLUE:
Layer1[posW++] = 3;
break;
case YELLOW:
Layer1[posW++] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[13] != 0)
{
switch (color)
{
case RED:
Layer1[posW + 2] = 1;
break;
case GREEN:
Layer1[posW + 2] = 2;
break;
case BLUE:
Layer1[posW + 2] = 3;
break;
case YELLOW:
Layer1[posW + 2] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[12] != 0)
{
switch (color)
{
case RED:
Layer1[posW + 3] = 1;
break;
case GREEN:
Layer1[posW + 3] = 2;
break;
case BLUE:
Layer1[posW + 3] = 3;
break;
case YELLOW:
Layer1[posW + 3] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[11] != 0)
{
switch (color)
{
case RED:
Layer2[posW] = 1;
break;
case GREEN:
Layer2[posW] = 2;
break;
case BLUE:
Layer2[posW] = 3;
break;
case YELLOW:
Layer2[posW] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[10] != 0)
{
switch (color)
{
case RED:
Layer2[posW++] = 1;
break;
case GREEN:
Layer2[posW++] = 2;
break;
case BLUE:
Layer2[posW++] = 3;
break;
case YELLOW:
Layer2[posW++] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[9] != 0)
{
switch (color)
{
case RED:
Layer2[posW + 2] = 1;
break;
case GREEN:
Layer2[posW + 2] = 2;
break;
case BLUE:
Layer2[posW + 2] = 3;
break;
case YELLOW:
Layer2[posW + 2] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[8] != 0)
{
switch (color)
{
case RED:
Layer2[posW + 3] = 1;
break;
case GREEN:
Layer2[posW + 3] = 2;
break;
case BLUE:
Layer2[posW + 3] = 3;
break;
case YELLOW:
Layer2[posW + 3] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[7] != 0)
{
switch (color)
{
case RED:
Layer3[posW] = 1;
break;
case GREEN:
Layer3[posW] = 2;
break;
case BLUE:
Layer3[posW] = 3;
break;
case YELLOW:
Layer3[posW] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[6] != 0)
{
switch (color)
{
case RED:
Layer3[posW++] = 1;
break;
case GREEN:
Layer3[posW++] = 2;
break;
case BLUE:
Layer3[posW++] = 3;
break;
case YELLOW:
Layer3[posW++] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[5] != 0)
{
switch (color)
{
case RED:
Layer3[posW + 2] = 1;
break;
case GREEN:
Layer3[posW + 2] = 2;
break;
case BLUE:
Layer3[posW + 2] = 3;
break;
case YELLOW:
Layer3[posW + 2] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[4] != 0)
{
switch (color)
{
case RED:
Layer3[posW + 3] = 1;
break;
case GREEN:
Layer3[posW + 3] = 2;
break;
case BLUE:
Layer3[posW + 3] = 3;
break;
case YELLOW:
Layer3[posW + 3] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[3] != 0)
{
switch (color)
{
case RED:
Layer4[posW] = 1;
break;
case GREEN:
Layer4[posW] = 2;
break;
case BLUE:
Layer4[posW] = 3;
break;
case YELLOW:
Layer4[posW] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[2] != 0)
{
switch (color)
{
case RED:
Layer4[posW++] = 1;
break;
case GREEN:
Layer4[posW++] = 2;
break;
case BLUE:
Layer4[posW++] = 3;
break;
case YELLOW:
Layer4[posW++] = 4;
break;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[1] != 0)
{
switch (color)
{
case RED:
Layer4[posW + 2] = 1;
break;
case GREEN:
Layer4[posW + 2] = 2;
break;
case BLUE:
Layer4[posW + 2] = 3;
case YELLOW:
Layer4[posW + 2] = 4;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
if (piece[0] != 0)
{
switch (color)
{
case RED:
Layer4[posW + 3] = 1;
break;
case GREEN:
Layer4[posW + 3] = 2;
break;
case BLUE:
Layer4[posW + 3] = 3;
case YELLOW:
Layer4[posW + 3] = 4;
}
posW = startposW;
posH = startposH;
Layer = 11;
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
}
}
void setup() {
tft.reset();
tft.begin(0x9341);
tft.setRotation(2);
tft.fillScreen(BLACK);
posW = startposW;
posH = startposH;
Layer = 11;
}
void loop()
{
if (init1 == false)
{
fall.start();
init1 = true;
}
if (fall.update())
{
fall.stop();
init1 = false;
makepiece(BLACK, S, posW, posH + 1);
makepiece(BLUE, S, posW, posH--);
Layer--;
for (int arraynum = 0; arraynum < 9; arraynum++)
{
int Width;
switch (arraynum)
{
case 9:
Width = 1;
break;
case 8:
Width = 2;
break;
case 7:
Width = 3;
break;
case 6:
Width = 4;
break;
case 5:
Width = 5;
break;
case 4:
Width = 6;
break;
case 3:
Width = 7;
break;
case 2:
Width = 8;
break;
case 1:
Width = 9;
break;
case 0:
Width = 10;
break;
}
switch (Layer1[arraynum])
{
case 1:
Block(Width, 1, RED);
break;
case 2:
Block(Width, 1, GREEN);
break;
case 3:
Block(Width, 1, BLUE);
break;
case 4:
Block(Width, 1, YELLOW);
break;
}
}
}
}
So What happens it that the S shape starts moving down. but when It reaches the bottom it just goes back up to the top. It is not saving the spot it fell in and making a piece there.
This is very important and I would be very thankful if anyone could help my out!
-M4A3E8 Sherman