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:
//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();
}
//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;
}