Tetris and the problems that occurred

The Start

I had a hard coming up with an idea for a final project. I eventually decided to make a tetris clone because I thought it was simple to do. I wanted to make it in processing because it was a program I liked using. This would prove to be a bit of a challenge as time went on. The first thing I start to do is figure out what I want the screen to look like. I first tried to draw out each piece and all the states that went with them. That would have taken way too long and decided to do some research on how to make it easier.  I found out that I could use 2d arrays to make a tetris game, and that the original used something similar.


The Discovers

I took a Saturday night to figure out how to use 2d arrays. Once I figured that out I made it so square would appear on-screen. I then tried to make a grid of squares. I then made it so I could get one square a different color from the rest.  That too me about 5 hours on a Saturday night to do. I felt awesome once I figured out 2d arrays. I think you can make some pretty neat games with them. I think the next I will do with 2d arrays is make the game of life originally by John Conway. Around this time I misplaced my USB which had all my work on it that I worked over the weekend for 5 hours. I learned the big importance of backups. SO a note to all people back up every program that you work on. On a job shadow that i have I saw that they keep a copy of every change that they make to there code for there web site Here is a link to what it is

Link


The struggle

The next step was making the pieces. I had graph paper from the being of the year and planned all states for each piece. Below are the plans that I made and some of the note I had.

001

 

As you can see there are seven pieces(I,o,t,z,s,j,l). Three pieces have 4 states,three pieces have three states, and one piece has one piece. With each piece I had make sure each state did not break the game. I kept getting an ArrayIndexOutofboundsexecption: x. Where x is the part of the array it was trying to access. This exception is caused by trying to access a part of an array that does not exists. As a result making the pieces took the longest. The t piece was the most difficult piece to make because each state is unique to that piece. The other 2 pieces where similar in shape. Making the pieces turn was simple. I just incremented a counter that started at 1 and went up to 4, then back down to one. Then I just put each state with a number, and those with 1 or 2 states I just gave each 2 numbers(ex 1 and 3).

So for example the t piece in the up state was like this in code

if (state == 1)//up
{
pos = 1;

if (x >= 9)
{
x = 7;
}

grid[x+1][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
if (x+2 == 10)
{
x = 7;
}
grid[x+2][y+1] = 1;
}

So I check if it would throw an error. I knew the grid had 10 blocks, but start at 0, so it was 0 though 9. That meant any thing greater than 9 would throw an exception.


The challenge

At this point i realized that I should have probably done this in green foot.  Processing is a pain to get collision working.  It was to late to switch programs and I had to much time into this project, plus I like a good challenge.  Up to this point I had it where the pieces would get to the bottom and stop with out exceptions being thrown most of the time. Some pieces had glitches where they would float in mid air after spinning around. This was the point where I knew I had to come up with a new system for the bottom of the screen. So I experimented a little bit and deiced to make the screen 21 x 10, but while hiding the 21st row. This would allow me to implement collision at any point the screen. I went ahead and implemented a section that would check to see if the row ahead of the most bottom piece was full. This was a bit of a challenge seeing how each piece has a different most bottom piece. So I implanted a variable called pos. And changed it based on what was being added to y. So for the t up piece, pos = 1 because that is the maximum value I am adding to y.

The current state

With leaving 20 minutes early each day and being busy with the program I am in. I did not get as much done as I would like. The current state of the project has fallen pieces that will fall to the bottom and change to a random piece really rapidly. I am struggling with spawning a new piece on the screen while keeping the old pieces. This game seemed easy in concept, but a little hard to execute than expected.

Below is over 600 lines of code that i wrote to make this project. I tried my best to comment what each method does to let other know what it does.

boolean m;
int[][] grid ;
int[][] grid1;
int[] queuearray;
int cellSize = 40;
int gridWidth;
int gridHeight;
int y;
int x;
int speed;
int state;
int pos = 0;
int sides = 1;
String s = "Score";
int score = 0;
int tet;
int queue = 99;
int q = 0;
tetr Cb = new tetr();
void setup()
{
background(0);
frameRate(7);
state = 4;
speed = 0;
size(800, 900);
// set grid size
gridWidth = 10;
gridHeight = 21;
// instigate the array
grid = new int[gridWidth][gridHeight];
grid1 = new int[gridWidth][gridHeight];
queuearray = new int[queue];
// populates grid
fillGrid();
displayGrid();
fillqueue();
}
void draw()
{
fillGrid();
setpiece(tet);
displayGrid();
move();
}
//goes though the queue and sets the next piece
void nextpiece()
{

if(q >= 99)
{
q = 0;
}
tet = queuearray[q];
q++;
print(q);
}
//fills the queue up with random numbers between 0 and 6
void fillqueue()
{
for(int q = 0;q < queue;q++)
{
setran();
queuearray[q] = tet;

}
}
void displayGrid()
{
for (int x = 0; x < gridWidth*40; x= x+ 40 ) {
for (int y = 0; y < gridHeight*40; y = y+40) {
if (grid[x/40][y/40] == 1 || grid1[x/40][y/40] == 1)
{
setpiececolor(tet);
}
if (grid[x/40][y/40] == 2 )
{
noStroke();
fill(0);
}
rect(x+200, y+40, cellSize, cellSize);
stroke(0);
fill(255);
}
}
}
//checkls to see if the value of the spot of the array is set to 1
//if it is then put fill to 128
/*void upadteDisplay()
{
for (int x = 0; x < gridWidth; x = x + 40) {
for (int c = 0; c < gridHeight - 1; c++) {
if (grid[x][y] == 1 || grid1[x][y] == 1)
{
setpiececolor(tet);
}

rect(x+40, 0, cellSize, cellSize);
}
}
}
*/
//sets all placews in the array to 0
void fillGrid()
{
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
grid[x][y] = 0;
grid1[x][y] = 0;
if (grid[x][y]== 0)
{
grid[x][20]= 2;
}
}
}
}
//checks for keyboard input
void keyPressed()
{
if (canmove()==true)
{
if (keyCode == UP)
{
rotatepiece();
}
if (x > 0) {
if (keyCode ==LEFT)
{
if (x < 0)
{
x = 0;
}
x = x-sides;
}
}
if (keyCode ==RIGHT)
{
if (x <= 0)
{
x = 0;
}
x = x+sides;
}
}
}
//checks to see if the peice can move
//then if it cant it will stop the piece and spawn a new piece
void move()
{
if (canmove()==false)
{
speed = 0;
sides = 0;
nextpiece();
copyarray();

score = score + 10;
} else {
speed = 1;
sides = 1;
}
y = y + speed;
}

void rotatepiece()
{
if (state == 4)
{
state = 0;
}
state = state + sides;
}

void ycheck()
{
if (x <= -1)
{
x = 0;
}

if (y+pos>= 21)
{
y = y-pos;
}
if (x>=10)
{
x = 9;
}
if (grid[x][y+pos] == 2)
{
y = y-1;
}
}

boolean canmove()
{
m = true;
if (y == 19-pos)
{
m = false;
}
return m;
}

void score()
{
rect(0, 0, 180, 80);
fill(0);
stroke(128);
textSize(32);
text(s, 50, 50);
text(score, 75, 75);
fill(255);
stroke(0);
}
void spawn()
{
if (canmove() == false)
{
setran();
m = true;
x=0;
y=0;
}
}
void setran()
{
tet = int(random(0,35)/6);
}
void setpiece(int tet1)
{
if (tet1 == 0)
{

Cb.shapei();
}
if (tet1 == 1)
{

Cb.shapeo();
}
if (tet1 == 2)
{

Cb.shapet();
}
if (tet1 == 3)
{

Cb.shapes();
}
if (tet1 == 4)
{

Cb.shapez();
}
if (tet1 == 5)
{

Cb.shapeJ();
}
if (tet1 ==6)
{

Cb.shapeL();
}
}

void setpiececolor(int tet1)
{
//i
if (tet1 == 0)
{
//blue
fill(2,176,242);
}
//o
if (tet1 == 1)
{
//yellow
fill(249, 201, 4);
}
//t
if (tet1 == 2)
{
//purple
fill(255, 0, 255);
}
//s
if (tet1 == 3)
{
//green
fill(0, 255, 0);
}
//z
if (tet1 == 4)
{
//red
fill(255, 0, 0);
}
//J
if (tet1 == 5)
{

//dark blue
fill(0,0,255);
}
//L
if (tet1 ==6)
{
//orange
fill(250, 69, 10);
}
}
void copyarray()
{
arrayCopy(grid, grid1);
}

class tetr
{

void shapei()
{
if (state == 1 || state == 3)//vertical
{
ycheck();
// @
// @
// @
// @
pos = 3;
if (x > 9)
{
x = 9;
}
grid[x][y] = 1;
grid[x][y+1] = 1;
grid[x][y+2] = 1;
grid[x][y+3] = 1;
}

if (state == 2 || state == 4)//horizontal
{
ycheck();
// @ @ @ @
pos = 0;
if (x < 0)
{
x = 0;
}
if (x > 6)
{
x = 6;
}
grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x+2][y] = 1;
grid[x+3][y] = 1;
}
}

