Tetris Game In JAVA


Welcome back guys sorry for the delay but I am back with the new project "Tetris Game" an old school, so here are the steps to create this game 

P.S.: Use Netbeans compiler

To download Netbeans click here

If you guys want to download this file click here

Steps 

1.) Create a new project and name it "Tetris".

2.) Create eight classes and name it "TetrisFrame","TetrisPanel","TetrisShape1","TetrisShape2","TetrisShape3","TetrisShape4","TetrisShape5" as shown in figure.



3.) Source Code for these classes is given below respectively.



For class TetrisFrame 


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;



import javax.swing.JFrame;



public class TretisFrame extends JFrame{
private static final long serialVersionUID = 1L;
public TretisPanel pan;
public Dimension dim;
int locX;
int locY;
public TretisFrame() throws InterruptedException{
dim = Toolkit.getDefaultToolkit().getScreenSize();
locX=(int) dim.getWidth()*4/12;//this is to set the game into the center of the screen
locY=(int) dim.getHeight()*2/12;
pan = new TretisPanel();
this.setTitle("TretisFrame");
this.setSize(326,585);
this.setLocation(locX, locY);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setFocusable(true);
this.setResizable(false);
this.add(pan);
addKeyListener(pan);
}
public static void main(String[] args) throws InterruptedException{
new TretisFrame();
}
}


For class TetrisPanel

import java.awt.Color;
import java.awt.Graphics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer; // not used the timer, but did use it for debuging


public class TretisPanel extends JPanel implements KeyListener, ActionListener{

private static final long serialVersionUID = 1L;
Tretiscontrol con;
public static int spin, num=1,yNum=1,xNum =1,offSet,offSet2,dropDownTo=500;
public static boolean shapeEnd,reached=false,onSide=true,firstShape=false;

public int iPlus20,jPlus20;
TretisShape1 shape1;
TretisShape2 shape2;
TretisShape3 shape3;
TretisShape4 shape4;
TretisShape5 shape5;
Timer time;

public TretisPanel() throws InterruptedException{
con = new Tretiscontrol();
shape1 = new TretisShape1();
shape2 = new TretisShape2();
shape3 = new TretisShape3();
shape4 = new TretisShape4();
shape5 = new TretisShape5();
spin=0;
this.setSize(350,300);
this.setVisible(true);
addKeyListener(this);
this.setFocusable(true);
this.requestFocus();
time = new Timer(30,this);
time.start();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,350,560);
for(int i = 0;i<375;i++){System.out.println();}     //put this in just to slow the program down, without spending to much time, as its a demo.
for(int i = 1;i<27;i++){
iPlus20=i*20;
for(int j = 1;j<15;j++){
jPlus20=j*20;
//*****************  Inside the two for loops, five different colours of each Shape for each element of the Array and white for background.  ********************//
if(con.cont[i][j]==0){
g.setColor(Color.white);
}
if(con.cont[i][j]==1){
g.setColor(Color.cyan);
}
if(con.cont[i][j]==2){
g.setColor(Color.red);
}
if(con.cont[i][j]==3){
g.setColor(Color.yellow);
}
if(con.cont[i][j]==4){
g.setColor(Color.green);
}
if(con.cont[i][j]==5){
g.setColor(Color.blue);
}
if(con.cont[i][j]==100){
g.setColor(Color.white);
}
//******************  ********************************************   ******************************************************//
g.fillRect(jPlus20, iPlus20, 20, 20);
}
}
if(num==1){
TretisShape1.num=1;
shape1.paint(g);
TretisPanel.shapeEnd=false;
con.checkRow1();
if(shape1.onSide==false){con.checkCols7(xNum);}    
if(shape1.onSide==true){con.checkCols4(xNum);}  
if(reached==true){ 
if(shape1.onSide==false){
con.cont[yNum-2][xNum]=Tretiscontrol.colr;
con.cont[yNum-1][xNum]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
}
if(shape1.onSide==true){
con.cont[yNum][xNum-2]=Tretiscontrol.colr;
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
}
}
}
if(num==2){
   TretisShape2.num=2;
shape2.paint(g);
TretisPanel.shapeEnd=false;
con.checkRow1();
if(shape2.onSide==1){con.checkCols2(xNum);}
if(shape2.onSide==2){con.checkCols4(xNum);}
if(shape2.onSide==3){con.checkCols6(xNum);}
if(shape2.onSide==4){con.checkCols3(xNum);}
//con.cont[1][9]=Tretiscontrol.colr;
if(reached==true){                                 //this draws the shape where it lands on the panel and sets the array.
if(shape2.onSide==1){
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum+2][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum+1]=Tretiscontrol.colr;
}
                                 //this draws the shape where it lands on the panel and sets the array.
if(shape2.onSide==2){
con.cont[yNum+1][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum-2]=Tretiscontrol.colr;
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
}
                               //this draws the shape where it lands on the panel and sets the array.
if(shape2.onSide==3){
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum][xNum+1]=Tretiscontrol.colr;
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
}
                                //this draws the shape where it lands on the panel and sets the array.
if(shape2.onSide==4){
con.cont[yNum-2][xNum]=Tretiscontrol.colr;
con.cont[yNum-1][xNum]=Tretiscontrol.colr;   //on its back pointing up
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum-1][xNum-1]=Tretiscontrol.colr;
}
}
}
if(num==3){
TretisShape3.num=3;
shape3.paint(g);
TretisPanel.shapeEnd=false;
con.checkRow1();
con.checkCols8(xNum);
//con.cont[1][9]=Tretiscontrol.colr;
if(reached==true){             
if(shape3.onSide==true){                          //this draws the shape where it lands on the panel and sets the array.
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
}
if(shape3.onSide==false){                          //this draws the shape where it lands on the panel and sets the array.
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
}
}
}
if(num==4){
TretisShape4.num=4;
shape4.paint(g);
TretisPanel.shapeEnd=false;
con.checkRow1();
if(shape4.onSide==false){con.checkCols9(xNum);}
if(shape4.onSide==true){con.checkCols1(xNum);}
//con.cont[1][9]=Tretiscontrol.colr;
if(reached==true){                                 //this draws the shape where it lands on the panel and sets the array.
if(shape4.onSide==true){
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum][xNum+1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum-1]=Tretiscontrol.colr;
}
if(shape4.onSide==false){
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum-1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum+2][xNum]=Tretiscontrol.colr;
}
}
}
if(num==5){
TretisShape5.num=5;
shape5.paint(g);
TretisPanel.shapeEnd=false;
con.checkRow1();
if(shape5.onSide==false){con.checkCols2(xNum);}
if(shape5.onSide==true){con.checkCols10(xNum);}
if(reached==true){                                 //this draws the shape where it lands on the panel and sets the array.
if(shape5.onSide==true){
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum][xNum]=Tretiscontrol.colr;
con.cont[yNum+1][xNum+1]=Tretiscontrol.colr;
con.cont[yNum][xNum-1]=Tretiscontrol.colr;
}
if(shape5.onSide==false){
con.cont[yNum][xNum+1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum+1]=Tretiscontrol.colr;
con.cont[yNum+1][xNum]=Tretiscontrol.colr;
con.cont[yNum+2][xNum]=Tretiscontrol.colr;
}
}
}
repaint();
g.dispose();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(shape1.onSide==false){
if(shape1.x3>20){         
shape1.x1=shape1.x1-20;
shape1.x2=shape1.x2-20;
shape1.x3=shape1.x3-20;
}
}
if(shape1.onSide==true){
if(shape1.x3>60){         
shape1.x1=shape1.x1-20;
shape1.x2=shape1.x2-20;
shape1.x3=shape1.x3-20;
}
}
/*
*      Shape 2 has to have different cooriinates for each position.
*/
if(shape2.onSide==1){
if(shape2.x3>20){
shape2.x1=shape2.x1-20;
shape2.x2=shape2.x2-20;
shape2.x3=shape2.x3-20;
shape2.x4=shape2.x4-20;
}
}
if(shape2.onSide==2){
if(shape2.x3>60){
shape2.x1=shape2.x1-20;
shape2.x2=shape2.x2-20;
shape2.x3=shape2.x3-20;
shape2.x4=shape2.x4-20;
}
}
if(shape2.onSide==3){
if(shape2.x3>40){
shape2.x1=shape2.x1-20;
shape2.x2=shape2.x2-20;
shape2.x3=shape2.x3-20;
shape2.x4=shape2.x4-20;
}
}
if(shape2.onSide==4){
if(shape2.x3>40){
shape2.x1=shape2.x1-20;
shape2.x2=shape2.x2-20;
shape2.x3=shape2.x3-20;
shape2.x4=shape2.x4-20;
}
}
if(shape3.x3>40){
shape3.x1=shape3.x1-20;
shape3.x2=shape3.x2-20;
shape3.x3=shape3.x3-20;
shape3.x4=shape3.x4-20;
}
if(shape4.onSide==true){
if(shape4.x3>40){
shape4.x1=shape4.x1-20;
shape4.x2=shape4.x2-20;
shape4.x3=shape4.x3-20;
shape4.x4=shape4.x4-20;
}
}
if(shape4.onSide==false){
if(shape4.x3>40){
shape4.x1=shape4.x1-20;
shape4.x2=shape4.x2-20;
shape4.x3=shape4.x3-20;
shape4.x4=shape4.x4-20;
}
}
if(shape5.onSide==true){
if(shape5.x3>40){
shape5.x1=shape5.x1-20;
shape5.x2=shape5.x2-20;
shape5.x3=shape5.x3-20;
shape5.x4=shape5.x4-20;
}
}
if(shape5.onSide==false){
if(shape5.x3>20){
shape5.x1=shape5.x1-20;
shape5.x2=shape5.x2-20;
shape5.x3=shape5.x3-20;
shape5.x4=shape5.x4-20;
}
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(shape1.x3<261){
shape1.x1=shape1.x1+20;
shape1.x2=shape1.x2+20;
shape1.x3=shape1.x3+20;
}
if(shape2.onSide==1){
if(shape2.x3<260){
shape2.x1=shape2.x1+20;
shape2.x2=shape2.x2+20;
shape2.x3=shape2.x3+20;
shape2.x4=shape2.x4+20;
}
}
if(shape2.onSide==2){
if(shape2.x3<280){
shape2.x1=shape2.x1+20;
shape2.x2=shape2.x2+20;
shape2.x3=shape2.x3+20;
shape2.x4=shape2.x4+20;
}
}
if(shape2.onSide==3){
if(shape2.x3<260){
shape2.x1=shape2.x1+20;
shape2.x2=shape2.x2+20;
shape2.x3=shape2.x3+20;
shape2.x4=shape2.x4+20;
}
}
if(shape2.onSide==4){
if(shape2.x3<280){
shape2.x1=shape2.x1+20;
shape2.x2=shape2.x2+20;
shape2.x3=shape2.x3+20;
shape2.x4=shape2.x4+20;
}
}
if(shape3.x3<261){
shape3.x1=shape3.x1+20;
shape3.x2=shape3.x2+20;
shape3.x3=shape3.x3+20;
shape3.x4=shape3.x4+20;
}
if(shape4.onSide==true){
if(shape4.x3<260){
shape4.x1=shape4.x1+20;
shape4.x2=shape4.x2+20;
shape4.x3=shape4.x3+20;
shape4.x4=shape4.x4+20;
}
}
if(shape4.onSide==false){
if(shape4.x3<280){
shape4.x1=shape4.x1+20;
shape4.x2=shape4.x2+20;
shape4.x3=shape4.x3+20;
shape4.x4=shape4.x4+20;
}
}
if(shape5.onSide==true){
if(shape5.x3<260){
shape5.x1=shape5.x1+20;
shape5.x2=shape5.x2+20;
shape5.x3=shape5.x3+20;
shape5.x4=shape5.x4+20;
}
}
if(shape5.onSide==false){
if(shape5.x3<260){
shape5.x1=shape5.x1+20;
shape5.x2=shape5.x2+20;
shape5.x3=shape5.x3+20;
shape5.x4=shape5.x4+20;
}
}
}
if(e.getKeyCode()==KeyEvent.VK_UP){
++spin;
if(spin==1||spin==3){
if(TretisPanel.shapeEnd==false){
shape1.onSide=false;
shape3.onSide=false;
shape4.onSide=false;
shape5.onSide=false;
shape1.offSet=shape1.y2;         //offSet for 1
shape1.offSet2=shape1.x2;
shape4.offSet=shape4.y3;
shape4.offSet2=shape4.x3;
shape5.offSet=shape5.y3;
shape5.offSet2=shape5.x3;
if(shape4.onSide==false){
shape4.shape();
}
if(shape1.onSide==false){
shape1.bar();
}
if(shape1.onSide==false){
shape3.square();
}
if(shape5.onSide==false){
shape5.shape();
}
}
System.out.println(spin);
}
if(spin==2||spin==4){
if(TretisPanel.shapeEnd==false){
shape1.onSide=true;
shape3.onSide=true;
shape4.onSide=true;
shape5.onSide=true;
shape1.offSet=shape1.y2;
shape1.offSet2=shape1.x2;
shape4.offSet=shape4.y3;
shape4.offSet2=shape4.x3;
shape5.offSet=shape5.y3;
shape5.offSet2=shape5.x3;
if(shape1.onSide==true){
shape1.barOnItsSide();
}
if(shape1.onSide==true){
shape3.square();
}
if(shape4.onSide==true){
shape4.shapeOnItsSide();
}
if(shape5.onSide==true){
shape5.shapeOnItsSide();
}
}
}
System.out.println(" spin : "+spin);
//shape2.onSide=1;
if(firstShape==true){spin=2;} //bug fix
if(spin==1&&spin !=3){
System.out.println("   inside it now!");
if(TretisPanel.shapeEnd==false){
shape2.onSide=1;
shape2.offSet=shape2.y2;
shape2.offSet2=shape2.x2;
//shape2.shapeTeeUp();
shape2.shapeTeeRight();
}
}
if(spin==2&&spin !=4){
firstShape=false;  //bug fix
if(TretisPanel.shapeEnd==false){
shape2.onSide=2;
shape2.offSet=shape2.y2;
shape2.offSet2=shape2.x2;
//shape2.shapeTeeRight();
shape2.shapeTeeUp();
}
}
if(spin==3&&spin !=1){
if(TretisPanel.shapeEnd==false){
shape2.onSide=3;
shape2.offSet=shape2.y2;
shape2.offSet2=shape2.x2;
shape2.shapeTeeBottom();
}
}
if(spin==4&&spin !=2){
if(TretisPanel.shapeEnd==false){
shape2.onSide=4;
shape2.offSet=shape2.y2;
shape2.offSet2=shape2.x2;
shape2.shapeTeeLeft();
spin=0;
}
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}

For class TetrisShape1

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TretisShape1 implements ActionListener {
public int x1,x2,x3,y1,y2,y3;
public int offSet,offSet2,dropDownTo;
public boolean onSide;
public static int num=0;

Timer time;

public TretisShape1(){
onSide=true;
if(TretisPanel.spin!=0){TretisPanel.spin=0;}
time = new Timer(50,this);
time.stop();// elements 6 7 8 
coordinates();
}
public void coordinates(){
//onSide=true;
x1=120;
x2=140;   
x3=160;
y1=60;
y2=60;   
y3=60;
this.dropDownTo=500;
// bug fix for spin 
if(TretisPanel.spin==1){TretisPanel.spin=2;}
if(TretisPanel.spin==3){TretisPanel.spin=0;}
if(onSide==false){
this.onSide=true;
}
TretisPanel.offSet = this.y2;
TretisPanel.offSet2=this.x2;
}
public void paint(Graphics g){
if(onSide==false){
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=dropDownTo+20){TretisPanel.shapeEnd=true;TretisPanel.num=2;coordinates();}
}
if(onSide==true){
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=dropDownTo+20){TretisPanel.shapeEnd=true;TretisPanel.num=2;coordinates();}
}
if(y1<dropDownTo+20){
TretisPanel.reached=false; 
y1=y1+1;
y2=y2+1;
y3=y3+1;
}
if(y1>=this.dropDownTo+20){
TretisPanel.reached =true;
}
g.setColor(Color.cyan);
Tretiscontrol.colr=1;
g.fillRect(x1, y1, 20, 20);
g.fillRect(x2, y2, 20, 20);
g.fillRect(x3, y3, 20, 20);
checkColumn();
checkRow();
}
public void bar(){
x1=offSet2;
x2=offSet2;
x3=offSet2; // x1 and y1 are offSet
y1=offSet+20;
y2=offSet;                 
y3=offSet-20;
}
public void barOnItsSide(){
x1=offSet2-20;
x2=offSet2;                 
x3=offSet2+20;
y1=offSet;
y2=offSet;
y3=offSet;
}
public void checkColumn(){
if(onSide==false){
switch(x3){
case 20 : TretisPanel.xNum = 1;break;
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
if(onSide==true){
switch(x3){
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
}
public void checkRow(){
switch(y1){
case 40 : TretisPanel.yNum = 2;break;
case 60 : TretisPanel.yNum = 3;break;
case 80 : TretisPanel.yNum = 4;break;
case 100: TretisPanel.yNum = 5;break;
case 120: TretisPanel.yNum = 6;break;
case 140: TretisPanel.yNum = 7;break;
case 160: TretisPanel.yNum = 8;break;
case 180: TretisPanel.yNum = 9;break;
case 200: TretisPanel.yNum = 10;break;
case 220: TretisPanel.yNum = 11;break;
case 240: TretisPanel.yNum = 12;break;
case 260: TretisPanel.yNum = 13;break;
case 280 : TretisPanel.yNum = 14;break;
case 300 : TretisPanel.yNum = 15;break;
case 320: TretisPanel.yNum = 16;break;
case 340: TretisPanel.yNum = 17;break;
case 360: TretisPanel.yNum = 18;break;
case 380: TretisPanel.yNum = 19;break;
case 400: TretisPanel.yNum = 20;break;
case 420: TretisPanel.yNum = 21;break;
case 440: TretisPanel.yNum = 22;break;
case 460: TretisPanel.yNum = 23;break;
case 480: TretisPanel.yNum = 24;break;
case 500: TretisPanel.yNum = 25;break;
case 520: TretisPanel.yNum = 26;break;
}
}
@Override
/*
 * 
 * 
 * (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub


}
}

For class TetrisShape2

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TretisShape2 implements ActionListener {
public int x1,x2,x3,x4,y1,y2,y3,y4,onSide;
public int offSet,offSet2,dropDownTo;
public static boolean pointTop,pointBottom,pointLeft,pointRight;
public static int num=0;

Timer time;

public TretisShape2(){
coordinates();
time = new Timer(40,this);
time.stop();
}
public void coordinates(){
this.onSide=1;
x1=120;   //x2 and y2 is the offSet of 120
x2=120;
x3=120;
x4=140;
y1=100; // elements 6 an 7
y2=120;
y3=140;
y4=120;
this.dropDownTo=500;
TretisPanel.offSet=this.y2;
TretisPanel.offSet2=this.x2;
}
public void paint(Graphics g){
if(TretisPanel.spin==0){TretisPanel.firstShape=true;}   //bug fix
g.setColor(Color.red);
Tretiscontrol.colr=2;
if(onSide==1){
this.dropDownTo=TretisPanel.dropDownTo-20;
if(y1>=dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=3;coordinates();}
}
if(onSide==2){
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=3;coordinates();}
}
if(onSide==3){
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=3;coordinates();}
}
if(onSide==4){
this.dropDownTo=TretisPanel.dropDownTo+20;
if(y1>=dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=3;coordinates();}
}
if(y1<dropDownTo){
TretisPanel.reached=false; 
y1=y1+1;
y2=y2+1;
y3=y3+1;
y4=y4+1;
}
if(y1>=this.dropDownTo){
TretisPanel.reached =true;
}
g.fillRect(x1, y1, 20, 20);
g.fillRect(x2, y2, 20, 20);
g.fillRect(x3, y3, 20, 20);
g.fillRect(x4, y4, 20, 20);
checkColumn();
checkRow();

}
public void shapeTeeRight(){
x1=offSet2;
x2=offSet2;
x3=offSet2;
x4=offSet2+20;
y1=offSet-20;
y2=offSet;
y3=offSet+20;
y4=offSet;
}

public void shapeTeeBottom(){
x1=offSet2-20;
x2=offSet2;
x3=offSet2;
x4=offSet2+20;
y1=offSet;
y2=offSet;
y3=offSet+20;
y4=offSet;
}
public void shapeTeeLeft(){
x1=offSet2;
x2=offSet2;
x3=offSet2;
x4=offSet2-20;
y1=offSet+20;
y2=offSet;
y3=offSet-20;
y4=offSet;
}
public void shapeTeeUp(){
x1=offSet2;
x2=offSet2;
x3=offSet2+20;
x4=offSet2-20;
y1=offSet-20;
y2=offSet;
y3=offSet;
y4=offSet;
}
public void checkColumn(){
switch(x3){
case 20 : TretisPanel.xNum = 1;break;
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
public void checkRow(){
switch(y1){
case 40 : TretisPanel.yNum = 2;break;
case 60 : TretisPanel.yNum = 3;break;
case 80 : TretisPanel.yNum = 4;break;
case 100: TretisPanel.yNum = 5;break;
case 120: TretisPanel.yNum = 6;break;
case 140: TretisPanel.yNum = 7;break;
case 160: TretisPanel.yNum = 8;break;
case 180: TretisPanel.yNum = 9;break;
case 200: TretisPanel.yNum = 10;break;
case 220: TretisPanel.yNum = 11;break;
case 240: TretisPanel.yNum = 12;break;
case 260: TretisPanel.yNum = 13;break;
case 280 : TretisPanel.yNum = 14;break;
case 300 : TretisPanel.yNum = 15;break;
case 320: TretisPanel.yNum = 16;break;
case 340: TretisPanel.yNum = 17;break;
case 360: TretisPanel.yNum = 18;break;
case 380: TretisPanel.yNum = 19;break;
case 400: TretisPanel.yNum = 20;break;
case 420: TretisPanel.yNum = 21;break;
case 440: TretisPanel.yNum = 22;break;
case 460: TretisPanel.yNum = 23;break;
case 480: TretisPanel.yNum = 24;break;
case 500: TretisPanel.yNum = 25;break;
case 520: TretisPanel.yNum = 26;break;
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}

For TetrisShape3



import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TretisShape3 implements ActionListener {
public int x1,x2,x3,x4,y1,y2,y3,y4;
public int offSet,offSet2;
public int dropDownTo;
public boolean onSide;
public static int num=0;

Timer time;

public TretisShape3(){ // 7 and 8 elements
onSide=true;
coordinates();
time = new Timer(30,this);
time.stop();
}
public void paint(Graphics g){
g.setColor(Color.yellow);
Tretiscontrol.colr=3;
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=this.dropDownTo){TretisPanel.num=4;coordinates();}


if(y1<this.dropDownTo){
TretisPanel.reached=false; 
y1=y1+1;
y2=y2+1;
y3=y3+1;
y4=y4+1;
}
if(y1>=this.dropDownTo){
TretisPanel.reached =true;
}
g.fillRect(x1, y1, 20, 20);
g.fillRect(x2, y2, 20, 20);
g.fillRect(x3, y3, 20, 20);
g.fillRect(x4, y4, 20, 20);
checkColumn();
checkRow();

}
public void coordinates(){
x1=120;
x2=140;
x3=140;
x4=120;
y1=100;
y2=100;
y3=120;
y4=120;
this.dropDownTo=500;
if(onSide==false){
this.onSide=true;
}
}
public void square(){
coordinates();
}
public void checkColumn(){
switch(x3){
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
public void checkRow(){
switch(y1){
case 40 : TretisPanel.yNum = 2;break;
case 60 : TretisPanel.yNum = 3;break;
case 80 : TretisPanel.yNum = 4;break;
case 100: TretisPanel.yNum = 5;break;
case 120: TretisPanel.yNum = 6;break;
case 140: TretisPanel.yNum = 7;break;
case 160: TretisPanel.yNum = 8;break;
case 180: TretisPanel.yNum = 9;break;
case 200: TretisPanel.yNum = 10;break;
case 220: TretisPanel.yNum = 11;break;
case 240: TretisPanel.yNum = 12;break;
case 260: TretisPanel.yNum = 13;break;
case 280 : TretisPanel.yNum = 14;break;
case 300 : TretisPanel.yNum = 15;break;
case 320: TretisPanel.yNum = 16;break;
case 340: TretisPanel.yNum = 17;break;
case 360: TretisPanel.yNum = 18;break;
case 380: TretisPanel.yNum = 19;break;
case 400: TretisPanel.yNum = 20;break;
case 420: TretisPanel.yNum = 21;break;
case 440: TretisPanel.yNum = 22;break;
case 460: TretisPanel.yNum = 23;break;
case 480: TretisPanel.yNum = 24;break;
case 500: TretisPanel.yNum = 25;break;
case 520: TretisPanel.yNum = 26;break;
}
}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

For class TetrisShape4

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class TretisShape4 implements ActionListener {

public int x1,x2,x3,x4,y1,y2,y3,y4,spin;
public int offSet,offSet2,dropDownTo;
public boolean onSide,standingOffset=false;
public static int num=0;
Timer time;
public TretisShape4(){
onSide=true;
coordinates();
time = new Timer(20,this);
time.stop();
}
public void coordinates(){
x1=140; // elements 6 7 8
x2=160;
x3=140;
x4=120;
y1=40;
y2=40;
y3=60;
y4=60;
this.dropDownTo=500;
if(onSide==false){
this.onSide=true;
}
TretisPanel.offSet=this.y3;
TretisPanel.offSet2=this.x3;
}
public void paint(Graphics g){
g.setColor(Color.green);
Tretiscontrol.colr=4;
if(onSide==false){
this.dropDownTo=TretisPanel.dropDownTo-20; 
if(y1>=this.dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=5;coordinates();}
}
if(onSide==true){          
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=this.dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=5;coordinates();}
}
if(y1<this.dropDownTo){
TretisPanel.reached=false;       //this is for drawing
y1=y1+1;
y2=y2+1;
y3=y3+1;
y4=y4+1;
}
if(y1>=this.dropDownTo){
TretisPanel.reached =true;
}
g.fillRect(x1, y1, 20, 20);
g.fillRect(x2, y2, 20, 20);
g.fillRect(x3, y3, 20, 20);
g.fillRect(x4, y4, 20, 20);
checkColumn();
checkRow();
}
public void shapeOnItsSide(){
standingOffset=false;
x1=offSet2;
x2=offSet2+20;
x3=offSet2; //offSet x3 and y3
x4=offSet2-20;
y1=offSet-20;
y2=offSet-20;
y3=offSet;//120
y4=offSet;
}
public void shape(){
standingOffset=true;
x1=offSet2-20;
x2=offSet2-20;
x3=offSet2;
x4=offSet2;
y1=offSet-20;
y2=offSet;
y3=offSet;
y4=offSet+20;
}
public void checkColumn(){
if(onSide==true){
switch(x3){
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
}
}
if(onSide==false){
switch(x3){
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
}
public void checkRow(){
switch(y1){
case 40 : TretisPanel.yNum = 2;break;
case 60 : TretisPanel.yNum = 3;break;
case 80 : TretisPanel.yNum = 4;break;
case 100: TretisPanel.yNum = 5;break;
case 120: TretisPanel.yNum = 6;break;
case 140: TretisPanel.yNum = 7;break;
case 160: TretisPanel.yNum = 8;break;
case 180: TretisPanel.yNum = 9;break;
case 200: TretisPanel.yNum = 10;break;
case 220: TretisPanel.yNum = 11;break;
case 240: TretisPanel.yNum = 12;break;
case 260: TretisPanel.yNum = 13;break;
case 280 : TretisPanel.yNum = 14;break;
case 300 : TretisPanel.yNum = 15;break;
case 320: TretisPanel.yNum = 16;break;
case 340: TretisPanel.yNum = 17;break;
case 360: TretisPanel.yNum = 18;break;
case 380: TretisPanel.yNum = 19;break;
case 400: TretisPanel.yNum = 20;break;
case 420: TretisPanel.yNum = 21;break;
case 440: TretisPanel.yNum = 22;break;
case 460: TretisPanel.yNum = 23;break;
case 480: TretisPanel.yNum = 24;break;
case 500: TretisPanel.yNum = 25;break;
case 520: TretisPanel.yNum = 26;break;
}
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

For class TetrisShape5

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TretisShape5 implements ActionListener {
Timer time;
public int x1,x2,x3,x4,y1,y2,y3,y4;
public int offSet,offSet2,dropDownTo;
public boolean onSide,standingOffset=false;

public static int num=0;

public TretisShape5(){
onSide=true;
coordinates();
time = new Timer(10,this);
time.stop();
}
public void coordinates(){
x1=120;
x2=140;
x3=140;
x4=160;
y1=40; // elements 6 7 8
y2=40;
y3=60;
y4=60;
this.dropDownTo=500;
if(onSide==false){
this.onSide=true;
}
TretisPanel.offSet2=this.x3;
TretisPanel.offSet=this.y3;
}
public void paint(Graphics g){
if(onSide==false){
this.dropDownTo=TretisPanel.dropDownTo-20;
if(y1>=this.dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=1;coordinates();}
}
if(onSide==true){ 
this.dropDownTo=TretisPanel.dropDownTo;
if(y1>=this.dropDownTo){TretisPanel.shapeEnd=true;TretisPanel.num=1;coordinates();}
}
if(y1<this.dropDownTo){
TretisPanel.reached=false;
y1=y1+1;
y2=y2+1;
y3=y3+1;
y4=y4+1;

}
if(y1>=this.dropDownTo){
TretisPanel.reached =true;
}
g.setColor(Color.blue);
Tretiscontrol.colr=5;
//Tretiscontrol.col=x3;
//Tretiscontrol.row=this.dropDownTo;
int nums=0;
g.fillRect(x1, y1, 20, 20);
g.fillRect(x2, y2, 20, 20);
g.fillRect(x3, y3, 20, 20);
g.fillRect(x4, y4, 20, 20);
checkColumn();
checkRow();
}
public void shape(){
standingOffset=true;
x1=offSet2+20;
x2=offSet2+20;
x3=offSet2;
x4=offSet2;
y1=offSet-40;
y2=offSet-20;
y3=offSet;
y4=offSet-20;
}
public void shapeOnItsSide(){
standingOffset=false;
x1=offSet2-20;
x2=offSet2;
x3=offSet2;
x4=offSet2+20;
y1=offSet-20;
y2=offSet-20;
y3=offSet;
y4=offSet;
}
public void checkColumn(){
if(onSide==true){
switch(x3){
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
}
}
if(onSide==false){
switch(x3){
case 20 : TretisPanel.xNum = 1;break;
case 40 : TretisPanel.xNum = 2;break;
case 60 : TretisPanel.xNum = 3;break;
case 80 : TretisPanel.xNum = 4;break;
case 100: TretisPanel.xNum = 5;break;
case 120: TretisPanel.xNum = 6;break;
case 140: TretisPanel.xNum = 7;break;
case 160: TretisPanel.xNum = 8;break;
case 180: TretisPanel.xNum = 9;break;
case 200: TretisPanel.xNum = 10;break;
case 220: TretisPanel.xNum = 11;break;
case 240: TretisPanel.xNum = 12;break;
case 260: TretisPanel.xNum = 13;break;
case 280: TretisPanel.xNum = 14;break;
}
}
}
public void checkRow(){
switch(y1){
case 40 : TretisPanel.yNum = 2;break;
case 60 : TretisPanel.yNum = 3;break;
case 80 : TretisPanel.yNum = 4;break;
case 100: TretisPanel.yNum = 5;break;
case 120: TretisPanel.yNum = 6;break;
case 140: TretisPanel.yNum = 7;break;
case 160: TretisPanel.yNum = 8;break;
case 180: TretisPanel.yNum = 9;break;
case 200: TretisPanel.yNum = 10;break;
case 220: TretisPanel.yNum = 11;break;
case 240: TretisPanel.yNum = 12;break;
case 260: TretisPanel.yNum = 13;break;
case 280 : TretisPanel.yNum = 14;break;
case 300 : TretisPanel.yNum = 15;break;
case 320: TretisPanel.yNum = 16;break;
case 340: TretisPanel.yNum = 17;break;
case 360: TretisPanel.yNum = 18;break;
case 380: TretisPanel.yNum = 19;break;
case 400: TretisPanel.yNum = 20;break;
case 420: TretisPanel.yNum = 21;break;
case 440: TretisPanel.yNum = 22;break;
case 460: TretisPanel.yNum = 23;break;
case 480: TretisPanel.yNum = 24;break;
case 500: TretisPanel.yNum = 25;break;
case 520: TretisPanel.yNum = 26;break;
}
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}

For class Tetriscontrol


public class Tretiscontrol{
public int[][] cont;
public static int colr=0,row=1,col=1;
int num1,num2;
private boolean rowCompleted=false;
public boolean test;
public Tretiscontrol(){
num1=0;
num2=0;
test=false;
cont= new int[27][15];//bug add extra one to row from 6 to seven as col 7 was not being accessed.
int num = 0;
for(int row=1;row<27;row++){
for(int col=1;col<15;col++){
num++;
if(num>5)num=num-6;
cont[row][col]=0;
}
}
for(int numb=1;numb<15;numb++){
//cont[5][numb]=2;
}
}
public void checkCols1(int x3){      
int x=x3; //green onits side
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j][x-1]>0||cont[j-1][x+1]>0){
if(test==false){ // 2 x squares chec
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols2(int x3){
int x=x3;                            // red tee shape to the right      and blue standing up
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j-1][x+1]>0){
if(test==false){ // 2 x squares checks
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols3(int x3){
int x=x3;                            // red tee shape to the left
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j-1][x-1]>0){
if(test==false){ // 2 x squares checks
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols4(int x3){      //  red tee up 3 x the same y
int x=x3-1;
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j][x+1]>0||cont[j][x-1]>0){
if(test==false){ //thats  a start
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}

}
public void checkCols5(int x3){      //  blue shape 5 onits side
int x=x3;
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j][x+1]>0||cont[j-1][x-1]>0){
if(test==false){ //thats  a start
num1=j;
test=true;
if(cont[j-1][x-1]>0){
TretisPanel.dropDownTo=((num1-2)*20);
}
else TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols6(int x3){      //  for red tee down shape
int x=x3;
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j-1][x-1]>0||cont[j-1][x+1]>0){
if(test==false){ //thats  a start
num1=j;
test=true;
if(cont[j-1][x-1]>0){
TretisPanel.dropDownTo=((num1-2)*20);
}
else TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}

public void checkCols7(int x3){
int x=x3;                            // bar standing up
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0){
if(test==false){
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols8(int x3){
int x=x3;                            // The square
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j][x-1]>0){
if(test==false){
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols9(int x3){      
int x=x3; //green Stabding up
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j-1][x-1]>0){
if(test==false){ // 2 x squares chec
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
public void checkCols10(int x3){      
int x=x3; //green onits side
test=false;
TretisPanel.dropDownTo=500;
for(int j = 1;j<27;j++){
if(cont[j][x]>0||cont[j][x+1]>0||cont[j-1][x-1]>0){
if(test==false){ // 2 x squares chec
num1=j;
test=true;
TretisPanel.dropDownTo=((num1-2)*20);
}
}
}
}
//check Rows for Completion
int completedRow=1;
public void checkRow1(){
for(int i =1;i<27;i++){
completedRow=0;
for(int j = 1; j <15;j++){
if(cont[i][j]!=0){
completedRow++;
if(completedRow==14){
makeRow100(i);
}
}
}
}
}
int zeroRow = 1;
public void checkCompletedRows(){
if(rowCompleted==true){
for(int i =26;i>0;i--){    //this counts down from 26 to 1
if(cont[i][0]==100){
for(int j =0;j<15;j++){
cont[i][j]=cont[i-1][j];
cont[i-1][0]=100;//////////////////////////////////////////////////////////////////
}
}
}
}
}
public void makeRow100(int row){
int r = row;
for(int num=0;num<15;num++){
cont[r][num]=100;
rowCompleted=true;
}
moveDownTo100(r);
}
//make row zero will have to call move row down again until it reaches 1
public void moveDownTo100(int row){
int r = row;
checkCompletedRows();
}
}


4.) Run the code and enjoy.

For any doubt, query and suggestion just drop a comment in the comment section. 

Peace.



















 

Comments

Popular posts from this blog

Brick Breaker Game in JAVA

How to make C program to convert Fahrenheit into Celsius