Pregunta: | 67268 - ELIMINAR EL PARPADEO EN JUEGO DE TETRIS (JFRAME) |
Autor: | ramon martinez |
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication76; import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; /** * * @author Administrador */ public class JavaApplication76 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here EventQueue.invokeLater(new Runnable() { public void run() { try { Tetris t = new Tetris(); t.setVisible(true); t.setBackground(Color.black); t.Empezar(); } catch (Exception e) { e.printStackTrace(); } } }); } } class Posicion { public int X; public int Y; } class Tetris extends JFrame { public Posicion _posicion; public ArrayList _lista; public Color[] _colores; private int _indicePieza; private int[][] _figuraActual; private int[][] _tablero; public int FILAS = 10; public int COLUMNAS = 20; private Random _r; private int _lado = 30; private JPanel contentPane; private Timer timer; private Color _violeta = new Color(192,0,255); private int[] _lineasVector = { 20, 40, 80, 100, 150, 200, 230, 270, 290, 400 }; private int [] _intervalosTiempo = { 1000, 800, 600, 550, 400, 350, 300, 200, 160, 50 }; private int _indiceTiempos=0; private int _lineasCompletas=0; // BufferStrategy bf = this.getBufferStrategy(); public Tetris(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setBounds(100, 100, 450, 300); // setBounds(0,0,0,0); contentPane = new JPanel(); //contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); contentPane.setBackground(Color.gray); int alto = FILAS * _lado + 1; int ancho = COLUMNAS * _lado + 1; setBounds(0,0,alto, ancho); this._tablero = new int[FILAS][COLUMNAS]; this._r = new Random(); this._posicion = new Posicion(); this._posicion.X=2; this._posicion.Y=3; this._colores = new Color[] {Color.blue , Color.red, Color.green, Color.orange, Color.magenta, Color.cyan, this._violeta }; this._lista = new ArrayList(); this._lista.add(new int[][] { {0,0,0}, {0,1,0}, {1,1,1} }); this._lista.add(new int[][] { {0,0,0}, {0,1,1}, {1,1,0} }); this._lista.add(new int[][] { {0,0,0}, {1,1,0}, {0,1,1} }); this._lista.add(new int[][] { {1,0,0}, {1,0,0}, {1,1,0} }); this._lista.add(new int[][] { {0,1,0}, {0,1,0}, {1,1,0} }); this._lista.add(new int[][] { {1,1}, {1,1} }); this._lista.add(new int[][] { {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0} }); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { actualiza(e.getKeyCode()); } /* public void keyReleased(KeyEvent e) { actualiza(e.getKeyCode()); }*/ private void actualiza(int keyCode) { switch (keyCode) { case KeyEvent.VK_UP: Rotar(); break; case KeyEvent.VK_SPACE: Caer(); break; case KeyEvent.VK_LEFT: MoverIzquierda(); break; case KeyEvent.VK_RIGHT: MoverDerecha(); break; } } }); //setFocusable(true); timer = new Timer (_intervalosTiempo[this._indiceTiempos], new ActionListener () { public void actionPerformed(ActionEvent e) { MoverAbajo(); if (!VerificarColision(_figuraActual, _posicion.X, _posicion.Y)) Detener(); repaint(); } }); } //FIN CONSTRUCTOR private void Detener() { timer.stop(); } public void MoverDerecha(){ if(VerificarColision(this._figuraActual, _posicion.X + 1, _posicion.Y ) ) { this._posicion.X++; } repaint(); } public void MoverIzquierda(){ if(VerificarColision(this._figuraActual, _posicion.X - 1, _posicion.Y ) ) { this._posicion.X--; } repaint(); } public boolean MoverAbajo(){ if(VerificarColision(this._figuraActual, _posicion.X, _posicion.Y + 1 )) { this._posicion.Y++; return true; } //SI NO APRUEBA FijarPieza(); //1. FIJA LA PIEZA AL TABLERO EliminarLineasCompletas(); Empezar(); //OBTIENE LA NUEVA PIEZA return false; } public void Caer(){ while(MoverAbajo()) ; repaint(); } public void Rotar(){ int tam = Array.getLength(this._figuraActual); int[][] auxiliar = new int[tam][ tam]; if (tam != 4) { for (int i = 0; i < tam; i++) for (int j = 0; j < tam; j++) auxiliar[j][ tam - i - 1] = _figuraActual[i][j]; }else{ for (int i = 0; i < tam; i++) for (int j = tam - 1; j >= 0; j--) auxiliar[i][j] = _figuraActual[j][i]; } if (VerificarColision(auxiliar , _posicion.X, _posicion.Y)) { Copiar(auxiliar); } repaint(); } public void Copiar(int[][] aux) { int tam = Array.getLength(this._figuraActual); for (int i = 0; i < tam; i++) for (int j = 0; j < tam; j++) _figuraActual[i][j] = aux[i ][j]; } private boolean VerificarColision(int[][] fig, int x, int y){ int tam = Array.getLength(fig); for(int i=0; i<tam; i++){ for(int j=0; j<tam; j++){ int rx = i + x; int ry = j + y; if( (rx < 0 || rx > FILAS-1 || ry < 0 || ry > COLUMNAS -1) && fig[i][j] !=0 ) return false; if( !(rx < 0 || rx > FILAS-1 || ry < 0 || ry > COLUMNAS -1)) { if(fig[i][j] !=0 && _tablero[rx][ry] != 0) return false; } } } return true; } private void FijarPieza(){ int tam =Array.getLength(this._figuraActual); for(int i=0; i<tam; i++){ for(int j=0; j<tam; j++){ if(_figuraActual[i][j] != 0){ this._tablero[i + _posicion.X][j + _posicion.Y] = _indicePieza + 1; } } } } private void EliminarLineasCompletas() { int contador = 0; for (int j = 1; j < COLUMNAS; j++) { boolean completa = true; for (int i = 0; i < FILAS; i++) { if (_tablero[i] [j] == 0) {completa = false;} } if (completa) { contador++; for (int k = j; k > 0; k--) for (int i = 0; i < FILAS; i++) _tablero[i][k] = _tablero[i][k - 1]; } completa = true; } this._lineasCompletas += contador; } public void ObtenerPieza(){ this._indicePieza = this._r.nextInt(this._lista.size()); Object x = this._lista.get(this._indicePieza); this._figuraActual =(int[][])x; } public void Empezar() { ObtenerPieza(); //CARGO LA FIGURA _posicion.X = 5; // SETEO LA POSICION INICIAL DE LA PIEZA _posicion.Y = 3; this.timer.setDelay( _intervalosTiempo[_indiceTiempos]); if (_indiceTiempos + 1 < _lineasVector.length && (_lineasCompletas > _lineasVector[_indiceTiempos])) { _indiceTiempos++; } timer.start(); } public void DibujarTablero(Graphics g){ for(int i=0; i<FILAS; i++){ for(int j=0; j<COLUMNAS ; j++){ if(_tablero[i][j] !=0){ // this._colores = new Color[] {Color.blue , Color.red, Color.green, // Color.orange, Color.magenta, Color.cyan}; Color unColor=null; switch(_tablero[i][j]){ case 1: unColor = Color.blue; break; case 2: unColor = Color.red; break; case 3: unColor = Color.green; break; case 4: unColor = Color.orange; break; case 5: unColor = Color.magenta; break; case 6: unColor = Color.cyan; break; case 7: unColor = this._violeta; break; } g.setColor(unColor); g.fillRect(i* _lado, j * _lado, _lado-1, _lado-1); } } } } public void DibujarPieza(Graphics g){ for(int i=0; i< Array.getLength(_figuraActual); i++){ for(int j=0; j< Array.getLength(_figuraActual); j++){ if(_figuraActual[i][j] != 0){ g.setColor(this._colores[_indicePieza]); g.fillRect((this._posicion.X + i) * _lado , (this._posicion.Y +j) * _lado , _lado-1, _lado-1); } } } } public void DibujarCuadricula(Graphics g){ for(int i=0; i<FILAS; i++){ for(int j=0; j<COLUMNAS; j++){ g.setColor(Color.black); /* rect = new Rectangle(i * lado, j * lado, lado - 1, lado - 1); gr.FillRectangle(pincel, rect); */ g.fillRect(i* _lado, j * _lado, _lado-1, _lado-1); } } } public void paint(Graphics g){ super.paint(g); DibujarCuadricula(g); DibujarTablero(g); DibujarPieza(g); } } |