void shapeo()//only has one state
{
pos = 1;
ycheck();
// @ @
// @ @
if (x+1 >= 10)
{
x = 8;
}

grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
}
void shapet()
{
ycheck();
// @
// @ @ @
if (state == 1)//up
{
pos = 1;

if (x >= 9)
{
x = 7;
}

grid[x+1][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
if (x+2 == 10)
{
x = 7;
}
grid[x+2][y+1] = 1;
}
// @
// @ @
// @
if (state == 2)//right
{
pos = 2;
if (x+1 >= 10)
{
x = 8;
}
grid[x][y] = 1;
grid[x+1][y+1] = 1;
grid[x][y+1] = 1;
if (y+2 >= 20)
{
y = 16;
}
grid[x][y+2] = 1;
}
// @ @ @
// @
if (state == 3)//down
{
pos = 1;
if (x+2 == 10)
{
x = 7;
}
grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x+2][y] = 1;
if (y+1 >= 20)
{
y = 18;
}
grid[x+1][y+1] = 1;
}
// @
// @ @
// @
if (state == 4)//left
{
pos = 2;
if (x+1 == 10)
{
x = 8;
}
if (y+2 >= 20)
{
y = 17;
}
grid[x+0][y+1] = 1;
grid[x+1][y+1] = 1;
grid[x+1][y] = 1;
grid[x+1][y+2] = 1;
}
}

void shapes()
{

if (state == 1 || state == 3)//left
{
if(x+2 >=10)
{
x = 7;
}
pos = 1;
ycheck();
grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x+1][y+1] = 1;
grid[x+2][y+1] = 1;
}

if (state == 2 || state == 4)//up
{
if(x + 1 >= 10)
{
x = 8;
}
pos = 2;
ycheck();
grid[x+1][y] = 1;
grid[x+1][y+1] = 1;
grid[x][y+1] = 1;
grid[x][y+2] = 1;
}
}

void shapez()
{
if (state == 1 || state == 3)//left
{
// @ @
// @ @
if (x+2 >= 10)
{
x = 7;
}
pos = 1;
ycheck();
grid[x+1][y] = 1;
grid[x+2][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
}
// @
// @ @
// @
if (state == 2 || state == 4)//up
{
if (x+1 >= 10)
{
x = 8;
}
pos = 2;
ycheck();
grid[x][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
grid[x+1][y+2] = 1;
}
}
void shapeJ()
{

// @
// @
// @ @
if (state == 1)//left
{
pos = 2;
if (x < 0)
{
x = 0;
}
if (x+1 >= 10)
{
x = 8;
}
ycheck();
grid[x+1][y] = 1;
grid[x+1][y+1] = 1;
grid[x+1][y+2] = 1;
grid[x][y+2] = 1;
}

// @ @ @
// @
if (state == 2)//down
{
pos = 1;
ycheck();
if (x+1 >= 10)
{
x = 8;
}
grid[x][y] = 1;
grid[x+1][y] = 1;
if (x+2 >= 9)
{
x = 7;
}
grid[x+2][y] = 1;
grid[x+2][y+1] = 1;
}

// @ @
// @
// @
if (state == 3)//right
{
pos = 2;
ycheck();
grid[x][y] = 1;
if (x + 1 >= 9)
{
x = 8;
}
grid[x+1][y] = 1;
grid[x][y+1] = 1;
if (y+1 >= 20)
{
y = 18;
}
grid[x][y+2] = 1;
}

// @
// @ @ @
if (state == 4)//up
{
pos = 1;
ycheck();

if (x+1 == 10)
{
x = 8;
}
if (x+2 >= 9)
{
x = 7;
}
grid[x][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
grid[x+2][y+1] = 1;
}
}
void shapeL()
{
if (state == 1)//right
{
// @
// @
// @ @
pos = 2;
ycheck();
if (x < 0)
{
x = 0;
}
if (x + 1 >= 10)
{
x = 8;
}
grid[x][y] = 1;
grid[x][y+1] = 1;
grid[x][y+2] = 1;
grid[x+1][y+2] = 1;
}

// @ @ @
// @
if (state == 2)//down
{
if (x+2 >= 10)
{
x = 7;
}
pos = 1;
ycheck();
grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x+2][y] = 1;
grid[x][y+1] = 1;
}

// @ @
// @
// @
if (state == 3)//left
{
if (x+ 1 >= 10)
{
x = 8;
}
pos = 2;
ycheck();
grid[x][y] = 1;
grid[x+1][y] = 1;
grid[x+1][y+1] = 1;
grid[x+1][y+2] = 1;
}

// @
// @ @ @
if (state == 4)//up
{
pos = 1;
ycheck();
if (x+2 >= 10)
{
x = 7;
}
grid[x+2][y] = 1;
grid[x][y+1] = 1;
grid[x+1][y+1] = 1;
grid[x+2][y+1] = 1;
}
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *