Skip to content

Instantly share code, notes, and snippets.

@Russell-Nesbitt
Forked from DataWraith/COPYING
Last active February 13, 2022 02:48
Show Gist options
  • Save Russell-Nesbitt/22875d3755f00cc604a998b9ca7bcfed to your computer and use it in GitHub Desktop.
Save Russell-Nesbitt/22875d3755f00cc604a998b9ca7bcfed to your computer and use it in GitHub Desktop.
A simple Tetris clone written in Java
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
Zed
8000
Fred
5000
Head
3000
Bed
2000
Ded
1000
// Tetris.java
//
// Written in 2013 by Johannes Holzfuß <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//import java.io.PrintStream;
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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) }
},
// L-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) }
},
// J-Piece (changed rotation order so it would start facing up rather than down)
{
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) }, //3
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) }, //4
{ new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) }, //1
{ new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) } //2
},
// 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) }
}
};
//Note: Switched colors of J and L to reflect tetris standard
private final Color[] tetraminoColors = {
Color.cyan, Color.orange, Color.blue, Color.yellow, Color.green, Color.pink, Color.red
};
private Point pieceOrigin;
private int currentPiece;
private boolean swapped = false;
private boolean gameOver = false;
private boolean keepPlaying = true;
private boolean added = false;
private static int storedPiece = -1;
private static int tempPiece = -1;
private int rotation;
private ArrayList<Integer> nextPieces = new ArrayList<Integer>();
private int score;
private int phase = 1;
private int timeDec = 200;
private Color[][] well;
private static JFrame f;
private static JFrame endGame;
private static JLabel endGameText;
private static JButton newGame;
private static JButton exit;
private static BufferedReader inputFile;
private static FileWriter writeFile;
private static int lowestScore;
private static int sleepTime = 1000;
private static String endGameString;
// Creates a border around the well and initializes the dropping piece
private void init() {
well = new Color[12][25];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 24; j++) {
if (i == 0 || i == 11 || j == 23 || j == 0) {
well[i][j] = Color.GRAY;
} else {
well[i][j] = Color.BLACK;
}
}
}
newPiece();
}
//resets the game world
private void restart()
{
//reset score and level variables
added = false;
endGame.setVisible(false);
gameOver = false;
phase = 1;
timeDec = 200;
sleepTime = 1000;
storedPiece = -1;
score = 0;
//clear the game board
for (int j = 22; j > 0; j--)
{
for(int i = 1; i < 11; i++)
{
well[i][j] = Color.BLACK;
}
}
newPiece();
repaint();
}
// Put a new, random piece into the dropping position
public void newPiece() {
pieceOrigin = new Point(5, 1);
rotation = 0;
//Game Over Code
if(collidesAt(5, 1, 0))
{
gameOver = true;
} else
{
if (nextPieces.isEmpty()) {
Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(nextPieces);
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
}
}
//Put a predetermined piece into the starting position
public void newPiece(int piece) {
pieceOrigin = new Point(5, 1);
rotation = 0;
//Game Over Code
if(collidesAt(5, 1, 0))
{
gameOver = true;
}
currentPiece = piece;
}
// 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();
}
// 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();
}
/*
* Drops the piece down until it collides. Only used if space is pressed.
* Player is awarded two bonus points for every row the piece drops
*/
public void dropToBottom()
{
//while it can drop down, drops down.
while(!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation))
{
score += 2;
pieceOrigin.y += 1;
}
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];
}
swapped = false;
clearRows();
newPiece();
}
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];
}
}
}
// 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 = 22; 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;
break;
case 2:
score += 300;
break;
case 3:
score += 500;
break;
case 4:
score += 800;
break;
}
//CODE FOR DIFFICULTY UPDATING
//decrements length of time the game sleeps between dropdowns every 1000 points earned
if(score >= 1000 * phase)
{
//prevents timeDec from going to zero and breaking game - if the current tick time is
//less than or equal to twice the timeDec value, timeDec is divided by 2
//Should scale infinitely
if(sleepTime <= (2 * timeDec))
{
timeDec /= 2;
}
sleepTime -= timeDec;
phase++;
}
}
// 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, (p.y + pieceOrigin.y) * 26 + 60, 25, 25);
}
}
// Returns the standard letter representation for a tetramino
public String getPieceName(int num)
{
String returnString = null;
if(num == -1)
{
returnString = "None";
} else if (storedPiece == 0)
{
returnString = "I";
} else if (storedPiece == 1)
{
returnString = "L";
} else if (storedPiece == 2)
{
returnString = "J";
} else if (storedPiece == 3)
{
returnString = "O";
} else if (storedPiece == 4)
{
returnString = "S";
} else if (storedPiece == 5)
{
returnString = "T";
} else if (storedPiece == 6)
{
returnString = "Z";
}
return returnString;
}
@Override
public void paintComponent(Graphics g)
{
String storedPieceName = null;
// Paint the well
//Note: Edited to leave blank space at top for scoring data
g.fillRect(0, 0, 26*12, 26*24 + 60);
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 24; j++) {
g.setColor(well[i][j]);
g.fillRect(26*i, 26*j + 60, 25, 25);
}
}
// Display the score
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 20, 35);
//Display the current phase/level
g.setColor(Color.WHITE);
g.drawString("Level: " + phase, 120, 35);
//Display the currently stored piece
g.setColor(Color.WHITE);
storedPieceName = getPieceName(storedPiece);
g.drawString("Stored: " + storedPieceName, 220, 35);
//Draw the currently falling piece
drawPiece(g);
}
public static void main(String[] args) throws IOException
{
//Get scores from the high score file
String[] storeScores = new String[10];
//Read data from the file and transfer it into an array for storage
inputFile = new BufferedReader(new FileReader("src/highscores.txt"));
String line = inputFile.readLine();
int lineNum = 0;
//Copies high-score information from file and into an array
while(line != null)
{
storeScores[lineNum] = line;
line = inputFile.readLine();
lineNum++;
}
lowestScore = Integer.parseInt(storeScores[9]);
//JFrame Initialization
f = new JFrame("Tetris");
endGame = new JFrame("Game Over");
endGame.setLayout(null);
endGame.setSize(12*26+15, 300);
endGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newGame = new JButton();
newGame.setText("Play Again");
exit = new JButton();
exit.setText("<html>Exit and Save Scores<html>");
endGameText = new JLabel();
endGameText.setSize(500, 100); //200 25
endGameText.setLocation(90, 160); //90 160
newGame.setSize(100, 100);
newGame.setLocation(50, 50);
newGame.setBackground(Color.GREEN);
exit.setSize(100, 100);
exit.setLocation((100 + 62), 50);
exit.setBackground(Color.RED);
endGame.add(newGame);
endGame.add(exit);
endGame.add(endGameText);
endGame.getContentPane().setBackground(Color.BLACK);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+15, 26*24+38 + 60);
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_UP:
game.rotate(+1);
break;
//Note: Drop down has been switched to the down arrow key.
case KeyEvent.VK_DOWN:
game.dropDown();
game.score += 1;
break;
case KeyEvent.VK_LEFT:
game.move(-1);
break;
case KeyEvent.VK_RIGHT:
game.move(+1);
break;
/*
* Space bar is used for a Hard Drop - Piece instantly moves to the bottom of
* the screen. This awards 2 points for every row dropped.
*/
case KeyEvent.VK_SPACE:
game.dropToBottom();
break;
/*
* Stored Piece Code
* On pressing c, the currently dropping piece should be
* stored and be replaced by the piece previously in storage. The game only
* permits "swapping" once per drop, unless the player is storing a piece for
* the first time.
*/
case KeyEvent.VK_C:
/*
* If no piece has been stored yet, store the current piece and drop a new
* randomly selected piece
*/
if(storedPiece == -1)
{
storedPiece = game.currentPiece;
game.repaint();
game.newPiece();
} else
{
/*
* If player has not already swapped this drop, swaps current
* and stored pieces and sets game.swapped to true
*/
if(game.swapped == false)
{
tempPiece = game.currentPiece;
game.newPiece(storedPiece);
storedPiece = tempPiece;
game.swapped = true;
game.repaint();
}
//If player has already swapped this drop, do nothing
}
break;
}
}
public void keyReleased(KeyEvent e)
{
}
}
);
//Modified dropdown iterator code, removed threads
while(game.keepPlaying)
{
while(game.gameOver == false)
{
try {
TimeUnit.MILLISECONDS.sleep(sleepTime);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
game.dropDown();
}
//GAME OVER CODE
//check to see if the player scored higher than the lowest scoring entry
if(game.score > lowestScore)
{
int currentScore = 0;
int i = 9;
while (game.added == false)
{
currentScore = Integer.parseInt(storeScores[i]);
//System.out.println("current Score: "+ currentScore);
//if player's score is the new highest score
if(i == 1)
{
game.added = true;
storeScores[i] = Integer.toString(game.score);
storeScores[i - 1] = "Player";
//if player's score belongs in the middle of the scores
}
if(game.score <= currentScore)
{
//System.out.println("Found where it belongs");
game.added = true;
storeScores[i + 2] = Integer.toString(game.score);
storeScores[i + 1] = "Player";
//System.out.println("Added: " + Arrays.toString(storeScores));
//otherwise, shift the array down one
}
if(game.score > currentScore && game.added == false)
{
//System.out.println("Too small, shift down: ");
storeScores[i] = storeScores[i - 2];
storeScores[i - 1] = storeScores[i - 3];
//System.out.println("Updated: " + Arrays.toString(storeScores));
}
i = i - 2;
}
lowestScore = Integer.parseInt(storeScores[9]);
}
endGameString = "<html><pre>";
for(int i = 0; i < 10; i = i + 2)
{
endGameString = endGameString + storeScores[i];
endGameString = endGameString + "&#9;";
endGameString = endGameString + storeScores[i + 1] + "<BR>";
}
endGameString = endGameString + "</pre></html>";
endGameText.setText(endGameString);
endGame.setVisible(true);
newGame.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
game.restart();
}
});
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
game.keepPlaying = false;
}
});
}
//Exit game code
endGame.dispose();
f.dispose();
inputFile.close();
//write new high scores to file
writeFile = new FileWriter("src/highscores.txt");
for(int i = 0; i < storeScores.length; i++)
{
if (i < storeScores.length - 1)
{
writeFile.write(storeScores[i] + "\n");
} else
{
writeFile.write(storeScores[i]);
}
}
writeFile.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment