color backgroundColor = color(50, 50, 50);
static int gameWidth = 600, gameHeight = 400;
GameObjects block1, block2, block3, block4, wallLeft, WallBottom, wallRight, wallCenter, controller;
wallLeft = new GameObject(0, 0, 19, gameHeight);
wallRight = new GameObject(gameWidth-20, 0, 20, gameHeight);
wallBottom = new GameObject(19, gameHeight-20, gameWidth-39, 20);
wallCenter = new GameObject(20+(10*20), 0, 20, gameHeight-20);
controller = new GameObject(-20, -20, 19, 19);
controller.setAlarm(gameSpeed);
f = createFont("Arial",72,false);
background(backgroundColor);
text("Lines cleared:"+linesCleared, 300, 150);
text("Level:"+level, 300, 200);
if (checkShapeCollision() == true) {
else if (keyCode == RIGHT) {
if (checkShapeCollision() == true) {
else if (keyCode == DOWN) {
if (checkShapeCollision() == true) {
block1.classTag = "static";
block2.classTag = "static";
block3.classTag = "static";
block4.classTag = "static";
controller.setAlarm(gameSpeed);
else if (keyCode == UP) {
int check = checkAnswer();
void alarm(GameObject alarmObject) {
if (alarmObject == controller) {
if (checkShapeCollision() == true) {
block1.classTag = "static";
block2.classTag = "static";
block3.classTag = "static";
block4.classTag = "static";
controller.setAlarm(gameSpeed);
void collision(GameObject moving, GameObject notMoving) {
boolean checkShapeCollision() {
boolean collided = false;
if (checkCollision(block1) != -1 || checkCollision(block2) != -1 || checkCollision(block3) != -1 || checkCollision(block4) != -1) {
for (int row = gameHeight - 40; row >= 0; row -= 20) {
for (int col = 20; col < 20 + (10 * 20); col += 20) {
if (checkCollision(controller) != -1 && objects[checkCollision(controller)].classTag == "static") {
for (int col = 20; col < 20 + (10 * 20); col += 20) {
objects[checkCollision(controller)].destroy();
for (int a = 0; a < arrayIndex; a++) {
if (objects[a].y < row && objects[a].classTag == "static") {
if (linesCleared % 10 == 0) {
gameSpeed = int(gameSpeed * 0.7);
if (gameSpeed <= 0) gameSpeed = 1;
void createTetrisShape() {
shape = int(random(1, 8));
color shapeColor = randomColor();
block1 = new GameObject(20+100, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 20, 19, 19, shapeColor);
block3 = new GameObject(20+100, 40, 19, 19, shapeColor);
block4 = new GameObject(20+100, 60, 19, 19, shapeColor);
else if (shape == BLOCK) {
block1 = new GameObject(20+80, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 0, 19, 19, shapeColor);
block3 = new GameObject(20+80, 20, 19, 19, shapeColor);
block4 = new GameObject(20+100, 20, 19, 19, shapeColor);
else if (shape == T_SHAPE) {
block1 = new GameObject(20+80, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 0, 19, 19, shapeColor);
block3 = new GameObject(20+120, 0, 19, 19, shapeColor);
block4 = new GameObject(20+100, 20, 19, 19, shapeColor);
else if (shape == L_SHAPE) {
block1 = new GameObject(20+100, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 20, 19, 19, shapeColor);
block3 = new GameObject(20+100, 40, 19, 19, shapeColor);
block4 = new GameObject(20+120, 40, 19, 19, shapeColor);
else if (shape == J_SHAPE) {
block1 = new GameObject(20+100, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 20, 19, 19, shapeColor);
block3 = new GameObject(20+100, 40, 19, 19, shapeColor);
block4 = new GameObject(20+80, 40, 19, 19, shapeColor);
else if (shape == S_SHAPE) {
block1 = new GameObject(20+100, 0, 19, 19, shapeColor);
block2 = new GameObject(20+120, 0, 19, 19, shapeColor);
block3 = new GameObject(20+80, 20, 19, 19, shapeColor);
block4 = new GameObject(20+100, 20, 19, 19, shapeColor);
else if (shape == Z_SHAPE) {
block1 = new GameObject(20+80, 0, 19, 19, shapeColor);
block2 = new GameObject(20+100, 0, 19, 19, shapeColor);
block3 = new GameObject(20+100, 20, 19, 19, shapeColor);
block4 = new GameObject(20+120, 20, 19, 19, shapeColor);
block1.classTag = "moving";
block2.classTag = "moving";
block3.classTag = "moving";
block4.classTag = "moving";
if (checkCollision(block1) != -1 || checkCollision(block2) != -1 || checkCollision(block3) != -1 || checkCollision(block4) != -1) {
drawQuestion("Game over! Play again?");
void moveBlocks(int x, int y) {
int randomPick = int(random(1, 8));
} else if (randomPick == 2) {
} else if (randomPick == 3) {
} else if (randomPick == 4) {
} else if (randomPick == 5) {
} else if (randomPick == 6) {
} else if (randomPick == 7) {
block1.x -= 20;block1.y += 20;
block3.x += 20;block3.y -= 20;
block4.x += 40;block4.y -= 40;
if (checkShapeCollision() == true) {
block1.x += 20;block1.y -= 20;
block3.x -= 20;block3.y += 20;
block4.x -= 40;block4.y += 40;
else if (rotation == 1) {
block1.x += 20;block1.y -= 20;
block3.x -= 20;block3.y += 20;
block4.x -= 40;block4.y += 40;
if (checkShapeCollision() == true) {
block1.x -= 20;block1.y += 20;
block3.x += 20;block3.y -= 20;
block4.x += 40;block4.y -= 40;
else if (shape == BLOCK) {
else if (shape == T_SHAPE) {
block1.x += 20;block1.y -= 20;
block3.x -= 20;block3.y += 20;
block4.x -= 20;block4.y -= 20;
if (checkShapeCollision() == true) {
block1.x -= 20;block1.y += 20;
block3.x += 20;block3.y -= 20;
block4.x += 20;block4.y += 20;
else if (rotation == 1) {
block1.x += 20;block1.y += 20;
block3.x -= 20;block3.y -= 20;
block4.x += 20;block4.y -= 20;
if (checkShapeCollision() == true) {
block1.x -= 20;block1.y -= 20;
block3.x += 20;block3.y += 20;
block4.x -= 20;block4.y += 20;
else if (rotation == 2) {
block1.x -= 20;block1.y += 20;
block3.x += 20;block3.y -= 20;
block4.x += 20;block4.y += 20;
if (checkShapeCollision() == true) {
block1.x += 20;block1.y -= 20;
block3.x -= 20;block3.y += 20;
block4.x -= 20;block4.y -= 20;
else if (rotation == 3) {
block1.x -= 20;block1.y -= 20;
block3.x += 20;block3.y += 20;
block4.x -= 20;block4.y += 20;
if (checkShapeCollision() == true) {
block1.x += 20;block1.y += 20;
block3.x -= 20;block3.y -= 20;
block4.x += 20;block4.y -= 20;
static int objectArraySize = 200;
static GameObject[] objects = new GameObject[objectArraySize];
static int arrayIndex = 0;
static int objectId = 100000;
static boolean askingQuestion = false, answer = false;
static int questionX=gameWidth/2-80, questionY=gameHeight/2-40, questionWidth=160, questionHeight=40;
final static int NO_ANSWER = 0;
final static int YES = 1;
if (mouseX >= questionX && mouseX <= questionX+questionWidth/2 && mouseY >= questionY+questionHeight/2 && mouseY <= questionY+questionHeight && askingQuestion == true) {
if (mouseX >= questionX+questionWidth/2 && mouseX <= questionX+questionWidth && mouseY >= questionY+questionHeight/2 && mouseY <= questionY+questionHeight && askingQuestion == true) {
int checkCollision(GameObject test) {
if (getIndex(test) == -1) {
for (int a = 0; a < arrayIndex; a++) {
if (test != objects[a] && test.x <= objects[a].x + objects[a].spriteWidth && objects[a].x <= test.x + test.spriteWidth
&& test.y <= objects[a].y + objects[a].spriteHeight && objects[a].y <= test.y + test.spriteHeight) {
void drawQuestion(String question) {
rect(questionX-2, questionY-2, questionWidth+4, questionHeight+4);
rect(questionX, questionY, questionWidth, questionHeight);
rect(questionX, questionY+questionHeight/2, questionWidth/2, questionHeight/2);
rect(questionX+questionWidth/2, questionY+questionHeight/2, questionWidth/2, questionHeight/2);
textFont(createFont("Arial",72,false), 12);
text(question, questionX+4, questionY+questionHeight-25);
text("Yes", questionX+4, questionY+questionHeight-2);
text("No", questionX+questionWidth/2+4, questionY+questionHeight-2);
for (int a = 0; a < arrayIndex; a++) {
int getIndex(GameObject test) {
for (int a = 0; a < arrayIndex; a++) {
if (test == objects[a]) {
for (int a = 0; a < arrayIndex; a++) {
objects[a].x += cos(radians(objects[a].direction)) * objects[a].speed;
objects[a].y -= sin(radians(objects[a].direction)) * objects[a].speed;
int collidedIndex = checkCollision(objects[a]);
if (collidedIndex != -1) {
if (objects[collidedIndex].solid == true) {
objects[a].x -= cos(radians(objects[a].direction)) * objects[a].speed;
objects[a].y += sin(radians(objects[a].direction)) * objects[a].speed;
collision(objects[a], objects[collidedIndex]);
if (objects[a].alarmTime > 0 && millis() - objects[a].startTime > objects[a].alarmTime) {
color objectColor = color(255, 255, 255);
String classTag = "", idTag = "";
int startTime, alarmTime = -1;
if (arrayIndex >= objectArraySize) {
GameObject[] bigger = new GameObject[objectArraySize];
for (int a = 0; a < arrayIndex; a++) {
objects[arrayIndex++] = this;
GameObject(float nx, float ny) {
GameObject(float nx, float ny, String imagePath) {
GameObject(float nx, float ny, int sW, int sH) {
GameObject(float nx, float ny, int sW, int sH, color c) {
for (int a = 0; a < arrayIndex; a++) {
if (this == objects[a]) {
for (int b = a; b < arrayIndex - 1; b++) {
objects[b] = objects[b + 1];
objects[arrayIndex] = null;
rect(x, y, spriteWidth, spriteHeight);
void move(float newSpeed, float newDirection) {
this.direction = newDirection;
void setAlarm(int timeUntilAlarm) {
alarmTime = timeUntilAlarm;
void setSprite(String imagePath) {
sprite = loadImage(imagePath);
spriteWidth = sprite.width;
spriteHeight = sprite.height;
void setTowards(GameObject destination) {
if (getIndex(destination) != -1) {
direction = degrees(atan2(-destination.y+y, destination.x-x));