Advertisement
Ligh7_of_H3av3n

Tetris

Feb 9th, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.99 KB | None | 0 0
  1. package MoreExercise;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. public class Tetris extends JPanel {
  13.  
  14.     private static final long serialVersionUID = -8715353373678321308L;
  15.  
  16.     private final Point[][][] Tetraminos = {
  17.             // I-Piece
  18.             {          
  19.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1)},
  20.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3)},
  21.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1)},
  22.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3)}
  23.             },
  24.  
  25.             // J-Piece
  26.             {
  27.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0)},
  28.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2)},
  29.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2)},
  30.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0)}
  31.             },
  32.  
  33.             // L-Piece
  34.             {
  35.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2)},
  36.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2)},
  37.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0)},
  38.                     {new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0)}
  39.             },
  40.  
  41.             // O-Piece
  42.             {
  43.                     {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
  44.                     {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
  45.                     {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)},
  46.                     {new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)}
  47.             },
  48.  
  49.             // S-Piece
  50.             {
  51.                     {new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1)},
  52.                     {new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)},
  53.                     {new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1)},
  54.                     {new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)}
  55.             },
  56.  
  57.             // T-Piece
  58.             {
  59.                     {new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1)},
  60.                     {new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2)},
  61.                     {new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2)},
  62.                     {new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2)}
  63.             },
  64.  
  65.             // Z-Piece
  66.             {
  67.                     {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)},
  68.                     {new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2)},
  69.                     {new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1)},
  70.                     {new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2)},
  71.             }
  72.     };
  73.  
  74.  
  75.  
  76.     private final Color[] tetraminoColors = {
  77.             Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
  78.     };
  79.  
  80.     private Point pieceOrigin;
  81.     private int currentPiece;
  82.     private int rotation;
  83.     private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
  84.  
  85.     private long score;
  86.     private Color[][] well;
  87.  
  88.     // Creates a border around the well and initializes the dropping piece
  89.     private void init() {
  90.         well = new Color[12][24];
  91.         for (int i = 0; i < 12; i++) {
  92.             for (int j = 0; j < 23; j++) {
  93.                 if (i == 0 || i == 11 || j == 22) {
  94.                     well[i][j] = Color.GRAY;
  95.                 } else {
  96.                     well[i][j] = Color.BLACK;
  97.                 }
  98.             }
  99.         }
  100.         newPiece();
  101.     }
  102.  
  103.     // Put a new, random piece into the dropping position
  104.     public void newPiece() {
  105.         pieceOrigin = new Point(5, 2);
  106.         rotation = 0;
  107.         if (nextPieces.isEmpty()) {
  108.             Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
  109.             Collections.shuffle(nextPieces);
  110.         }
  111.         currentPiece = nextPieces.get(0);
  112.         nextPieces.remove(0);
  113.  
  114.         // Check if the new piece immediately collides with existing blocks
  115.         if (collidesAt(pieceOrigin.x, pieceOrigin.y, rotation)) {
  116.             // Game over condition
  117.             System.out.println("Game Over");
  118.             // You can add additional game over actions here, such as showing a message dialog
  119.             System.exit(0); // Exit the program after game over
  120.         }
  121.     }
  122.  
  123.     // Collision test for the dropping piece
  124.     private boolean collidesAt(int x, int y, int rotation) {
  125.         for (Point p : Tetraminos[currentPiece][rotation]) {
  126.             if (well[p.x + x][p.y + y] != Color.BLACK) {
  127.                 return true;
  128.             }
  129.         }
  130.         return false;
  131.     }
  132.  
  133.     // Rotate the piece clockwise or counterclockwise
  134.     public void rotate(int i) {
  135.         int newRotation = (rotation + i) % 4;
  136.         if (newRotation < 0) {
  137.             newRotation = 3;
  138.         }
  139.         if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
  140.             rotation = newRotation;
  141.         }
  142.         repaint();
  143.     }
  144.  
  145.     // Move the piece left or right
  146.     public void move(int i) {
  147.         if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
  148.             pieceOrigin.x += i;
  149.         }
  150.         repaint();
  151.     }
  152.  
  153.     // Drops the piece one line or fixes it to the well if it can't drop
  154.     public void dropDown() {
  155.         if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
  156.             pieceOrigin.y += 1;
  157.         } else {
  158.             fixToWell();
  159.         }
  160.         repaint();
  161.     }
  162.  
  163.     // Make the dropping piece part of the well, so it is available for
  164.     // collision detection.
  165.     public void fixToWell() {
  166.         for (Point p : Tetraminos[currentPiece][rotation]) {
  167.             well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
  168.         }
  169.         clearRows();
  170.         newPiece();
  171.     }
  172.  
  173.     public void deleteRow(int row) {
  174.         for (int j = row-1; j > 0; j--) {
  175.             for (int i = 1; i < 11; i++) {
  176.                 well[i][j+1] = well[i][j];
  177.             }
  178.         }
  179.     }
  180.  
  181.     // Clear completed rows from the field and award score according to
  182.     // the number of simultaneously cleared rows.
  183.     public void clearRows() {
  184.         boolean gap;
  185.         int numClears = 0;
  186.  
  187.         for (int j = 21; j > 0; j--) {
  188.             gap = false;
  189.             for (int i = 1; i < 11; i++) {
  190.                 if (well[i][j] == Color.BLACK) {
  191.                     gap = true;
  192.                     break;
  193.                 }
  194.             }
  195.             if (!gap) {
  196.                 deleteRow(j);
  197.                 j += 1;
  198.                 numClears += 1;
  199.             }
  200.         }
  201.  
  202.         switch (numClears) {
  203.             case 1:
  204.                 score += 100;
  205.                 break;
  206.             case 2:
  207.                 score += 300;
  208.                 break;
  209.             case 3:
  210.                 score += 500;
  211.                 break;
  212.             case 4:
  213.                 score += 800;
  214.                 break;
  215.         }
  216.     }
  217.  
  218.     // Draw the falling piece
  219.     private void drawPiece(Graphics g) {
  220.         g.setColor(tetraminoColors[currentPiece]);
  221.         for (Point p : Tetraminos[currentPiece][rotation]) {
  222.             g.fillRect((p.x + pieceOrigin.x) * 26,
  223.                     (p.y + pieceOrigin.y) * 26,
  224.                     25, 25);
  225.         }
  226.     }
  227.  
  228.     @Override
  229.     public void paintComponent(Graphics g)
  230.     {
  231.         // Paint the well
  232.         g.fillRect(0, 0, 26*12, 26*23);
  233.         for (int i = 0; i < 12; i++) {
  234.             for (int j = 0; j < 23; j++) {
  235.                 g.setColor(well[i][j]);
  236.                 g.fillRect(26*i, 26*j, 25, 25);
  237.             }
  238.         }
  239.  
  240.         // Display the score
  241.         g.setColor(Color.WHITE);
  242.         g.drawString("" + score, 19*12, 25);
  243.  
  244.         // Draw the currently falling piece
  245.         drawPiece(g);
  246.     }
  247.  
  248.     public static void main(String[] args) {
  249.         JFrame f = new JFrame("Tetris");
  250.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  251.         f.setSize(12*26+10, 26*23+25);
  252.         f.setVisible(true);
  253.  
  254.         final Tetris game = new Tetris();
  255.         game.init();
  256.         f.add(game);
  257.  
  258.         // Keyboard controls
  259.         f.addKeyListener(new KeyListener() {
  260.             public void keyTyped(KeyEvent e) {
  261.             }
  262.  
  263.             public void keyPressed(KeyEvent e) {
  264.                 switch (e.getKeyCode()) {
  265.                     case KeyEvent.VK_UP:
  266.                         game.rotate(-1);
  267.                         break;
  268.                     case KeyEvent.VK_DOWN:
  269.                         game.rotate(+1);
  270.                         break;
  271.                     case KeyEvent.VK_LEFT:
  272.                         game.move(-1);
  273.                         break;
  274.                     case KeyEvent.VK_RIGHT:
  275.                         game.move(+1);
  276.                         break;
  277.                     case KeyEvent.VK_SPACE:
  278.                         game.dropDown();
  279.                         game.score += 1;
  280.                         break;
  281.                 }
  282.             }
  283.  
  284.             public void keyReleased(KeyEvent e) {
  285.             }
  286.         });
  287.  
  288.         // Make the falling piece drop every second
  289.         new Thread() {
  290.             @Override public void run() {
  291.                 while (true) {
  292.                     try {
  293.                         Thread.sleep(1000);
  294.                         game.dropDown();
  295.                     } catch ( InterruptedException e ) {}
  296.                 }
  297.             }
  298.         }.start();
  299.     }
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement