代码全程无注释,想必一些零基础的可能稍微看不懂,没关系,COPY进去自己慢慢研究


cell类

import java.awt.Image;

public class Cell {

private int row;
private int col;
private Image p_w_picpath;
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public Image getImage() {
return p_w_picpath;
}
public void setImage(Image p_w_picpath) {
this.p_w_picpath = p_w_picpath;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public Cell(int row, int col, Image p_w_picpath) {
super();
this.row = row;
this.col = col;
this.p_w_picpath = p_w_picpath;
}
public void drop(){
row++;
}
public void right(){
col++;
}
public void left(){
col--;
}
@Override
public String toString() {


return row+" "+col;
}



}

tetromino类

------------------------------------------




import java.util.Random;

public abstract class Tetromino {

Cell[] cells=new Cell[9];
public Tetromino(){

}

public void softDrop(){
for(Cell cell:cells){
cell.drop();
}
}
public void moveright(){
for(Cell cell:cells){
cell.right();
}
}
public void moveleft(){
for(Cell cell:cells){
cell.left();
}
}
public static Tetromino randomOne(){
Random random=new Random();
int type=random.nextInt(6);
switch (type) {
case 0:return new T();
// case 1:return new I();
case 1:return new S();
case 2:return new J();
case 3:return new O();
case 4:return new Z();
case 5:return new L();

default:
return null;
}
}
private static class T extends Tetromino{
public T(){
cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,Tetris.T );
cells[4]=new Cell(1,4,Tetris.T);
cells[5]=new Cell(1,5,Tetris.T);
cells[6]=new Cell(2,3,null);
cells[7]=new Cell(2,4,Tetris.T );
cells[8]=new Cell(2,5,null);

}
}
// private static class I extends Tetromino{
// public I(){
// cells[0] = new Cell(0, 4, Tetris.I);
// cells[1] = new Cell(0, 3, Tetris.I);
// cells[2] = new Cell(0, 5, Tetris.I);
// cells[3] = new Cell(0, 6, Tetris.I);
// }
// }
private static class S extends Tetromino{
public S(){

cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,null);
cells[4]=new Cell(1,4,Tetris.S);
cells[5]=new Cell(1,5,Tetris.S);
cells[6]=new Cell(2,3,Tetris.S);
cells[7]=new Cell(2,4,Tetris.S );
cells[8]=new Cell(2,5,null);
}
}
private static class Z extends Tetromino{
public Z(){
cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,Tetris.Z );
cells[4]=new Cell(1,4,Tetris.Z);
cells[5]=new Cell(1,5,null);
cells[6]=new Cell(2,3,null);
cells[7]=new Cell(2,4,Tetris.Z );
cells[8]=new Cell(2,5,Tetris.Z );
}
}
private static class L extends Tetromino{
public L(){
cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,Tetris.L );
cells[4]=new Cell(1,4,Tetris.L);
cells[5]=new Cell(1,5,Tetris.L);
cells[6]=new Cell(2,3,Tetris.L);
cells[7]=new Cell(2,4,null);
cells[8]=new Cell(2,5,null);
}
}
private static class J extends Tetromino{
public J(){
cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,Tetris.J );
cells[4]=new Cell(1,4,Tetris.J);
cells[5]=new Cell(1,5,Tetris.J);
cells[6]=new Cell(2,3,null);
cells[7]=new Cell(2,4,null);
cells[8]=new Cell(2,5,Tetris.J );
}
}
private static class O extends Tetromino{
public O(){
cells[0]=new Cell(0,3,null);
cells[1]=new Cell(0,4,null);
cells[2]=new Cell(0,5,null);
cells[3]=new Cell(1,3,null);
cells[4]=new Cell(1,4,Tetris.O);
cells[5]=new Cell(1,5,Tetris.O);
cells[6]=new Cell(2,3,null);
cells[7]=new Cell(2,4,Tetris.O );
cells[8]=new Cell(2,5,Tetris.O );
}
}



}
=====================================================================

tetris类



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.p_w_picpath.BufferedImage;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import javax.p_w_picpathio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.xml.bind.Marshaller.Listener;

import org.w3c.dom.events.Event;

//import com.sun.corba.se.spi.orbutil.fsm.Action;
//import com.sun.org.apache.bcel.internal.generic.LAND;
//import com.tarena.tetris.Cell;

import sun.awt.RepaintArea;
import sun.net.www.content.audio.wav;

public class Tetris extends JPanel{

private int SCORE;
private int lines;
public static final int ROWS=20;
public static final int COLS=10;
public static final int CELL_SIZE=26;
public Cell[][] wall = new Cell[ROWS][COLS];
public static Tetromino tetromino ;
public static Tetromino nextone;
public static BufferedImage bg;
public static BufferedImage T;
public static BufferedImage I;
public static BufferedImage S;
public static BufferedImage Z;
public static BufferedImage L;
public static BufferedImage J;
public static BufferedImage O;
public static BufferedImage go;
public static BufferedImage K;
static{
try {
bg=ImageIO.read(Tetris.class.getResource("tetris.png"));
I=ImageIO.read(Tetris.class.getResource("I.png"));
S=ImageIO.read(Tetris.class.getResource("S.png"));
L=ImageIO.read(Tetris.class.getResource("L.png"));
Z=ImageIO.read(Tetris.class.getResource("Z.png"));
O=ImageIO.read(Tetris.class.getResource("O.png"));
T=ImageIO.read(Tetris.class.getResource("T.png"));
J=ImageIO.read(Tetris.class.getResource("J.png"));
go=ImageIO.read(Tetris.class.getResource("game-over.png"));
K=ImageIO.read(Tetris.class.getResource("k.png"));
} catch ( Exception e) {
e.printStackTrace();
}
}
private boolean pause;
private boolean finish;
private Timer timer;
public void startAction(){
clearWall();
pause =false;
finish=false;
tetromino=Tetromino.randomOne();
nextone=Tetromino.randomOne();
lines=0;
SCORE=0;
timer=new Timer();
timer.schedule(new TimerTask(){

@Override
public void run() {
// TODO Auto-generated method stub
softDorpAction();
repaint();

}}, 7000,7000);
}
private void clearWall() {
// TODO Auto-generated method stub
for(Cell[] cell:wall){
Arrays.fill(cell, null);
}

}
public void pauseACtion(){
pause =true;
timer.cancel();

}
public void continueAction(){

pause=false;
timer=new Timer();
timer.schedule(new TimerTask(){

@Override
public void run() {
// TODO Auto-generated method stub
softDorpAction();
repaint();

}}, 700,700);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf =new JFrame("俄罗斯方块");
jf.setLocation(500, 0);
jf.setSize(530,580);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
Tetris tetris=new Tetris();
jf.add(tetris);
jf.setColor.black);
jf.add(tetris) ;
jf.setResizable(false);
// jf.setUndecorated(true);
// jf.setLocationRelativeTo(null);
tetris.action();
}
public void action() {

// wall[19][5]=new Cell(19,5,Tetris.T);
// tetromino = Tetromino.randomOne();
//nextone=Tetromino.randomOne();
startAction();
repaint();
KeyListener listener=new KeyAdapter(){

public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
if(tetromino!=null){
softDorpAction();
}
break;
case KeyEvent.VK_LEFT:
if(tetromino!=null){
moveLeftAction();
break;
}
case KeyEvent.VK_RIGHT:
if(tetromino!=null){
moveRightAction();
}
break;
case KeyEvent.VK_SPACE:
if(tetromino!=null){
hardDorpAction();
}
break;
case KeyEvent.VK_S:
for(Cell[] line: wall){ Arrays.fill(line, null);}
action();
break;
case KeyEvent.VK_UP:
change();
repaint();
break;



default:
break;
}
repaint();
}


};
this.addKeyListener(listener);
this.requestFocus();


}
@Override
public void paint(Graphics g) {

g.drawImage(bg, 0, 0, null);
g.translate(15, 15);
drawWall(g);
drawTetromino(g);
nextOne(g);
g.setColor(Color.BLACK);
Font font=getFont();
font=new Font(font.getName(),font.BOLD,30);
g.setFont(font);
g.drawString("SCORE:"+SCORE, 289, 160);
g.drawString("Lines:"+lines,289, 218);
checkGame(g);


}
private void checkGame(Graphics g) {
if(wall[1][4]!=null){
g.drawImage(this.go, 0, 0, null);
tetromino=null;
}

}
private void nextOne(Graphics g) {

Cell[] cells=nextone.cells;
for(Cell cell:cells){
if(cell!=null)
g.drawImage(cell.getImage(),
cell.getCol()*CELL_SIZE+260,cell.getRow()*CELL_SIZE+25, null);
}
}
private void drawWall(Graphics g) {

for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
Cell cell=wall[i][j];
if(cell==null){
g.drawRect(j*CELL_SIZE, i*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}else{
g.drawImage(cell.getImage(), j*CELL_SIZE, i*CELL_SIZE, null);
}
}
}


}
private void drawTetromino(Graphics g) {
if(tetromino!=null){
Cell[] cells=tetromino.cells;
for(Cell cell:cells){
if(cell!=null&&cell.getImage()!=null)
g.drawImage(cell.getImage(),cell.getCol()*CELL_SIZE,
cell.getRow()*CELL_SIZE, null);
}

}
}
private void moveLeftAction(){
Cell[] cells=tetromino.cells;
tetromino.moveleft();
for(Cell cell:cells){
if(cell.getCol()<0/*&&cell.getImage()!=null*/||
wall[cell.getRow()][cell.getCol()]!=null){
tetromino.moveright();
}
}

}
private void moveRightAction(){
Cell[] cells=tetromino.cells;
tetromino.moveright();
for(Cell cell:cells){
if(cell.getCol()>9||
wall[cell.getRow()][cell.getCol()]!=null){
tetromino.moveleft();
}
}

}
private void softDorpAction(){
if(canDrop()){
tetromino.softDrop();
}else{
landWall();
destroyLine();

tetromino=nextone;
nextone=Tetromino.randomOne();
}

}
private void hardDorpAction(){
while(canDrop()){
tetromino.softDrop();
}
landWall();
destroyLine();

tetromino=nextone;
nextone=Tetromino.randomOne();


}

