import java

import java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;

import javax.swing.*;

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

public class Game extends JPanel implements ActionListener, KeyListener
{
PieceData data = new PieceData(); //initializing info about each piece
Point shapes = data.shapes; //hold coordinates an rotation information about each piece
Color colors = data.colors; //holds the colors of each piece in respective order

Point startPoint;
int currentPiece;
int orientationOfCurrentPiece;
ArrayList upcomingPieces = new ArrayList();
Timer t;
int playerScore;
private Color tetBoard;

/**
* Constructor
*/
public Game() {

//standard tetris board size is
tetBoard = new Color1224;

/*
* Only colors the absolute edges; leaves field blank
*/
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 23; j++)
{
if (i == 0 || i == 12 || j == 22)
{
tetBoardij = Color.white;
}
else
{
tetBoardij = Color.BLACK;
}
}
}

t = new Timer(1000, this);
t.start();

//since this starts as empty, it gets filled with all 7 pieces and they are shuffled
showNextPiece();

addKeyListener(this);
}

// Put a new, random piece into the dropping position
public void showNextPiece() {

//this is the point from which the piece will enter the field
startPoint = new Point(5, 2);

//new piece starts out un-rotated
orientationOfCurrentPiece = 0;

/*
* adds new pieces if there are none left in the upcomingPieces array;
* has to be checked before accessing the next piece to avoid ArrayListOutOfBounds
*/
addNewPieces();

currentPiece = upcomingPieces.get(0); //access next piece
upcomingPieces.remove(0); //removes the piece
}

/**
* When the space key is pressed, this method is called and the piece is moved down. If the piece is being blocked
* or in the case of tetris, "held up", this method calls the stick method which makes the piece become a part
* of the board array
*/
public void movePieceDown() {
if (!hasCollided(startPoint.x, startPoint.y + 1, orientationOfCurrentPiece))
{
startPoint.y += 1;
} else
{
stick();
}
repaint();
}

public void addNewPieces()
{
//checks if there are no pieces left, if so, adds all possible ones in
if (upcomingPieces.isEmpty()) {
Collections.addAll(upcomingPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(upcomingPieces);
}
}

//Checks if the piece has collided
private boolean hasCollided(int xLoc, int yLoc, int rotation) {
for (Point p : shapescurrentPiecerotation) {
if (tetBoardp.x + xLocp.y + yLoc != Color.BLACK) {
return true;
}
}
return false;
}

// Rotate the piece clockwise or counterclockwise
public void rotatePiece(int i)
{
int newRotation = (orientationOfCurrentPiece + i) % 4;
if (newRotation 0; j–)
{
for (int i = 1; i 0; j–) {
gap = false;

/*
Runs through entire field and checks for gaps
Sets boolean accordingly
*/
for (int i = 1; i < 11; i++)
{
if (tetBoardij == Color.BLACK)
{
gap = true;
break;
}
}

/*
If there was no gap in a particular row on the field, delete it
*/
if (!gap)
{
deleteRow(j);
j += 1;
numClears += 1;
}
}

switch (numClears) {
case 1:
playerScore += 100;
break;
case 2:
playerScore += 300;
break;
case 3:
playerScore += 500;
break;
case 4:
playerScore += 800;
break;
}
}

// Draw the falling piece
private void drawPiece(Graphics g) {
g.setColor(colorscurrentPiece);
for (Point p : shapescurrentPieceorientationOfCurrentPiece) {
g.fillRect((p.x + startPoint.x) * 26,
(p.y + startPoint.y) * 26,
25, 25);
}
}

@Override
public void paintComponent(Graphics g)
{
//Paint the tetBoard
g.fillRect(0, 0, 26*12, 26*23);
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 23; j++) {
g.setColor(tetBoardij);
g.fillRect(26*i, 26*j, 26, 26);
}
}

// Display the playerScore
g.setColor(Color.WHITE);
g.drawString("" + playerScore, 19*12, 25);

// Draw the currently falling piece
drawPiece(g);
}

public void actionPerformed(ActionEvent e)
{
movePieceDown();
}

public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
rotatePiece(-1);
break;
case KeyEvent.VK_DOWN:
rotatePiece(+1);
break;
case KeyEvent.VK_LEFT:
movePiece(-1);
break;
case KeyEvent.VK_RIGHT:
movePiece(+1);
break;
case KeyEvent.VK_SPACE:
movePieceDown();
playerScore += 1;
break;
}
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}
}

x

Hi!
I'm Alfred!

We can help in obtaining an essay which suits your individual requirements. What do you think?

Check it out