Source Code Of Tetris Game import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Tetris extends JFrame implements KeyListener { int pos[] = {0,1}; boolean bottom = false; int n = 20; int m = 10; JButton b[][]; Color tmp[][] = new Color[m][n]; int rand = 0; int centralx = 0; int centraly = 0; int deltax = 0; int perim[][] = new int[m+4][n+4]; or[][][] prof = new or[4][4][7]; Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange}; int rowsclrd = 0; public Tetris(){
29
Embed
world4java.files.wordpress.com · Web viewSource Code Of Tetris Game. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Tetris extends JFrame implements
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Source Code Of Tetris Game
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tetris extends JFrame implements KeyListener {
int pos[] = {0,1};
boolean bottom = false;
int n = 20;
int m = 10;
JButton b[][];
Color tmp[][] = new Color[m][n];
int rand = 0;
int centralx = 0;
int centraly = 0;
int deltax = 0;
int perim[][] = new int[m+4][n+4];
or[][][] prof = new or[4][4][7];
Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange};
int rowsclrd = 0;
public Tetris(){
for(int a = 0;a<4;a++){
for(int b = 0;b<4;b++){
for(int c = 0;c<7;c++){
prof[a][b][c] = new or();
}
}
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
prof[0][0][0].x = -1;
prof[0][1][0].x = 0;
prof[0][2][0].x = 0;
prof[0][3][0].x = 1;
prof[1][0][0].x = 0;
prof[1][1][0].x = 0;
prof[1][2][0].x = -1;
prof[1][3][0].x = -1;
prof[2][0][0].x = 1;
prof[2][1][0].x = 0;
prof[2][2][0].x = 0;
prof[2][3][0].x = -1;
prof[3][0][0].x = 0;
prof[3][1][0].x = 0;
prof[3][2][0].x = 1;
prof[3][3][0].x = 1;
prof[0][0][1].x = -1;
prof[0][1][1].x = -1;
prof[0][2][1].x = 0;
prof[0][3][1].x = 1;
prof[1][0][1].x = -1;
prof[1][1][1].x = 0;
prof[1][2][1].x = 0;
prof[1][3][1].x = 0;
prof[2][0][1].x = 1;
prof[2][1][1].x = 1;
prof[2][2][1].x = 0;
prof[2][3][1].x = -1;
prof[3][0][1].x = 1;
prof[3][1][1].x = 0;
prof[3][2][1].x = 0;
prof[3][3][1].x = 0;
prof[0][0][2].x = -1;
prof[0][1][2].x = 0;
prof[0][2][2].x = 1;
prof[0][3][2].x = 1;
prof[1][0][2].x = 0;
prof[1][1][2].x = 0;
prof[1][2][2].x = 0;
prof[1][3][2].x = -1;
prof[2][0][2].x = 1;
prof[2][1][2].x = 0;
prof[2][2][2].x = -1;
prof[2][3][2].x = -1;
prof[3][0][2].x = 0;
prof[3][1][2].x = 0;
prof[3][2][2].x = 0;
prof[3][3][2].x = 1;
prof[0][0][3].x = -1;
prof[0][1][3].x = 0;
prof[0][2][3].x = 0;
prof[0][3][3].x = 1;
prof[1][0][3].x = -1;
prof[1][1][3].x = -1;
prof[1][2][3].x = 0;
prof[1][3][3].x = 0;
prof[2][0][3].x = 1;
prof[2][1][3].x = 0;
prof[2][2][3].x = 0;
prof[2][3][3].x = -1;
prof[3][0][3].x = 1;
prof[3][1][3].x = 1;
prof[3][2][3].x = 0;
prof[3][3][3].x = 0;
prof[0][0][4].x = -1;
prof[0][1][4].x = 0;
prof[0][2][4].x = 0;
prof[0][3][4].x = 1;
prof[1][0][4].x = 0;
prof[1][1][4].x = 0;
prof[1][2][4].x = -1;
prof[1][3][4].x = 0;
prof[2][0][4].x = 1;
prof[2][1][4].x = 0;
prof[2][2][4].x = 0;
prof[2][3][4].x = -1;
prof[3][0][4].x = 0;
prof[3][1][4].x = 0;
prof[3][2][4].x = 1;
prof[3][3][4].x = 0;
prof[0][0][5].x = 0;
prof[0][1][5].x = 0;
prof[0][2][5].x = 1;
prof[0][3][5].x = 1;
prof[1][0][5].x = 0;
prof[1][1][5].x = 0;
prof[1][2][5].x = 1;
prof[1][3][5].x = 1;
prof[2][0][5].x = 0;
prof[2][1][5].x = 0;
prof[2][2][5].x = 1;
prof[2][3][5].x = 1;
prof[3][0][5].x = 0;
prof[3][1][5].x = 0;
prof[3][2][5].x = 1;
prof[3][3][5].x = 1;
prof[0][0][6].x = -1;
prof[0][1][6].x = 0;
prof[0][2][6].x = 1;
prof[0][3][6].x = 2;
prof[1][0][6].x = 0;
prof[1][1][6].x = 0;
prof[1][2][6].x = 0;
prof[1][3][6].x = 0;
prof[2][0][6].x = 1;
prof[2][1][6].x = 0;
prof[2][2][6].x = -1;
prof[2][3][6].x = -2;
prof[3][0][6].x = 0;
prof[3][1][6].x = 0;
prof[3][2][6].x = 0;
prof[3][3][6].x = 0;
prof[0][0][0].y = 0;
prof[0][1][0].y = 0;
prof[0][2][0].y = 1;
prof[0][3][0].y = 1;
prof[1][0][0].y = -1;
prof[1][1][0].y = 0;
prof[1][2][0].y = 0;
prof[1][3][0].y = 1;
prof[2][0][0].y = 0;
prof[2][1][0].y = 0;
prof[2][2][0].y = -1;
prof[2][3][0].y = -1;
prof[3][0][0].y = 1;
prof[3][1][0].y = 0;
prof[3][2][0].y = 0;
prof[3][3][0].y = -1;
prof[0][0][1].y = 0;
prof[0][1][1].y = 1;
prof[0][2][1].y = 0;
prof[0][3][1].y = 0;
prof[1][0][1].y = -1;
prof[1][1][1].y = -1;
prof[1][2][1].y = 0;
prof[1][3][1].y = 1;
prof[2][0][1].y = -1;
prof[2][1][1].y = 0;
prof[2][2][1].y = 0;
prof[2][3][1].y = 0;
prof[3][0][1].y = 1;
prof[3][1][1].y = 1;
prof[3][2][1].y = 0;
prof[3][3][1].y = -1;
prof[0][0][2].y = 0;
prof[0][1][2].y = 0;
prof[0][2][2].y = 0;
prof[0][3][2].y = 1;
prof[1][0][2].y = -1;
prof[1][1][2].y = 0;
prof[1][2][2].y = 1;
prof[1][3][2].y = 1;
prof[2][0][2].y = 0;
prof[2][1][2].y = 0;
prof[2][2][2].y = 0;
prof[2][3][2].y = -1;
prof[3][0][2].y = 1;
prof[3][1][2].y = 0;
prof[3][2][2].y = -1;
prof[3][3][2].y = -1;
prof[0][0][3].y = 1;
prof[0][1][3].y = 1;
prof[0][2][3].y = 0;
prof[0][3][3].y = 0;
prof[1][0][3].y = -1;
prof[1][1][3].y = 0;
prof[1][2][3].y = 0;
prof[1][3][3].y = 1;
prof[2][0][3].y = -1;
prof[2][1][3].y = -1;
prof[2][2][3].y = 0;
prof[2][3][3].y = 0;
prof[3][0][3].y = 1;
prof[3][1][3].y = 0;
prof[3][2][3].y = 0;
prof[3][3][3].y = -1;
prof[0][0][4].y = 0;
prof[0][1][4].y = 0;
prof[0][2][4].y = 1;
prof[0][3][4].y = 0;
prof[1][0][4].y = -1;
prof[1][1][4].y = 0;
prof[1][2][4].y = 0;
prof[1][3][4].y = 1;
prof[2][0][4].y = 0;
prof[2][1][4].y = 0;
prof[2][2][4].y = -1;
prof[2][3][4].y = 0;
prof[3][0][4].y = 1;
prof[3][1][4].y = 0;
prof[3][2][4].y = 0;
prof[3][3][4].y = -1;
prof[0][0][5].y = 0;
prof[0][1][5].y = 1;
prof[0][2][5].y = 0;
prof[0][3][5].y = 1;
prof[1][0][5].y = 0;
prof[1][1][5].y = 1;
prof[1][2][5].y = 0;
prof[1][3][5].y = 1;
prof[2][0][5].y = 0;
prof[2][1][5].y = 1;
prof[2][2][5].y = 0;
prof[2][3][5].y = 1;
prof[3][0][5].y = 0;
prof[3][1][5].y = 1;
prof[3][2][5].y = 0;
prof[3][3][5].y = 1;
prof[0][0][6].y = 0;
prof[0][1][6].y = 0;
prof[0][2][6].y = 0;
prof[0][3][6].y = 0;
prof[1][0][6].y = -1;
prof[1][1][6].y = 0;
prof[1][2][6].y = 1;
prof[1][3][6].y = 2;
prof[2][0][6].y = 0;
prof[2][1][6].y = 0;
prof[2][2][6].y = 0;
prof[2][3][6].y = 0;
prof[3][0][6].y = -1;
prof[3][1][6].y = 0;
prof[3][2][6].y = 1;
prof[3][3][6].y = 2;
for (int y = 0;y<2;y++){
for (int x = 0;x<m+4;x++){
perim[x][y]= 1;
}}
for (int y = n+2;y<n+4;y++){
for (int x = 0;x<m+4;x++){
perim[x][y]= 4;
}}
for (int y = 2;y<n+2;y++){
for (int x = 0;x<2;x++){
perim[x][y]= 2;
}}
for (int y = 2;y<n+2;y++){
for (int x = m+2;x<m+4;x++){
perim[x][y]= 2;
}}
for(int y = 0;y<n+4;y++){
for (int x = 0;x<m+4;x++){
System.out.print(perim[x][y]);
}
System.out.println("");
}
b = new JButton [m][n];
setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!
for (int y = 0;y<n;y++){
for (int x = 0;x<m;x++){
b[x][y] = new JButton(" ");
tmp[x][y] = Color.DARK_GRAY;
b[x][y].setBackground(Color.DARK_GRAY);
add(b[x][y]);
b[x][y].setEnabled(true);
}//end inner for
}
setFocusable(true);
addKeyListener(this);
pack();
setVisible(true);
blockgen();
}//end constructor Mine()
class or {
int x;
int y;
}
public void blockgen(){
Component temporaryLostComponent = null;
pos[0] = 0;
pos[1] = 1;
rand = (int) (Math.floor(Math.random()*7+1));
centralx = 4;
centraly = 0;
System.out.print(rand);
if ((b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].getBackground() == Color.DARK_GRAY) &&