private void destroyLine() {
for(int i=0;i<ROWS;i++){
if(fullCell(i)){
clearLine(i);
}
}
}
private void clearLine(int j) {
for(int i=j;i>0;i--){
System.arraycopy(wall[j-1], 0, wall[j], 0, COLS);
System.out.println("bbbbb");
}
Arrays.fill( wall[0],null);

}
private boolean fullCell(int j) {

for(int i=0;i<COLS;i++){
if(wall[j][i]==null)
return false;
}
return true;
}
private void landWall() {
if(tetromino!=null){
Cell[] cells=tetromino.cells;

for(Cell cell:cells){
if(cell.getImage()!=null)
wall[cell.getRow()][cell.getCol()]=
new Cell(cell.getRow(),cell.getCol(),cell.getImage());
}
}
}
private boolean canDrop(){
if(tetromino==null){
return false;
}
Cell[] cells=tetromino.cells;
for(Cell cell:cells){
if(cell.getRow()>ROWS-2||
wall[cell.getRow()+1][cell.getCol()]!=null&&
cell.getImage()!=null){
return false;
}
}
return true;
}
public void change(){
Cell[] cell=new Cell[9];
cell[0]=new Cell(tetromino.cells[6].getRow()-2,
tetromino.cells[6].getCol(),tetromino.cells[6].getImage());
cell[1]=new Cell(tetromino.cells[3].getRow()-1,
tetromino.cells[3].getCol()+1,tetromino.cells[3].getImage());
cell[2]=new Cell(tetromino.cells[0].getRow(),
tetromino.cells[0].getCol()+2,tetromino.cells[0].getImage());
cell[3]=new Cell(tetromino.cells[7].getRow()-1,
tetromino.cells[7].getCol()-1,tetromino.cells[7].getImage());
cell[4]=tetromino.cells[4];
cell[5]=new Cell(tetromino.cells[1].getRow()+1,
tetromino.cells[1].getCol()+1,tetromino.cells[1].getImage());
cell[6]=new Cell(tetromino.cells[8].getRow(),
tetromino.cells[8].getCol()-2,tetromino.cells[8].getImage());
cell[7]=new Cell(tetromino.cells[5].getRow()+1,
tetromino.cells[5].getCol()-1,tetromino.cells[5].getImage());
cell[8]=new Cell(tetromino.cells[2].getRow()+2,
tetromino.cells[2].getCol(),tetromino.cells[2].getImage());

tetromino.cells=cell;
repaint();

}
}