Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Tetris extends JFrame implements KeyListener {
- int pos = 0;
- 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];
- public Tetris(){
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- 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()
- public void blockgen(){
- pos = 0;
- rand = (int) (Math.floor(Math.random()*7+1));
- System.out.print(rand);
- if (rand == 1){
- z();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 2){
- l();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 3){
- bl();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 4){
- s();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 5){
- t();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 6){
- sq();
- centralx = 4;
- centraly = 0;
- go();
- } else if (rand == 7){
- i();
- centralx = 4;
- centraly = 0;
- go();
- }
- }
- public void rotate(){
- pos++;
- if (rand == 1){
- } else if (rand == 2){
- } else if (rand == 3){
- } else if (rand == 4){
- } else if (rand == 5){
- } else if (rand == 6){
- } else if (rand == 7){
- if (pos == 1){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly-1].setBackground(Color.orange);
- b[centralx][centraly].setBackground(Color.orange);
- b[centralx][centraly+1].setBackground(Color.orange);
- b[centralx][centraly+2].setBackground(Color.orange);
- }
- }
- }
- public void movedown(){
- if (rand == 1){
- if ((perim[2+centralx-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4)
- && (perim[centralx+2][centraly+1+1+2] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+1+2][centraly+1+1+2] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly].setBackground(Color.red);
- b[centralx][centraly].setBackground(Color.red);
- b[centralx][centraly+1].setBackground(Color.red);
- b[centralx+1][centraly+1].setBackground(Color.red);
- } else {
- bottom = true;
- }
- } else if (rand == 2){
- if ((perim[centralx+2-1][centraly+1+2] != 4)
- && (perim[centralx+2-1][centraly+1+2+1] != 4) && (b[centralx-1][centraly+1+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly].setBackground(Color.yellow);
- b[centralx-1][centraly+1].setBackground(Color.yellow);
- b[centralx][centraly].setBackground(Color.yellow);
- b[centralx+1][centraly].setBackground(Color.yellow);
- } else {
- bottom = true;
- }
- } else if (rand == 3){
- if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+1][centraly+1+2] != 4)
- && (perim[centralx+2+1][centraly+1+2+1] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly].setBackground(Color.cyan);
- b[centralx][centraly].setBackground(Color.cyan);
- b[centralx+1][centraly].setBackground(Color.cyan);
- b[centralx+1][centraly+1].setBackground(Color.cyan);
- } else {
- bottom = true;
- }
- } else if (rand == 4){
- if ((perim[centralx+2-1][centraly+1+2+1] != 4) && (b[centralx-1][centraly+1+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4)
- && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)){
- b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly+1].setBackground(Color.green);
- b[centralx][centraly+1].setBackground(Color.green);
- b[centralx][centraly].setBackground(Color.green);
- b[centralx+1][centraly].setBackground(Color.green);
- } else {
- bottom = true;
- }
- } else if (rand == 5){
- if ((perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4)
- && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)){
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly].setBackground(Color.white);
- b[centralx][centraly].setBackground(Color.white);
- b[centralx+1][centraly].setBackground(Color.white);
- b[centralx][centraly+1].setBackground(Color.white);
- } else {
- bottom = true;
- }
- } else if (rand == 6){
- if ((perim[centralx+2][centraly+1+2] != 4)
- && (perim[centralx+2][centraly+1+2+1] != 4) && (b[centralx][centraly+1+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+1][centraly+1+2] != 4)
- && (perim[centralx+2+1][centraly+1+2+1] != 4) && (b[centralx+1][centraly+1+1].getBackground() == Color.DARK_GRAY)){
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx][centraly].setBackground(Color.blue);
- b[centralx][centraly+1].setBackground(Color.blue);
- b[centralx+1][centraly].setBackground(Color.blue);
- b[centralx+1][centraly+1].setBackground(Color.blue);
- } else {
- bottom = true;
- }
- } else if (rand == 7){
- if (pos == 0){
- if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+2][centraly+1+2] != 4) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx-1][centraly].setBackground(Color.orange);
- b[centralx][centraly].setBackground(Color.orange);
- b[centralx+1][centraly].setBackground(Color.orange);
- b[centralx+2][centraly].setBackground(Color.orange);
- } else {
- bottom = true;
- }
- } else if (pos == 1){
- if ((perim[centralx+2-1][centraly+1+2] != 4) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2][centraly+1+2] != 4) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+1][centraly+1+2] != 4) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)
- && (perim[centralx+2+2][centraly+1+2] != 4) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)){
- b[centralx][centraly-1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+2].setBackground(Color.DARK_GRAY);
- centraly++;
- b[centralx][centraly-1].setBackground(Color.orange);
- b[centralx][centraly].setBackground(Color.orange);
- b[centralx][centraly+1].setBackground(Color.orange);
- b[centralx][centraly+2].setBackground(Color.orange);
- } else {
- bottom = true;
- }
- }
- }
- }
- public void go(){
- do{
- try {
- Thread.sleep(1000L);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- movedown();
- rowcheck();
- }
- while(bottom == false);
- bottom = false;
- blockgen();
- }
- public void rowcheck(){
- int row = 0;
- for (int y = 0;y<20;y++){
- for (int x = 0;x<10;x++){
- if (b[x][y].getBackground() != Color.DARK_GRAY){
- row++;
- }
- if (row == 10){
- rowclear(y);
- }
- }
- row = 0;
- }
- }
- public void rowclear(int y){
- int inc = 0;
- for (int x = 0;x<10;x++){
- b[x][y].setBackground(Color.DARK_GRAY);
- }
- for (int c = y-1;c>-1;c--){
- for (int x = 0;x<10;x++){
- tmp[x][y-inc] = b[x][c].getBackground();
- }inc++;}
- for (int c = y;c>-1;c--){
- for (int x = 0;x<10;x++){
- b[x][c].setBackground(tmp[x][c]);
- }
- }
- }
- public void movelr(){
- if (rand == 1){
- if (((perim[centralx+deltax-1+2][centraly+2] != 1) && (perim[2+centralx+deltax-1][centraly+2] != 2) && (perim[2+centralx+deltax-1][2+centraly] != 3) && (perim[2+centralx+deltax-1][centraly+2] != 4))
- && ((perim[centralx+deltax+2][centraly+2] != 1) && (perim[2+centralx+deltax][centraly+2] != 2) && (perim[centralx+deltax+2][centraly+2] != 3) && (perim[centralx+deltax+2][centraly+2] != 4))
- && ((perim[centralx+deltax+2][centraly+1+2] != 1) && (perim[centralx+deltax+2][centraly+1+2] != 2) && (perim[centralx+deltax+2][centraly+1+2] != 3) && (perim[centralx+deltax+2][centraly+1+2] != 4))
- && ((perim[centralx+1+deltax+2][centraly+1+2] != 1) && (perim[centralx+deltax+1+2][centraly+1+2] != 2) && (perim[centralx+deltax+1+2][centraly+1+2] != 3) && (perim[centralx+deltax+1+2][centraly+1+2] != 4))){
- if (((deltax == 1) && (b[centralx+deltax+1][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax][centraly].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY))){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly].setBackground(Color.red);
- b[centralx][centraly].setBackground(Color.red);
- b[centralx][centraly+1].setBackground(Color.red);
- b[centralx+1][centraly+1].setBackground(Color.red);
- }
- }
- } else if (rand == 2){
- if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
- && ((perim[centralx+2+deltax-1][centraly+2+1] != 1) && (perim[centralx+2+deltax-1][centraly+2+1] != 2) && (perim[centralx+2+deltax-1][centraly+2+1] != 3) && (perim[centralx+2+deltax-1][centraly+2+1] != 4))
- && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))){
- if (((deltax == 1) && (b[centralx+deltax+1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+deltax-1][centraly+1].getBackground() == Color.DARK_GRAY))){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly].setBackground(Color.yellow);
- b[centralx-1][centraly+1].setBackground(Color.yellow);
- b[centralx][centraly].setBackground(Color.yellow);
- b[centralx+1][centraly].setBackground(Color.yellow);
- }
- }
- } else if (rand == 3){
- if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
- && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2+1] != 1) && (perim[centralx+2+deltax+1][centraly+2+1] != 2) && (perim[centralx+2+deltax+1][centraly+2+1] != 3) && (perim[centralx+2+deltax+1][centraly+2+1] != 4))){
- if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx][centraly+1].getBackground() == Color.DARK_GRAY) && (b[centralx-2][centraly].getBackground() == Color.DARK_GRAY))){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly].setBackground(Color.cyan);
- b[centralx][centraly].setBackground(Color.cyan);
- b[centralx+1][centraly].setBackground(Color.cyan);
- b[centralx+1][centraly+1].setBackground(Color.cyan);
- }
- }
- } else if (rand == 4){
- if (((perim[centralx+2+deltax-1][centraly+2+1] != 1) && (perim[centralx+2+deltax-1][centraly+2+1] != 2) && (perim[centralx+2+deltax-1][centraly+2+1] != 3) && (perim[centralx+2+deltax-1][centraly+2+1] != 4))
- && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))
- && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))){
- if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-2][centraly+1].getBackground() == Color.DARK_GRAY))){
- b[centralx-1][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly+1].setBackground(Color.green);
- b[centralx][centraly+1].setBackground(Color.green);
- b[centralx][centraly].setBackground(Color.green);
- b[centralx+1][centraly].setBackground(Color.green);
- }
- }
- } else if (rand == 5){
- if (((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
- && ((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
- && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))){
- if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+1][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY))){
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly].setBackground(Color.white);
- b[centralx][centraly].setBackground(Color.white);
- b[centralx+1][centraly].setBackground(Color.white);
- b[centralx][centraly+1].setBackground(Color.white);
- }
- }
- } else if (rand == 6){
- if (((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax][centraly+2+1] != 1) && (perim[centralx+2+deltax][centraly+2+1] != 2) && (perim[centralx+2+deltax][centraly+2+1] != 3) && (perim[centralx+2+deltax][centraly+2+1] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2+1] != 1) && (perim[centralx+2+deltax+1][centraly+2+1] != 2) && (perim[centralx+2+deltax+1][centraly+2+1] != 3) && (perim[centralx+2+deltax+1][centraly+2+1] != 4))){
- if (((deltax == 1) && (b[centralx+2][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx+2][centraly+1].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx-1][centraly].getBackground() == Color.DARK_GRAY) && (b[centralx-1][centraly+1].getBackground() == Color.DARK_GRAY))){
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly+1].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly+1].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx][centraly].setBackground(Color.blue);
- b[centralx][centraly+1].setBackground(Color.blue);
- b[centralx+1][centraly].setBackground(Color.blue);
- b[centralx+1][centraly+1].setBackground(Color.blue);
- }
- }
- } else if (rand == 7){
- if (((perim[centralx+2+deltax-1][centraly+2] != 1) && (perim[centralx+2+deltax-1][centraly+2] != 2) && (perim[centralx+2+deltax-1][centraly+2] != 3) && (perim[centralx+2+deltax-1][centraly+2] != 4))
- && ((perim[centralx+2+deltax][centraly+2] != 1) && (perim[centralx+2+deltax][centraly+2] != 2) && (perim[centralx+2+deltax][centraly+2] != 3) && (perim[centralx+2+deltax][centraly+2] != 4))
- && ((perim[centralx+2+deltax+1][centraly+2] != 1) && (perim[centralx+2+deltax+1][centraly+2] != 2) && (perim[centralx+2+deltax+1][centraly+2] != 3) && (perim[centralx+2+deltax+1][centraly+2] != 4))
- && ((perim[centralx+2+deltax+2][centraly+2] != 1) && (perim[centralx+2+deltax+2][centraly+2] != 2) && (perim[centralx+2+deltax+2][centraly+2] != 3) && (perim[centralx+2+deltax+2][centraly+2] != 4))){
- if (((deltax == 1) && (b[centralx+deltax+2][centraly].getBackground() == Color.DARK_GRAY)) || ((deltax == -1) && (b[centralx+deltax-1][centraly].getBackground() == Color.DARK_GRAY))){
- b[centralx-1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+1][centraly].setBackground(Color.DARK_GRAY);
- b[centralx+2][centraly].setBackground(Color.DARK_GRAY);
- centralx = centralx+deltax;
- b[centralx-1][centraly].setBackground(Color.orange);
- b[centralx][centraly].setBackground(Color.orange);
- b[centralx+1][centraly].setBackground(Color.orange);
- b[centralx+2][centraly].setBackground(Color.orange);
- }
- }
- }
- }
- public void l(){
- b[3][0].setBackground(Color.yellow);
- b[3][1].setBackground(Color.yellow);
- b[4][0].setBackground(Color.yellow);
- b[5][0].setBackground(Color.yellow);
- }
- public void sq(){
- b[4][0].setBackground(Color.blue);
- b[4][1].setBackground(Color.blue);
- b[5][0].setBackground(Color.blue);
- b[5][1].setBackground(Color.blue);
- }
- public void bl(){
- b[3][0].setBackground(Color.cyan);
- b[4][0].setBackground(Color.cyan);
- b[5][0].setBackground(Color.cyan);
- b[5][1].setBackground(Color.cyan);
- }
- public void s(){
- b[3][1].setBackground(Color.green);
- b[4][1].setBackground(Color.green);
- b[4][0].setBackground(Color.green);
- b[5][0].setBackground(Color.green);
- }
- public void z(){
- b[3][0].setBackground(Color.red);
- b[4][0].setBackground(Color.red);
- b[4][1].setBackground(Color.red);
- b[5][1].setBackground(Color.red);
- }
- public void i(){
- b[6][0].setBackground(Color.orange);
- b[3][0].setBackground(Color.orange);
- b[4][0].setBackground(Color.orange);
- b[5][0].setBackground(Color.orange);
- }
- public void t(){
- b[3][0].setBackground(Color.white);
- b[4][0].setBackground(Color.white);
- b[4][1].setBackground(Color.white);
- b[5][0].setBackground(Color.white);
- }
- public static void main (String[] args){
- new Tetris();
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_RIGHT){
- deltax = 1;
- movelr();
- }
- if (e.getKeyCode() == KeyEvent.VK_LEFT){
- deltax = -1;
- movelr();
- }
- if (e.getKeyCode() == KeyEvent.VK_UP){
- rotate();
- }
- if (e.getKeyCode() == KeyEvent.VK_DOWN){
- movedown();
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement