Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 java.util.Random;
- import java.io.*;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Tetris extends JPanel {
- private static final long serialVersionUID = -8715353373678321308L;
- private final Point[][][] Tetraminos = {
- // I-Piece
- {
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) },
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }
- },
- // J-Piece
- {
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) },
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) }
- },
- // L-Piece
- {
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) },
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) },
- { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }
- },
- // O-Piece
- {
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) },
- { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }
- },
- // S-Piece
- {
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
- { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) },
- { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }
- },
- // T-Piece
- {
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) },
- { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) },
- { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) },
- { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) }
- },
- // Z-Piece
- {
- { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) },
- { new Point(1, 0), new Point(0, 1), new Point(1, 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(0, 1), new Point(1, 1), new Point(0, 2) }
- }
- };
- private final Color[] tetraminoColors = {
- Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
- };
- private Point pieceOrigin;
- private int currentPiece;
- private int rotation;
- private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
- private int nextPiece1;
- private int nextPiece2;
- private int nextPiece3;
- private int holdPiece;
- private long score;
- private long highScore;
- private long totalScore;
- private long lines;
- //private long totalLines;
- private int level = 0;
- private int linesToNext;
- private Color[][] well;;
- public void save(){
- try{ // Catch errors in I/O if necessary.
- //Open a file to write to, named SavedObj.sav.
- FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
- // Create an ObjectOutputStream to put objects into save file.
- ObjectOutputStream save = new ObjectOutputStream(saveFile);
- // Now we do the save.
- save.writeObject(highScore);
- save.writeObject(totalScore);
- //save.writeObject(totalLines);
- // Close the file.
- save.close(); // This also closes saveFile.
- }
- catch(Exception exc){
- exc.printStackTrace(); // If there was an error, print the info.
- }
- }
- public void load(){
- try{
- FileInputStream saveFile = new FileInputStream("SaveObj.sav");
- ObjectInputStream save = new ObjectInputStream(saveFile);
- totalScore = (Long) save.readObject();
- highScore = (Long) save.readObject();
- }
- catch(Exception exc){
- exc.printStackTrace(); // If there was an error, print the info.
- }
- }
- // Creates a border around the well and initializes the dropping piece
- private void init() {
- Random rand = new Random();
- load();
- well = new Color[12][24];
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 23; j++) {
- if (i == 0 || i == 11 || j == 22) {
- well[i][j] = Color.GRAY;
- } else {
- well[i][j] = Color.BLACK;
- }
- }
- }
- totalScore += score; // total score counter
- score = 0; //score
- lines = 0; //counts how many lines have been cleared
- level = 1; //affects the drop speed and multiplier
- linesToNext = 10; //how many lines until the next level
- nextPiece1 = rand.nextInt(6); //sets next piece 1 to a random piece
- nextPiece2 = rand.nextInt(6); //sets next piece 2 to a random piece
- nextPiece3 = rand.nextInt(6); //sets next piece 3 to a random piece
- holdPiece = 7; //sets hold piece to a non piece value
- newPiece();
- }
- // Put a new, random piece into the dropping position
- public void newPiece() {
- Random rand = new Random();
- pieceOrigin = new Point(5, 2);
- rotation = 0;
- currentPiece = nextPiece1;
- nextPiece1 = nextPiece2;
- nextPiece2 = nextPiece3;
- nextPiece3 = rand.nextInt(6);
- if (collidesAt (pieceOrigin.x, pieceOrigin.y, rotation))
- {
- save();
- if (score>=highScore)
- {
- highScore = score;
- }
- init();
- }
- }
- // Collision test for the dropping piece
- private boolean collidesAt(int x, int y, int rotation) {
- for (Point p : Tetraminos[currentPiece][rotation]) {
- if (well[p.x + x][p.y + y] != Color.BLACK) {
- return true;
- }
- }
- return false;
- }
- // Rotate the piece clockwise or counterclockwise
- public void rotate(int i) {
- int newRotation = (rotation + i) % 4;
- if (newRotation < 0) {
- newRotation = 3;
- }
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
- rotation = newRotation;
- }
- repaint();
- }
- //Holds a piece for you
- public void hold()
- {
- int n;
- if (holdPiece == 7)
- {
- holdPiece = currentPiece;
- newPiece();
- }
- else
- {
- n = holdPiece;
- holdPiece = currentPiece;
- currentPiece = n;
- pieceOrigin = new Point(5, 2);
- rotation = 0;
- }
- }
- // Move the piece left or right
- public void move(int i) {
- if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
- pieceOrigin.x += i;
- }
- repaint();
- }
- // Drops the piece one line or fixes it to the well if it can't drop
- public void dropDown() {
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
- pieceOrigin.y += 1;
- } else {
- fixToWell();
- }
- repaint();
- }
- // Make the dropping piece part of the well, so it is available for
- // collision detection.
- public void fixToWell() {
- for (Point p : Tetraminos[currentPiece][rotation]) {
- well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
- }
- clearRows();
- newPiece();
- }
- public boolean bottom(boolean bottom)
- {
- if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) {
- return false;
- } else {
- return true;
- }
- }
- public void deleteRow(int row) {
- for (int j = row-1; j > 0; j--) {
- for (int i = 1; i < 11; i++) {
- well[i][j+1] = well[i][j];
- }
- }
- }
- // Draw the falling piece
- private void drawPiece(Graphics g) {
- g.setColor(tetraminoColors[currentPiece]);
- for (Point p : Tetraminos[currentPiece][rotation]) {
- g.fillRect((p.x + pieceOrigin.x) * 26+6*24,
- (p.y + pieceOrigin.y) * 26,
- 25, 25);
- }
- g.setColor(tetraminoColors[nextPiece1]);
- for (Point p : Tetraminos[nextPiece1][0]) {
- g.fillRect((p.x)*26+28*12+6*24,
- (p.y)*26+8*12,
- 26, 26);
- }
- g.setColor(tetraminoColors[nextPiece2]);
- for (Point p : Tetraminos[nextPiece2][0]) {
- g.fillRect((p.x)*26+28*12+6*24,
- (p.y)*26+15*12,
- 26, 26);
- }
- g.setColor(tetraminoColors[nextPiece3]);
- for (Point p : Tetraminos[nextPiece3][0]) {
- g.fillRect((p.x)*26 + 28*12+6*24,
- (p.y)*26 + 22*12,
- 26, 26);
- }
- if (holdPiece != 7)
- {
- g.setColor(tetraminoColors[holdPiece]);
- for (Point p : Tetraminos[holdPiece][0]) {
- g.fillRect((p.x)*26 + 3*12,
- (p.y)*26 + 5*12,
- 26, 26);
- }
- }
- }
- // Clear completed rows from the field and award score according to
- // the number of simultaneously cleared rows.
- public void clearRows() {
- boolean gap;
- int numClears = 0;
- for (int j = 21; j > 0; j--) {
- gap = false;
- for (int i = 1; i < 11; i++) {
- if (well[i][j] == Color.BLACK) {
- gap = true;
- break;
- }
- }
- if (!gap) {
- deleteRow(j);
- j += 1;
- numClears += 1;
- }
- }
- switch (numClears) {
- case 1:
- score += 100*level;
- lines += 1;
- linesToNext -= 1;
- break;
- case 2:
- score += 300*level;
- lines += 2;
- linesToNext -= 2;
- break;
- case 3:
- score += 500*level;
- lines += 3;
- linesToNext -= 3;
- break;
- case 4:
- score += 800*level;
- lines += 4;
- linesToNext -= 4;
- break;
- }
- if (linesToNext<=0)
- {
- level ++;
- linesToNext += level*5;
- }
- }
- public int findLevel(int ha)
- {
- ha = level;
- return ha;
- }
- @Override
- public void paintComponent(Graphics g)
- {
- g.setColor(Color.BLACK); //sets paint to black
- g.fillRect(0,0,24*32,23*26); //paints the background
- // Paint the well
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 23; j++) {
- g.setColor(well[i][j]);
- g.fillRect(26*i+24*6, 26*j, 25, 25);
- }
- }
- g.setColor(Color.BLACK);
- g.fillRect(12*26+24*6,0,12*46+12*6,23*26);
- // Display the score
- g.setColor(Color.WHITE);
- g.drawString("score " + score, 28*12+6*24, 2*12); // write current score
- g.drawString("level " + level, 28*12+6*24, 4*12); // display current level
- g.drawString("high score " + highScore, 28*12+6*24, 3*12); //display highest score achieved
- g.drawString("lines to next Level " + linesToNext, 28*12+6*24, 5*12); // display lines until next level
- g.drawString("NEXT",28*12+6*24,7*12);
- g.drawString("Hold",5*12,3*12);
- // Draw the currently falling piece
- drawPiece(g);
- }
- public static void main(String[] args) {
- JFrame f = new JFrame("Tetris");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setSize(12*52+10, 26*23+25);
- f.setVisible(true);
- final Tetris game = new Tetris();
- game.init();
- f.add(game);
- // Keyboard controls
- f.addKeyListener(new KeyListener()
- {
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- switch (e.getKeyCode()) {
- case KeyEvent.VK_C:
- game.hold();
- break;
- case KeyEvent.VK_UP:
- game.rotate(+1);
- break;
- case KeyEvent.VK_Z:
- game.rotate(-1);
- break;
- case KeyEvent.VK_SPACE:
- while (!game.bottom(false) )
- {
- game.dropDown(); //drops to the bottom
- game.score += 2*game.findLevel(1); //adds 1 point per level
- }
- game.dropDown();
- break;
- case KeyEvent.VK_LEFT:
- game.move(-1); //moves left 1
- break;
- case KeyEvent.VK_RIGHT:
- game.move(+1); //moves right 1
- break;
- case KeyEvent.VK_DOWN: //if you press down
- game.dropDown(); // goes down 1 square
- game.score += 1*game.findLevel(1); //adds 1 point
- break;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- });
- // Make the falling piece drop every second
- new Thread()
- {
- @Override public void run() {
- while (true) {
- try {
- Thread.sleep(1000-(10*game.findLevel(1)));
- game.dropDown();
- } catch ( InterruptedException e ) {}
- }
- }
- }
- .start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement