Example #1
0
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == timer) {
     if (finishedFalling) {
       finishedFalling = false;
       makeNewPiece();
     } else {
       dropOneLine();
     }
   }
   if (e.getSource() == timer1 && state.time > 0 && state != null) {
     connection.send("time");
     if (state.time <= 1) {
       paused = true;
     }
     repaint();
   }
   if (e.getSource() == timer2) {
     if (state != null && startDone == false) {
       paused = false;
       start();
       startDone = true;
     }
     if (state != null && state.needToAdd1 && myID == state.player1) addLine(state.addLines1);
     else if (state != null && state.needToAdd2 && myID == state.player2)
       addLine(state.addLines2);
   }
 }
Example #2
0
 private void addLine(int num) {
   for (int i = 0; i < staticY.size(); i++) staticY.set(i, staticY.get(i) - num);
   for (int x = 0; x < FinalWidth; x++) {
     for (int y = 0; y < num; y++) {
       staticX.add(x);
       staticY.add(FinalHeight - 1 - y);
     }
   }
   findShadow();
   if (state != null) {
     connection.send("done");
   }
 }
Example #3
0
 private void removeLine() {
   int[] allCols = new int[FinalHeight];
   for (int i = 0; i < FinalHeight; i++) allCols[i] = 0;
   for (int j = 0; j < staticY.size(); j++) {
     allCols[staticY.get(j)]++;
   }
   ArrayList<Integer> fullRows = new ArrayList<Integer>();
   for (int c = 0; c < allCols.length; c++) {
     if (allCols[c] >= FinalWidth
         && (c == tempY[0] || c == tempY[1] || c == tempY[2] || c == tempY[3])) {
       fullRows.add(c);
     }
   }
   if (fullRows.size() > 0) {
     for (int a = 0; a < fullRows.size(); a++) {
       for (int b = 0; b < staticY.size(); b++) {
         if (fullRows.get(a) == staticY.get(b)) {
           staticX.remove(b);
           staticY.remove(b);
           b--;
         }
       }
     }
     for (int k = 0; k < fullRows.size(); k++) {
       for (int w = 0; w < staticY.size(); w++) {
         if (staticY.get(w) < fullRows.get(k)) staticY.set(w, staticY.get(w) + 1);
       }
     }
     score += fullRows.size();
     if (state != null) {
       connection.send(score);
       connection.send((double) fullRows.size());
     }
     findShadow();
     makeNewPiece();
     repaint();
   }
 }
Example #4
0
 public TetrisWindow(String hostName, int serverPortNumber) throws IOException {
   super("Rainbow Tetris");
   connection = new TetrisClient(hostName, serverPortNumber);
   myID = connection.getID();
   board = new Board();
   message = new JLabel("Waiting for two players to connect.", JLabel.CENTER);
   board.setBackground(Color.WHITE);
   board.setPreferredSize(new Dimension(300, 660));
   board.addMouseListener(
       new MouseAdapter() {
         public void mousePressed(MouseEvent evt) {
           doMouseClick();
         }
       });
   message.setBackground(Color.LIGHT_GRAY);
   message.setOpaque(true);
   JPanel content = new JPanel();
   content.setLayout(new BorderLayout(2, 2));
   content.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
   content.setBackground(Color.GRAY);
   content.add(board, BorderLayout.CENTER);
   content.add(message, BorderLayout.SOUTH);
   setContentPane(content);
   pack();
   setResizable(false);
   setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
   addWindowListener(
       new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
           dispose();
           connection.disconnect();
           try {
             Thread.sleep(333);
           } catch (InterruptedException e) {
           }
           System.exit(0);
         }
       });
   setLocation(200, 100);
   setVisible(true);
 }
Example #5
0
    private void makeNewPiece() {
      if (!paused) {
        randNum = (int) (Math.random() * 7);
        currentX = findXCoords(randNum);
        currentY = findYCoords(randNum);
        turnAmount = 0;
        findShadow();

        boolean possibility = true;
        for (int i = 0; i < 4; i++) {
          if (!attemptMove(currentX[i], currentY[i])) {
            possibility = false;
          }
        }
        if (state != null && !possibility) {
          timer.stop();
          connection.send("KO");
          start();
        }
      }
    }