# Making of: Tetris

**URL:** <https://discourse.processing.org/t/making-of-tetris/15024>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 29, 2019, 4:25pm UTC](https://discourse.processing.org/t/making-of-tetris/15024 "2019-10-29T16:25:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImDaGr3en](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@ImDaGr3en](https://discourse.processing.org/u/ImDaGr3en)\
**Post date:** [October 29, 2019, 4:25pm UTC](https://discourse.processing.org/t/making-of-tetris/15024/1 "2019-10-29T16:25:55Z")

</div>

**Hey all!**  
Today at school I started coding a Tetris-like mini game, based on shapes which fall to the ground and land on the bottom of the window… but I’m now stuck!  
The fact is, everything goes right when I create a new object, it’s drawn and it lands on the ground. But then I can’t build the code so I can refresh the screen without overlaying the previous shape (I thought about putting a `background(0);` code before the `draw()` executes, and I kept this solution until now, but it’s just the reason why I’m asking, because it causes me the problem I wrote before)…  
So, kindly, _could anyone help me?_

> Here’s the code:

```auto
//main file

int pTile=0;
ArrayList<Blocks> block;

void setup() {

  frameRate(10);
  size(900, 900);
  background(0);
  block=new ArrayList<Blocks>(1);
  block.add(new Blocks());
  blockSetup();
}

void blockSetup() {

  block.get(pTile).shape=(int)random(0, 5);
  float q=(int)random(0, 2);
  if (q==1) block.get(pTile).mirror=true;
  else block.get(pTile).mirror=false;
}

void keyReleased() {

  if (keyCode==LEFT) block.get(pTile).x-=block.get(pTile).dim;
  else if (keyCode==RIGHT) block.get(pTile).x+=block.get(pTile).dim;
  keyCode=ENTER;
}

void printTile() {

  if (block.get(pTile).shape==0) {
    if (block.get(pTile).y==height-(LShape(block.get(pTile), block.get(pTile).x, block.get(pTile).y, block.get(pTile).mirror))*block.get(pTile).dim) {
      block.get(pTile).y=height-block.get(pTile).dim;
      pTile++;
      block.add(new Blocks());
      blockSetup();
    }
  } else if (block.get(pTile).shape==1) {
    if (block.get(pTile).y==height-(QShape(block.get(pTile), block.get(pTile).x, block.get(pTile).y))*block.get(pTile).dim) {
      block.get(pTile).y=height-block.get(pTile).dim;
      pTile++;
      block.add(new Blocks());
      blockSetup();
    }
  } else if (block.get(pTile).shape==2) {
    if (block.get(pTile).y==height-(RShape(block.get(pTile), block.get(pTile).x, block.get(pTile).y))*block.get(pTile).dim) {
      block.get(pTile).y=height-block.get(pTile).dim;
      pTile++;
      block.add(new Blocks());
      blockSetup();
    }
  } else if (block.get(pTile).shape==3) {
    if (block.get(pTile).y==height-(SShape(block.get(pTile), block.get(pTile).x, block.get(pTile).y, block.get(pTile).mirror))*block.get(pTile).dim) {
      block.get(pTile).y=height-block.get(pTile).dim;
      pTile++;
      block.add(new Blocks());
      blockSetup();
    }
  } else if (block.get(pTile).shape==4) {
    if (block.get(pTile).y==height-(TShape(block.get(pTile), block.get(pTile).x, block.get(pTile).y))*block.get(pTile).dim) {
      block.get(pTile).y=height-block.get(pTile).dim;
      pTile++;
      block.add(new Blocks());
      blockSetup();
    }
  }

  block.get(pTile).y+=block.get(pTile).dim;
}

void draw() {

  printTile();
}

```

```auto
//class and shapes declaration

class Blocks {

  final int dim=20;
  int x=width/2, y;
  float shape;
  boolean mirror;
  PShape s=createShape(RECT, 0, 0, dim, dim);
}

int LShape (Blocks b, int i, int j, boolean dir) {

  shape(b.s, i, j);
  shape(b.s, i, j+b.dim);
  shape(b.s, i, j+(2*b.dim));
  if (dir==true) //left-sided
    shape(b.s, i-b.dim, j+(2*b.dim));
  else if (dir==false) //right-sided
    shape(b.s, i+b.dim, j+(2*b.dim));
  return 2;
}

int QShape (Blocks b, int i, int j) {

  shape(b.s, i, j);
  shape(b.s, i+b.dim, j);
  shape(b.s, i, j+b.dim);
  shape(b.s, i+b.dim, j+b.dim);
  return 1;
}

int RShape (Blocks b, int i, int j) {

  shape(b.s, i, j);
  shape(b.s, i, j+b.dim);
  shape(b.s, i, j+(2*b.dim));
  shape(b.s, i, j+(3*b.dim));
  return 3;
}

int SShape (Blocks b, int i, int j, boolean dir) {

  if (dir==true) { //left-sided
    shape(b.s, i, j);
    shape(b.s, i, j+b.dim);
    shape(b.s, i-b.dim, j+b.dim);
    shape(b.s, i-b.dim, j+(2*b.dim));
  }
  else if (dir==false) { //right-sided
    shape(b.s, i-b.dim, j);
    shape(b.s, i-b.dim, j+b.dim);
    shape(b.s, i, j+b.dim);
    shape(b.s, i, j+(2*b.dim));
  }
  return 2;
}

int TShape (Blocks b, int i, int j) {
  
  shape(b.s, i, j);
  shape(b.s, i-b.dim, j+b.dim);
  shape(b.s, i, j+b.dim);
  shape(b.s, i+b.dim, j+b.dim);
  return 1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 29, 2019, 4:44pm UTC](https://discourse.processing.org/t/making-of-tetris/15024/2 "2019-10-29T16:44:51Z")

</div>

I don’t know the structure of your program

but you could have the actual _Tetris game field_ as a grid and once a shape has stopped, copy it into the grid. Then display this grid throughout.

After copying the old shape that landed, start with a new shape.

Chrisir
