Created
January 13, 2012 14:46
-
-
Save anonymous/1606601 to your computer and use it in GitHub Desktop.
Tetris Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//To download the jar file: http://www.mediafire.com/?c79343sv8okmgay | |
import java.applet.Applet; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.awt.MediaTracker; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import javax.swing.JOptionPane; | |
public class Tetris extends Applet implements Runnable, KeyListener { | |
/* | |
* Winston: | |
* Added Dropping | |
* Renamed a ton of variables, classes. removed unnecessary components | |
* Made pieces bump left, right, or down based on edge collisions. (might want to make bump up also). | |
* still some problems with bumping. | |
* also, Tetrimino can replace previous tetrimino after rotation. | |
* Made two different rotation types. | |
* Might want to create own rotation methods | |
* | |
* | |
* Crazy speedup when the program reaches level 11 | |
*/ | |
MediaTracker tracker; | |
Dimension offDimension; | |
Image BG, offscreen, offImage, MC, Square, grid, Npiece, number; | |
Graphics offGraphics; | |
Thread animate = null; | |
int code = 0, thread = 1, frame = 0, frameb = 0, piece, pCount = 0, | |
count = 0, maxX = 0, minX = 340, maxY = 0, mCount = 0, | |
count2 = 0, lines = 0, lvl, winx = 306, | |
winy = 68, points=0,linecount; | |
int [] NX = new int[4], NY = new int[4];//this will have to be removed, and errors corrected to allow for reset game feature to work. | |
ArrayList<String> name = new ArrayList<String>(), Level = new ArrayList<String>(); | |
String image, lastkey, level, record,Points; | |
Random random = new Random(); | |
static String[][] TetrisGrid, pLayout; | |
Piece currentTetrimino, nextTetrimino; | |
long keyTime, dropTime; | |
public static int dropDelayInterval; | |
public static int minimumLockTime=300; | |
static final long keyHeldDelayInterval=100; | |
public ArrayList<Integer> keysActive; | |
public void init() { | |
animate = new Thread(this); | |
addKeyListener(this); | |
tracker = new MediaTracker(this); | |
setBackground(Color.BLACK); | |
resize(612, 612); | |
offscreen = createImage(612, 612); | |
BG = getImage(getCodeBase(), "Background.jpg"); | |
tracker.addImage(BG, 0); | |
} | |
public void start() { | |
keysActive=new ArrayList<Integer>(); | |
dropTime=System.currentTimeMillis(); | |
keyTime=System.currentTimeMillis(); | |
dropDelayInterval=1000; | |
TetrisGrid=new String[10][19]; | |
pLayout= new String[4][2]; | |
count = 0; | |
thread = 1; | |
points=0; | |
lvl=1; | |
currentTetrimino=getTetrimino(random.nextInt(7)); | |
nextTetrimino=getTetrimino(random.nextInt(7)); | |
NX = nextTetrimino.xPositions(); | |
NY = nextTetrimino.yPositions(); | |
for (int y = 0; y < 18; y++) {//grid blank | |
for (int x = 0; x < 10; x++) { | |
TetrisGrid[x][y] = null; | |
} | |
} | |
if(!animate.isAlive()) | |
animate.start(); | |
} | |
public void stop() { | |
System.exit(0); | |
} | |
public void destroy(){ | |
} | |
public static void printArray(int[] arr){ | |
for(int i = 0; i < arr.length; i++){ | |
System.out.print(arr[i]+" "); | |
} | |
System.out.println(); | |
} | |
@Override | |
public void run() { | |
while (Thread.currentThread()!=null) {//soem kind of speed differential when using the right handed side | |
//while(!hasFocus()){} | |
if(System.currentTimeMillis()-keyTime>=keyHeldDelayInterval&&!keysActive.isEmpty()){ | |
processKeys(); | |
keyTime=System.currentTimeMillis(); | |
} | |
//print2DArray(TetrisGrid); | |
//System.out.println(); | |
// printArray(currentTetrimino.xPositions); | |
// printArray(currentTetrimino.yPositions); | |
repaint(); | |
if (System.currentTimeMillis()-dropTime>=dropDelayInterval) {//system timed drop | |
currentTetrimino.softDrop(); | |
dropTime=System.currentTimeMillis(); | |
} | |
if(currentTetrimino.isLocked()){//takes next piece | |
System.out.println("Locked"); | |
for (int i = 0; i < 4; i++) {//i is some index for the PosX array | |
if (currentTetrimino.yPositions[i] >= 576|| (TetrisGrid[currentTetrimino.xPositions[i] / 34][(currentTetrimino.yPositions[i] / 34) + 1] != null && (currentTetrimino.yPositions[i] / 34) + 1 < 18)) {//TODO problem here when turning I off the top... idk how | |
for (int x = 0; x < 4; x++) //conversion spot from PosX[] and PosY[] to TetrisGrid storage locations | |
TetrisGrid[currentTetrimino.xPositions[x] / 34][currentTetrimino.yPositions[x] / 34] = currentTetrimino.getColor(); | |
currentTetrimino=nextTetrimino; | |
nextTetrimino=getTetrimino(random.nextInt(7)); | |
pLayout = nextTetrimino.getPLayout(); | |
for (int x = 0; x < 4; x++) { | |
if (TetrisGrid[currentTetrimino.xPositions[x] / 34][currentTetrimino.yPositions[x] / 34] != null) {//game over conditions. may haev messed with by tangling/fiddling | |
JOptionPane.showMessageDialog(null,"Game Over"); | |
String s = (String) JOptionPane.showInputDialog(null,"Enter your name:\n","Tetris",JOptionPane.PLAIN_MESSAGE,null, null, "Name"); | |
name.add(s); | |
Level.add(Points); | |
JOptionPane.showMessageDialog(null,toString()); | |
int n = JOptionPane.showConfirmDialog(null,"Play again?", "Tetris",JOptionPane.YES_NO_OPTION); | |
if (n == JOptionPane.YES_OPTION) { | |
start(); | |
} else if (n == JOptionPane.NO_OPTION) { | |
stop(); | |
} | |
} | |
} | |
} | |
} | |
for (int y = 0; y < 18; y++) {//line clearing related | |
for (int x = 0; x < 10; x++) { | |
if (TetrisGrid[x][y] != null)//point counting something | |
pCount++; | |
if (pCount == 10) { | |
for (int x2 = 0; x2 < 10; x2++) { | |
TetrisGrid[x2][y] = null; | |
} | |
for (int xb = 0; xb < 10; xb++) { | |
for (int yb = y; yb >= 0; yb--) { | |
if (yb - 1 != -1) | |
TetrisGrid[xb][yb] = TetrisGrid[xb][yb - 1]; | |
//System.out.println("something about line clearing"); | |
} | |
} | |
lines++; | |
if (lines % 10 == 0 && lines != 0) { | |
dropDelayInterval -= 100; | |
lvl++; | |
} | |
linecount++; | |
} | |
} | |
pCount = 0; | |
} | |
points+=(linecount*linecount)*100; | |
linecount=0; | |
count = 0; | |
} | |
count++; | |
} | |
} | |
public void paint(Graphics g) { | |
update(g); | |
} | |
public void update(Graphics g) {//assume this works | |
Dimension d = getSize(); | |
if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) { | |
offDimension = d; | |
offImage = createImage(d.width, d.height); | |
offGraphics = offImage.getGraphics(); | |
} | |
offGraphics.setColor(getBackground()); | |
offGraphics.fillRect(0, 0, d.width, d.height); | |
offGraphics.setColor(Color.black); | |
offGraphics.drawImage(BG, 0, 0, null); | |
drawPiece(offGraphics); | |
drawGrid(offGraphics); | |
level = "" + lvl; | |
for (int i = 0; i < level.length(); i++) { | |
number = getImage(getCodeBase(), level.charAt(i) + ".jpg"); | |
offGraphics.drawImage(number, (i * 34) + 34 * 14, 34 * 7, null); | |
} | |
Points=""+points; | |
for (int i = 0; i < Points.length(); i++) { | |
number = getImage(getCodeBase(), Points.charAt(i) + ".jpg"); | |
offGraphics.drawImage(number, (i * 34) + 34 * 11, 34 * 11, null); | |
} | |
g.drawImage(offImage, 0, 0, null); | |
} | |
public String toString() { | |
record = "Name Score \n"; | |
for (int i1 = 0; i1 < name.size(); i1++) { | |
record = record + name.get(i1) + " " + Level.get(i1) | |
+ "\n"; | |
} | |
return record; | |
} | |
public void drawPiece(Graphics g) { | |
for (int i = 0; i < 4; i++) { | |
Square = getImage(getCodeBase(), currentTetrimino.getColor()); | |
g.drawImage(Square, currentTetrimino.xPositions[i], currentTetrimino.yPositions[i], null); | |
for(int x=0;x<4;x++){ | |
for(int y=0;y<2;y++){ | |
Npiece = getImage(getCodeBase(), nextTetrimino.getPLayout()[x][y]); | |
g.drawImage(Npiece, x*34 + 408, y*34 + 60, null); | |
} | |
} | |
} | |
} | |
public Color selectColor(String colorString){ | |
if(colorString.equals("Bluesquare.jpg")) | |
return Color.blue; | |
if(colorString.equals("GreenSquare.jpg")) | |
return Color.green; | |
if(colorString.equals("OrangeSquare.jpg")) | |
return Color.orange; | |
if(colorString.equals("PinkSquare.jpg")) | |
return Color.pink; | |
if(colorString.equals("PurpleSquare.jpg")) | |
return Color.getHSBColor(128, 0, 128); | |
if(colorString.equals("RedSquare.jpg")) | |
return Color.red; | |
if(colorString.equals("YellowSquare.jpg")) | |
return Color.yellow; | |
System.out.println("what?"); | |
return Color.DARK_GRAY; | |
} | |
public void drawGrid(Graphics g) | |
{ | |
for(int i=0;i<10;i++) | |
for(int j=0;j<18;j++){ | |
grid = getImage(getCodeBase(),TetrisGrid[i][j]); | |
offGraphics.drawImage(grid, i*34, j*34,null); | |
} | |
} | |
//added by Winston | |
public void processKeys(){ | |
if(keysActive.contains(KeyEvent.VK_RIGHT)) | |
currentTetrimino.moveRight(); | |
if(keysActive.contains(KeyEvent.VK_LEFT)) | |
currentTetrimino.moveLeft(); | |
if(keysActive.contains(KeyEvent.VK_DOWN)) | |
currentTetrimino.softDrop(); | |
} | |
@Override | |
public void keyPressed(KeyEvent arg0) { | |
//moded by Winston | |
int key=arg0.getKeyCode(); | |
if(key==KeyEvent.VK_F) | |
currentTetrimino.rotateClockwise(); | |
else if(key==KeyEvent.VK_D) | |
currentTetrimino.rotateCounterClockwise(); | |
else if(key==KeyEvent.VK_SPACE) | |
currentTetrimino.hardDrop(); | |
else if(key==KeyEvent.VK_UP){//to | |
//currentTetrimino.rotate(); | |
currentTetrimino.rotateClockwise(); | |
} | |
else if(!keysActive.contains(key)){//the ones after this point are added to keysActive ArrList | |
keyTime=System.currentTimeMillis(); | |
keysActive.add(arg0.getKeyCode()); | |
if(key==KeyEvent.VK_RIGHT) | |
currentTetrimino.moveRight(); | |
else if(key==KeyEvent.VK_LEFT) | |
currentTetrimino.moveLeft(); | |
else if(key==KeyEvent.VK_DOWN) | |
currentTetrimino.softDrop(); | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent arg0) { | |
while(keysActive.contains((Integer) arg0.getKeyCode())) | |
keysActive.remove((Integer) arg0.getKeyCode()); | |
} | |
@Override | |
public void keyTyped(KeyEvent arg0) {} | |
private Piece getTetrimino (int n){ | |
switch(n){ | |
case 0: return new OTetrimino(); | |
case 1: return new ITetrimino(); | |
case 2: return new TTetrimino(); | |
case 3: return new LTetrimino(); | |
case 4: return new JTetrimino(); | |
case 5: return new ZTetrimino(); | |
case 6: return new STetrimino(); | |
} | |
return null; | |
} | |
public static void print2DArray(Object[][] arr) { | |
for (int i = 0; i < arr.length; i++) { | |
for (int j = 0; j < arr[i].length; j++) { | |
System.out.print(arr[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment