俄罗斯方块 (转)

老早以前写的一个俄罗斯方块,不知有没有bug。 /* * 俄罗斯方块(基本功能实现只需400行左右代码) * 程序主框架为: gameInit() * run() -----> paint() & update() * | * |new curBlock <- | need new block? * | | * KListener(moveLeft(), moveRight(), * moveDown(), moveRotate()) * * 显示游戏界面 paint() ,update() * 初始化程序参数,创建游戏线程进入游戏循环{ * 响应用户操作 KListener */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Tetrics { JFrame mainF; TetricsCanvas tCanvas = new TetricsCanvas(); public Tetrics() { mainF = new JFrame(); mainF.getContentPane().add(tCanvas); showFrame(); } private void showFrame() { mainF.setTitle("Tetrics"); mainF.setSize(320,550); mainF.setResizable(false); mainF.setLocation(300,100); mainF.setVisible(true); } public static void main(String[] args) { new Tetrics(); } } -------------------------------------------------------------------------------- import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TetricsCanvas extends Canvas implements Runnable { boolean bGameOver = false; int UNITSIZE = 15, ROWS = 32, COLUMNS = 18; int[][] gameBoard = new int[ROWS + 1][COLUMNS]; int[][] blockBoard = new int[ROWS + 1][COLUMNS]; Block[] curBlock = new Block[4]; boolean bnewBlock = true; Image bgImage; Graphics bg; public TetricsCanvas() { setBackground(Color.white); addKeyListener(new KListener()); gameInit(); } public void gameInit() { for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLUMNS; j++) { gameBoard[i][j] = 0; blockBoard[i][j] = 0; } Thread self = new Thread(this); self.start(); } public void paint(Graphics g) { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLUMNS; j++) { g.setColor(Color.white); g.draw3DRect(20 + j * UNITSIZE, 20 + i * UNITSIZE , UNITSIZE, UNITSIZE, false); if(gameBoard[i][j] == 1 || blockBoard[i][j] == 1) { if(gameBoard[i][j] == 1) g.setColor(Color.blue); else if(blockBoard[i][j] == 1) g.setColor(Color.green); g.fill3DRect(20 + j * UNITSIZE, 20 + i * UNITSIZE , UNITSIZE, UNITSIZE, false); } else { g.setColor(Color.BLACK); g.fill3DRect(20 + j * UNITSIZE, 20 + i * UNITSIZE , UNITSIZE, UNITSIZE, false); } } } if(bGameOver) { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLUMNS; j++) { g.setColor(Color.black); g.fill3DRect(20 + j * UNITSIZE, 20 + i * UNITSIZE, UNITSIZE, UNITSIZE, false); } } g.setColor(Color.white); g.drawString("感谢您的测试", 120, 250); } } public void update(Graphics g) { if (bgImage == null) { bgImage = createImage(this.getSize().width,this.getSize().height); bg = bgImage.getGraphics(); } bg.setColor(getBackground()); bg.fillRect(0 ,0 ,this.getSize().width ,this.getSize().height); bg.setColor(getForeground()); paint (bg); g.drawImage(bgImage ,0 ,0 ,this); } public void run() { while(!bGameOver) { try { Thread.sleep(100); } catch(Exception e) { e.printStackTrace(); } if(bnewBlock) { newBlock(); checkGameOver(); bnewBlock = false; } else { bnewBlock = !moveDown(); } repaint(); } gameOverDlg(); } public void gameOverDlg() { JPanel msgP = new JPanel(); msgP.setLayout(new GridLayout(3, 1)); msgP.add(new JLabel("Author : pluto")); msgP.add(new JLabel("Date : 2005.4")); msgP.add(new JLabel("Site : BJTU15#")); JOptionPane.showMessageDialog(null, msgP, "Info", JOptionPane.YES_OPTION); } public void checkGameOver() { for(int i = 0 ;i < 4 ;i++) if(gameBoard[curBlock[i].row][curBlock[i].column] == 1) { bGameOver = true; System.out.println(bGameOver); } } class Block { int row ,column; public Block(int row ,int column) { this.row = row; this.column = column; } } public void newBlock() { int urow = 0; int ucolumn = COLUMNS / 2; switch((int)(Math.random() * 6)) { case 0: /* @@@@ */ curBlock[0] = new Block(urow ,ucolumn - 1); curBlock[1] = new Block(urow,ucolumn - 2); curBlock[2] = new Block(urow ,ucolumn); curBlock[3] = new Block(urow ,ucolumn + 1); break; case 1: /* @ @@@ */ curBlock[0] = new Block(urow + 1,ucolumn); curBlock[1] = new Block(urow ,ucolumn); curBlock[2] = new Block(urow + 1 ,ucolumn - 1); curBlock[3] = new Block(urow + 1 ,ucolumn + 1); break; case 2: /* @@ @@ */ curBlock[0] = new Block(urow + 1,ucolumn); curBlock[1] = new Block(urow ,ucolumn - 1); curBlock[2] = new Block(urow ,ucolumn); curBlock[3] = new Block(urow + 1 ,ucolumn + 1); break; case 3: /* @@ @@ */ curBlock[0] = new Block(urow + 1 ,ucolumn); curBlock[1] = new Block(urow ,ucolumn + 1); curBlock[2] = new Block(urow ,ucolumn); curBlock[3] = new Block(urow + 1 ,ucolumn - 1); break; case 4: /* @@ @@ */ curBlock[0] = new Block(urow ,ucolumn - 1); curBlock[1] = new Block(urow ,ucolumn); curBlock[2] = new Block(urow + 1 ,ucolumn - 1); curBlock[3] = new Block(urow + 1 ,ucolumn); break; case 5: /* @@@ @ */ curBlock[0] = new Block(urow ,ucolumn); curBlock[1] = new Block(urow ,ucolumn - 1); curBlock[2] = new Block(urow ,ucolumn + 1); curBlock[3] = new Block(urow + 1 ,ucolumn + 1); break; case 6: /* @@@ @ */ curBlock[0] = new Block(urow ,ucolumn); curBlock[1] = new Block(urow ,ucolumn + 1); curBlock[2] = new Block(urow ,ucolumn - 1); curBlock[3] = new Block(urow + 1 ,ucolumn - 1); break; } } public void moveLeft() { for(int i = 0 ;i < 4 ;i++) { if(curBlock[i].column - 1 < 0 || gameBoard[curBlock[i].row][curBlock[i].column - 1] == 1) return ; } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column] = 0; } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column - 1] = 1; } for(int i = 0 ;i < 4 ;i++) { curBlock[i] = new Block(curBlock[i].row ,curBlock[i].column - 1); } } public void moveRight() { for(int i = 0 ;i < 4 ;i++) { if(curBlock[i].column + 1 > COLUMNS - 1 || gameBoard[curBlock[i].row][curBlock[i].column + 1] == 1) return ; } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column] = 0; } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column + 1] = 1; } for(int i = 0 ;i < 4 ;i++) { curBlock[i] = new Block(curBlock[i].row ,curBlock[i].column + 1); } } public boolean moveDown() { for(int i = 0 ;i < 4 ;i++) { if(curBlock[i].row == ROWS - 1 || gameBoard[curBlock[i].row + 1][curBlock[i].column] == 1) { copyData(); return false; } } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column] = 0; } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row + 1][curBlock[i].column] = 1; } for(int i = 0 ;i < 4 ;i++) { curBlock[i] = new Block(curBlock[i].row + 1 ,curBlock[i].column); } return true; } public void copyData() { for(int i = 0 ;i < 4 ;i++) { gameBoard[curBlock[i].row][curBlock[i].column] = 1; } checkFullRow(); } public void checkFullRow() { int num = 0; for(int i = ROWS - 1 ;i >= 0 ;i--) { for(int j = COLUMNS - 1 ;j >= 0 ;j--) { if(gameBoard[i][j] == 1) num++; } if(num == COLUMNS) { for(int r = i ;r > 0 ;r--) for(int c = COLUMNS - 1 ;c >= 0 ;c--) { gameBoard[r][c] = gameBoard[r - 1][c]; blockBoard[r][c] = blockBoard[r - 1][c]; } for(int top = COLUMNS - 1 ;top >= 0 ;top--) { gameBoard[0][top] = 0; blockBoard[0][top] = 0; } i++; } num = 0; } } public boolean bRotate(Block[] tempBlock ,int i) { if(tempBlock[i].row < 0 || tempBlock[i].row > ROWS - 1 || tempBlock[i].column < 0 || tempBlock[i].column > COLUMNS - 1) return false; if(gameBoard[tempBlock[i].row][tempBlock[i].column] == 1) return false; return true; } public void moveRotate() { int[][] offset = new int[4][2]; for(int i = 0 ;i < 4 ;i++) { offset[i][0] = curBlock[i].column - curBlock[0].column; offset[i][1] = curBlock[i].row - curBlock[0].row; } Block[] tempBlock = new Block[4]; for(int i = 0 ;i < 4 ;i++) { tempBlock[i] = new Block(curBlock[i].row ,curBlock[i].column); } int tempx ,tempy; for(int i = 0 ;i < 4 ;i++) { if(offset[i][0] > 0 && offset[i][1] == 0) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = 0 + tempBlock[0].column; tempBlock[i].row = tempx + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] > 0 && offset[i][1] > 0) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = -tempx + tempBlock[0].column; tempBlock[i].row = tempy + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] == 0 && offset[i][1] > 0) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = -tempy + tempBlock[0].column; tempBlock[i].row = 0 + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] < 0 && offset[i][1] > 0 ) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = tempx + tempBlock[0].column; tempBlock[i].row = -tempy + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] < 0 && offset[i][1] == 0 ) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = 0 + tempBlock[0].column; tempBlock[i].row = tempx + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] < 0 && offset[i][1] < 0 ) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = -tempx + tempBlock[0].column; tempBlock[i].row = tempy + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] == 0 && offset[i][1] < 0 ) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = -tempy + tempBlock[0].column; tempBlock[i].row = 0 + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } else if(offset[i][0] > 0 && offset[i][1] < 0 ) { tempx = offset[i][0]; tempy = offset[i][1]; tempBlock[i].column = tempx + tempBlock[0].column; tempBlock[i].row = -tempy + tempBlock[0].row; if(!bRotate(tempBlock ,i)) { return ; } } } for(int i = 0 ;i < 4 ;i++) { blockBoard[curBlock[i].row][curBlock[i].column] = 0; } for(int i = 0 ;i < 4 ;i++) { blockBoard[tempBlock[i].row][tempBlock[i].column] = 1; } for(int i = 0 ;i < 4 ;i++) { curBlock[i] = new Block(tempBlock[i].row ,tempBlock[i].column); } } class KListener extends KeyAdapter { public void keyPressed(KeyEvent ke) { if(!bGameOver) { int command = ke.getKeyCode(); if(command == KeyEvent.VK_LEFT) { moveLeft(); repaint(); } if(command == KeyEvent.VK_RIGHT) { moveRight(); repaint(); } if(command == KeyEvent.VK_DOWN) { moveDown(); repaint(); } if(command == KeyEvent.VK_SPACE || command == KeyEvent.VK_UP) { moveRotate(); repaint(); } } } } }

你可能感兴趣的:(俄罗斯方块 (转))