/****************************************************************************** * PROJETO PENTRIS - Disciplina Processamento da Informacao * Professora Carla Negri Lintzmayer * * Alunos: * Fulano de tal 1, RA XXXXX * Fulano de tal 2, RA YYYYY * Fulano de tal, RA ZZZZZ * * 6 pecas escolhidas: A, B, C, D, E, F * Peca criada: * * XXXX * X * *******************************************************************************/ import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Collections; import javax.swing.JFrame; import javax.swing.JPanel; public class Pentris extends JPanel { /* Quantidade de linhas e colunas da tela do jogo * MODIFICAR APENAS SE ESTRITAMENTE NECESSARIO */ private static int ALTURA = 30; private static int LARGURA = 20; /* Largura em pixels dos quadrados * MODIFICAR APENAS SE ESTRITAMENTE NECESSARIO */ private static int QUADRADO = 26; private Color[][] tela; private final Point[][][] pecas = { /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } }, /* Peca A e suas rotacoes possiveis: */ { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 2) } } }; private final Color[] cor_peca = { Color.CYAN, Color.BLUE, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.PINK, Color.RED }; /* Variavel peca_atual guarda o indice da peca dentro do vetor pecas */ private int peca_atual; /* Variavel rotacao guarda qual das 4 rotacoes a peca atual esta */ private int rotacao; /* Variavel centro_peca_atual guarda as coordenadas do centro da peca atual */ private Point centro_peca_atual; private ArrayList proximas_pecas = new ArrayList(); private long pontuacao; /* Cria a tela de jogo */ private void gera_tela() { tela = new Color[LARGURA][ALTURA]; /* Bordas em cinza */ for (int y = 0; y < ALTURA; y++) { tela[0][y] = Color.GRAY; tela[LARGURA-1][y] = Color.GRAY; } for (int x = 0; x < LARGURA; x++) { tela[x][0] = Color.GRAY; tela[x][ALTURA-1] = Color.GRAY; } /* Restante em preto */ for (int y = 1; y < ALTURA-1; y++) { for (int x = 1; x < LARGURA-1; x++) { tela[x][y] = Color.BLACK; } } } /* Coloca uma nova peca, escolhida aleatoriamente, na posicao de queda */ public void nova_peca() { if (proximas_pecas.isEmpty()) { /* se nao houver proximas pecas, coloca uma de cada tipo e as embaralha */ Collections.addAll(proximas_pecas, 0, 1, 2, 3, 4, 5, 6); Collections.shuffle(proximas_pecas); } /* escolhe a primeira peca da lista de proximas pecas e a remove da lista */ peca_atual = proximas_pecas.get(0); proximas_pecas.remove(0); rotacao = 0; centro_peca_atual = new Point(LARGURA/2 - 2, 3); } /* Testa se a peca atual colide caso seu centro seja colocado na posicao (x,y) e ela * esteja na rotacao dada (independente do centro e rotacao atuais dela) */ private boolean existe_colisao(int x, int y, int rotacao) { for (Point p : pecas[peca_atual][rotacao]) { /* Se algum ponto da peca toca na borda da tela ou em uma peca ja posicionada, * entao ela colide */ if (tela[p.x + x][p.y + y] != Color.BLACK) { return true; } } return false; } /* Rotaciona a peca no sentido horario, se for possivel */ public void rotaciona() { /*************************** * * IMPLEMENTE-ME! * * *****************************/ repaint(); } /* Move a peca para a direita se i == 1 ou para a esquerda se i == -1, se possivel */ public void move(int i) { /*************************** * * IMPLEMENTE-ME! * * *****************************/ repaint(); } /* Limpa linhas que estiverem completas e arruma a pontuacao de acordo */ public void limpa_linhas_completas() { int num_linhas_limpas = 0; /*************************** * * IMPLEMENTE-ME! * * *****************************/ /* Dependendo do numero de linhas que foram limpas, arrumar a pontuacao */ switch (num_linhas_limpas) { case 1: pontuacao += 100; break; case 2: pontuacao += 300; break; case 3: pontuacao += 500; break; case 4: pontuacao += 800; break; } } /* Faz com que a peca atual se torne parte da tela, para permitir deteccao de colisao * Pode gerar linhas completas * Unico momento em que uma nova peca pode ser gerada */ public void fixa_na_tela() { /*************************** * * IMPLEMENTE-ME! * * *****************************/ repaint(); } /* Se for possivel, desce a peca **uma** linha para baixo * Se nao for, fixa a peca na tela */ public void desce() { /*************************** * * IMPLEMENTE-ME! * * *****************************/ repaint(); } /* Derruba a peca de uma vez */ public void derruba() { /*************************** * * IMPLEMENTE-ME! * * *****************************/ repaint(); } @Override public void paintComponent(Graphics g) { /* Desenha a tela */ g.fillRect(0, 0, QUADRADO * LARGURA, QUADRADO * ALTURA); for (int y = 0; y < ALTURA; y++) { for (int x = 0; x < LARGURA; x++) { g.setColor(tela[x][y]); g.fillRect(QUADRADO * x, QUADRADO * y, QUADRADO-1, QUADRADO-1); } } /* Mostra a pontuacao */ g.setColor(Color.WHITE); g.drawString("Pontuacao: " + pontuacao, (QUADRADO-6) * LARGURA, QUADRADO/2); /* Desenha a peca que esta caindo */ g.setColor(cor_peca[peca_atual]); for (Point p : pecas[peca_atual][rotacao]) { g.fillRect((p.x + centro_peca_atual.x) * QUADRADO, (p.y + centro_peca_atual.y) * QUADRADO, QUADRADO-1, QUADRADO-1); } } public static void main(String[] args) { JFrame f = new JFrame("Pentris"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(QUADRADO * (LARGURA), QUADRADO * (ALTURA+1)); f.setVisible(true); final Pentris jogo = new Pentris(); jogo.gera_tela(); jogo.nova_peca(); f.add(jogo); /* Controle do teclado */ f.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: jogo.rotaciona(); break; case KeyEvent.VK_DOWN: jogo.desce(); jogo.pontuacao += 1; break; case KeyEvent.VK_LEFT: jogo.move(-1); break; case KeyEvent.VK_RIGHT: jogo.move(+1); break; case KeyEvent.VK_SPACE: jogo.derruba(); jogo.pontuacao += 10; break; } } public void keyReleased(KeyEvent e) { } }); /* Faz a peca atual cair a cada segundo */ new Thread() { @Override public void run() { while (true) { try { Thread.sleep(1000); jogo.desce(); } catch ( InterruptedException e ) {} } } }.start(); } }