Tetromino Game Java Program and GUI
Java Code:import java.awt.Color;
import java.awt.Graphics2D;
public class Block implements Comparable<Block>
{
private int xWidth;
private int yWidth;
private double xValue;
private double yValue;
private Color myColor;
public Block()
{
xWidth = 40;
yWidth = 40;
xValue = 0;
yValue = 0;
myColor = Color.RED;
}
public Block(int myXValue, int myYValue)
{
xWidth = 40;
yWidth = 40;
xValue = myXValue;
yValue = myYValue;
}
public void setColor(Color c)
{
myColor = c;
}
public Color getColor()
{
return myColor;
}
public void setXValue(double x)
{
xValue = x;
}
public double getXValue()
{
return xValue;
}
public void setYValue(double y)
{
yValue = y;
}
public double getYValue()
{
return yValue;
}
public void draw(Graphics2D g)
{
g.setColor(myColor);
g.fillRect((int) (getXValue()), (int) (getYValue()), xWidth, yWidth);
g.setColor(Color.BLACK);
g.drawRect((int) (getXValue()), (int) (getYValue()), xWidth, yWidth);
}
@Override
public int compareTo(Block b)
{
return ((int) (b.getYValue() - yValue));
}
}
-----------------------------------------------
import javax.swing.JFrame;
public class Driver
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Tetris");
frame.setSize(TetrisPanel.XFRAME, TetrisPanel.YFRAME);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TetrisPanel());
frame.setVisible(true);
}
}
------------------
import java.awt.Color;
import java.util.ArrayList;
public class IntersectTetromino extends Tetromino
{
public IntersectTetromino()
{
blockArray = new ArrayList<Block>();
for (int i = 0; i < 3; i++)
{
blockArray.add(new Block(160 + (40 * i), 40));
}
blockArray.add(new Block(200, 0));
setColor(Color.PINK);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 180)
y = -20;
if (getRotationAngle() == 360)
y = 20;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 90)
y = 20;
if (getRotationAngle() == 270)
y = -20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
--------------------------------------------
import java.awt.Color;
import java.util.ArrayList;
public class LeftLTetromino extends Tetromino
{
public LeftLTetromino()
{
blockArray = new ArrayList<Block>();
for (int i = 0; i < 3; i++)
{
blockArray.add(new Block(160 + (40 * i), 40));
}
blockArray.add(new Block(160, 0));
setColor(Color.YELLOW);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 180)
y = -20;
if (getRotationAngle() == 360)
y = 20;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 90)
y = 20;
if (getRotationAngle() == 270)
y = -20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
----------------------------
import java.awt.Color;
import java.util.ArrayList;
public class LeftZigZagTetromino extends Tetromino
{
public LeftZigZagTetromino()
{
blockArray = new ArrayList<Block>();
blockArray.add(new Block(200, 0));
blockArray.add(new Block(240, 0));
blockArray.add(new Block(160, 40));
blockArray.add(new Block(200, 40));
setColor(Color.CYAN);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 180)
y = -20;
if (getRotationAngle() == 360)
y = 20;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 90)
y = 20;
if (getRotationAngle() == 270)
y = -20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
--------------------------------------------
import java.awt.Color;
import java.util.ArrayList;
public class LineTetromino extends Tetromino
{
public LineTetromino()
{
blockArray = new ArrayList<Block>();
for (int i = 0; i < 4; i++)
{
blockArray.add(new Block(120 + (40 * i), 40));
}
setColor(Color.RED);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = -20;
if ((((getRotationAngle() / 90)) % 2) != 0)
y = 20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
-----------------------------------------------------
import java.util.ArrayList;
public class PartialTetromino extends Tetromino
{
public PartialTetromino(Block block)
{
blockArray = new ArrayList<Block>();
blockArray.add(block);
setColor(block.getColor());
}
public PartialTetromino(ArrayList<Block> theBlockArray)
{
blockArray = theBlockArray;
setColor(theBlockArray.get(0).getColor());
}
@Override
public double getXRotation()
{
return 0.0;
}
@Override
public double getYRotation()
{
return 0.0;
}
}
----------------------------------------
import java.awt.Color;
import java.util.ArrayList;
public class RightLTetromino extends Tetromino
{
public RightLTetromino()
{
blockArray = new ArrayList<Block>();
for (int i = 0; i < 3; i++)
{
blockArray.add(new Block(160 + (40 * i), 40));
}
blockArray.add(new Block(240, 0));
setColor(Color.GRAY);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 180)
y = -20;
if (getRotationAngle() == 360)
y = 20;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 90)
y = 20;
if (getRotationAngle() == 270)
y = -20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
-------------------------------------------
import java.awt.Color;
import java.util.ArrayList;
public class RightZigZagTetromino extends Tetromino
{
public RightZigZagTetromino()
{
blockArray = new ArrayList<Block>();
blockArray.add(new Block(160, 0));
blockArray.add(new Block(200, 0));
blockArray.add(new Block(200, 40));
blockArray.add(new Block(240, 40));
setColor(Color.GREEN);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 180)
y = -20;
if (getRotationAngle() == 360)
y = 20;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return ((highest + lowest) / 2) + y;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
int y = 0;
if (getRotationAngle() == 90)
y = 20;
if (getRotationAngle() == 270)
y = -20;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return ((highest + lowest) / 2) + y;
}
}
-------------------------------
import java.awt.Color;
import java.util.ArrayList;
public class SquareTetromino extends Tetromino
{
public SquareTetromino()
{
blockArray = new ArrayList<Block>();
blockArray.add(new Block(160, 40));
blockArray.add(new Block(200, 40));
blockArray.add(new Block(160, 80));
blockArray.add(new Block(200, 80));
setColor(Color.MAGENTA);
}
@Override
public double getXRotation()
{
double lowest = 9001;
double highest = 0;
for (Block block : blockArray)
{
if (block.getXValue() > highest)
highest = block.getXValue();
if (block.getXValue() < lowest)
lowest = block.getXValue();
}
return (highest + lowest) / 2;
}
@Override
public double getYRotation()
{
double lowest = 9001;
double highest = 0;
for (Block block : blockArray)
{
if (block.getYValue() > highest)
highest = block.getYValue();
if (block.getYValue() < lowest)
lowest = block.getYValue();
}
return (highest + lowest) / 2;
}
}
--------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
//Need to break down, too many lines of code.
public class TetrisPanel extends JPanel
{
public static final int XFRAME = 400;
public static final int YFRAME = 800;
public static final Color BACKGROUND = Color.black;
private BufferedImage myImage;
private Graphics2D myBuffer;
private ArrayList<Tetromino> tetrominoArray;
private Tetromino activeTetromino;
private Timer t;
private JTextField text;
public TetrisPanel()
{
myImage = new BufferedImage(XFRAME, YFRAME, BufferedImage.TYPE_INT_RGB);
myBuffer = (Graphics2D) myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, XFRAME, YFRAME);
activeTetromino = spawnRandomTetromino();
activeTetromino.draw(myBuffer, 0);
tetrominoArray = new ArrayList<Tetromino>();
String[] controlers = { "CONTROLERS","Left arrow to left", "Right arrow to right", "up arrow to change the shape", "download to sppeedy down"};
String[] help = {"add help"};
JComboBox control = new JComboBox(controlers);
add(control);
JComboBox hel = new JComboBox(help);
add(hel);
t = new Timer(1000, new Listener());
t.start();
addKeyListener(new Key());
setFocusable(true);
}
private class Listener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
activeTetromino.doMoveDown();
doCollisionChecksAndPaint(0);
}
}
private class Key extends KeyAdapter
{
@Override
public void keyPressed(KeyEvent e)
{
int rotation = 0;
if (e.getKeyCode() == KeyEvent.VK_DOWN)
activeTetromino.doMoveDown();
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
activeTetromino.doMoveLeft();
if (activeTetromino.sideCollision(getXArray(), getYArray()))
activeTetromino.doMoveRight();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
activeTetromino.doMoveRight();
{
if (activeTetromino.sideCollision(getXArray(), getYArray()))
activeTetromino.doMoveLeft();
}
if (e.getKeyCode() == KeyEvent.VK_UP)
rotation = 90;
doCollisionChecksAndPaint(rotation);
}
}
public void doCollisionChecksAndPaint(int rotation)
{
if (activeTetromino.getCollision(tetrominoArray))
{
tetrominoArray.add(activeTetromino);
//checkExplosion();
activeTetromino = spawnRandomTetromino();
} else
{
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, XFRAME, YFRAME);
for (Tetromino tetr : tetrominoArray)
tetr.draw(myBuffer, 0);
}
activeTetromino.draw(myBuffer, rotation);
repaint();
}
@Override
public void paintComponent(Graphics g)
{
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
/*public void checkExplosion()
{
ArrayList<Block> delArray = new ArrayList<Block>();
ArrayList<Double> yArray = getYArray();
Collections.sort(yArray);
Collections.reverse(yArray);
/*for (Double y : yArray)
{
int freq = Collections.frequency(getYArray(), y);
if (freq >= 10)
{
for (Tetromino tetr : tetrominoArray)
{
for (Block block : tetr.blockArray)
{
if (block.getYValue() == y)
delArray.add(block);
}
}
/*
for (Block delBlock : delArray)
{
for (int k = 0; k < tetrominoArray.size(); k++)
{
tetrominoArray.get(k).blockArray.remove(delBlock);
}
}
Collections.sort(tetrominoArray);
createPartialTetrominos();
Collections.sort(tetrominoArray);
for (int a = 0; a < tetrominoArray.size(); a++)
{
Tetromino tetr = tetrominoArray.get(a);
if (tetr.blockArray.isEmpty())
{
tetrominoArray.remove(tetr);
}
System.out.println(tetr.blockArray);
if (!tetr.getCollision(tetrominoArray))
{
tetr.doMoveDown();
}
}
}
}
*/
//}
private JTextField JTextField(String string) {
// TODO Auto-generated method stub
return null;
}
public void createPartialTetrominos()
{
for (int i = 0; i < tetrominoArray.size(); i++)
{
Tetromino tetr = tetrominoArray.get(i);
for (int j = 0; j < tetr.blockArray.size(); j++)
{
Block block = tetr.blockArray.get(j);
boolean isSeperate = false;
for (int k = 0; k < tetr.blockArray.size(); k++)
{
Block otherBlock = tetr.blockArray.get(k);
if (block != otherBlock)
{
// Checks to see if there is another block next to
// itself
boolean down = (block.getXValue() == otherBlock
.getXValue())
&& (block.getYValue() == (otherBlock
.getYValue() - 40));
boolean up = (block.getXValue() == otherBlock
.getXValue())
&& (block.getYValue() == (otherBlock
.getYValue() + 40));
boolean left = (block.getXValue() == (otherBlock
.getXValue() + 40))
&& (block.getYValue() == otherBlock.getYValue());
boolean right = (block.getXValue() == (otherBlock
.getXValue() - 40))
&& (block.getYValue() == otherBlock.getYValue());
if (!up && !down && !left && !right)
{
isSeperate = true;
}
}
}
if (isSeperate)
{
tetrominoArray.add(new PartialTetromino(block));
tetr.blockArray.remove(block);
}
}
}
}
public ArrayList<Double> getXArray()
{
ArrayList<Double> xArray = new ArrayList<Double>();
for (Tetromino tetr : tetrominoArray)
{
for (Block block : tetr.blockArray)
{
xArray.add(block.getXValue());
}
}
return xArray;
}
public ArrayList<Double> getYArray()
{
ArrayList<Double> yArray = new ArrayList<Double>();
for (Tetromino tetr : tetrominoArray)
{
for (Block block : tetr.blockArray)
{
yArray.add(block.getYValue());
}
}
return yArray;
}
public static Tetromino spawnRandomTetromino()
{
double rand = Math.random();
if (rand > .86)
return new SquareTetromino();
else if (rand > .72)
return new LineTetromino();
else if (rand > .58)
return new RightLTetromino();
else if (rand > .44)
return new LeftLTetromino();
else if (rand > .30)
return new LeftZigZagTetromino();
else if (rand > .15)
return new RightZigZagTetromino();
else
return new IntersectTetromino();
}
}
--------------------------------------------
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
public abstract class Tetromino implements Comparable<Tetromino>
{
public ArrayList<Block> blockArray;
private Color myColor;
private boolean isFinished;
private double rotationAngle;
public abstract double getXRotation();
public abstract double getYRotation();
public double getRotationAngle()
{
return rotationAngle;
}
public void setRotationAngle(double angle)
{
rotationAngle = angle;
}
public double getLowestBlock()
{
double total = TetrisPanel.YFRAME;
for (Block block : blockArray)
{
if (block.getYValue() < total)
total = block.getYValue();
}
return total;
}
public void setColor(Color c)
{
myColor = c;
for (Block block : blockArray)
{
block.setColor(myColor);
}
}
public Color getColor()
{
return myColor;
}
public boolean isFinished()
{
return isFinished;
}
public void setFinished(boolean isFinished)
{
this.isFinished = isFinished;
}
public boolean getCollision(ArrayList<Tetromino> tetrominoArray)
{
ArrayList<Double> xArray = new ArrayList<Double>();
ArrayList<Double> yArray = new ArrayList<Double>();
for (Tetromino tetr : tetrominoArray)
if (tetr != this)
{
for (Block block : tetr.blockArray)
{
xArray.add(block.getXValue());
yArray.add(block.getYValue());
}
}
for (Block block : blockArray)
{
for (int i = 0; i < xArray.size(); i++)
{
if ((xArray.get(i) == block.getXValue())
&& (yArray.get(i) == (block.getYValue() + 40)))
{
return true;
}
}
}
for (Block block : blockArray)
if (block.getYValue() == 760)
return true;
return false;
}
public boolean sideCollision(ArrayList<Double> xArray,
ArrayList<Double> yArray)
{
for (Block block : blockArray)
{
for (int i = 0; i < xArray.size(); i++)
{
if ((xArray.get(i) == block.getXValue())
&& (yArray.get(i) == (block.getYValue())))
{
return true;
}
}
}
return false;
}
public void doMoveDown()
{
for (Block block : blockArray)
{
block.setYValue(block.getYValue() + 40);
}
}
public void doMoveLeft()
{
for (Block block : blockArray)
{
block.setXValue(block.getXValue() - 40);
}
}
public void doMoveRight()
{
for (Block block : blockArray)
{
block.setXValue(block.getXValue() + 40);
}
}
public void draw(Graphics2D g, int rotation)
{
if (!isFinished())
{
setRotationAngle(getRotationAngle() + rotation);
double xRotation = getXRotation();
double yRotation = getYRotation();
boolean canDraw = true;
boolean leftTurn = false;
for (Block block : blockArray)
{
if (rotation > 0)
{
double newX = (yRotation - block.getYValue()) + xRotation;
double newY = -xRotation + block.getXValue() + yRotation;
block.setXValue(newX);
block.setYValue(newY);
}
if (block.getXValue() < 0)
canDraw = false;
else if ((block.getXValue() + 40) > TetrisPanel.XFRAME)
{
canDraw = false;
leftTurn = true;
}
block.draw(g);
}
if (!canDraw)
{
g.setColor(TetrisPanel.BACKGROUND);
g.fillRect(0, 0, TetrisPanel.XFRAME, TetrisPanel.YFRAME);
if (!leftTurn)
doMoveRight();
else
doMoveLeft();
draw(g, 0);
}
if (getRotationAngle() == 360)
setRotationAngle(0);
}
}
@Override
public int compareTo(Tetromino tetr)
{
return ((int) (tetr.getLowestBlock() - getLowestBlock()));
}
}
No comments:
Post a Comment