Java版俄罗斯方块
转载于:http://www.iteye.com/topic/595321
程序经过稍微的修改如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Terris extends JFrame implements Runnable, KeyListener {
static final String MY_PATH="D:/workspace/Russian/src/img/";
private short isPlaying=0,xOffSet = 2, yOffSet = 0, blockType = (short) Math.round(Math.random() * 6), blockRotation = 0, blockColor = (short) Math.round(Math.random() * 5);
private short matrix[][] = new short[21][10];//
private short block[][][][] = {{{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 1, 0, 0 } },/* l */
{ { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } } },/*-*/
{{ { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } }, /* z */
{ { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 },{ 0, 1, 0, 0 } } },/* z| */
{{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 },{ 0, 0, 0, 0 } }, /* xz */
{ { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } } },/* xz| */
{ { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } } },/** []*/
{{ { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 },{ 0, 0, 0, 0 } },
{ { 1, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } } },/* f */
{{ { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 },{ 0, 0, 0, 0 } } },/* xf */
{{ { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } } } };/* t */
private Image[] images = {new ImageIcon(MY_PATH+"Red.gif").getImage(),new ImageIcon(MY_PATH+"Blue.gif").getImage(),new ImageIcon((MY_PATH+"Pink.gif")).getImage(),new ImageIcon((MY_PATH+"BBlue.gif")).getImage(),new ImageIcon((MY_PATH+"Orange.gif")).getImage(),new ImageIcon((MY_PATH+"Green.gif")).getImage(),new ImageIcon(MY_PATH+"Red.gif").getImage()};
public Terris() {
setSize(160, 335);
setVisible(true);
createBufferStrategy(2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
}
public void paint(Graphics g) {
Graphics tg = this.getBufferStrategy().getDrawGraphics();
tg.fillRect(5, 30, 150, 340);
for (int i = 0; i < 21; i++)
for (int j = 0; j < 10; j++) {
if (matrix[i][j] != 0)
tg.drawImage(images[matrix[i][j]], j * 15 + 5, i * 15 + 15,null);
if (i < 4 && j < 4&& block[blockType][blockRotation][i][j] != 0)
tg.drawImage(images[blockColor + 1],((j + xOffSet) * 15) + 5,((i + yOffSet) * 15) + 15, null);
}
this.getBufferStrategy().show();
}
public static void main(String[] args) {
new Thread(new Terris()).start();
}
public void run() {
while (isPlaying==0)
try {
if (check(0, 0, 0, 1))
yOffSet += 1;
else {
if (yOffSet == 0) {
isPlaying = 1;
continue;
}
freezeAndNew();
}
repaint();
Thread.sleep(600);
} catch (InterruptedException e) {
}
}
private boolean check(int left, int right, int up, int down) {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (((xOffSet + j - left + right < 0 || xOffSet + j - left+ right >= 10) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| ((yOffSet + i + down >= 21) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| (block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0 && matrix[yOffSet+ i + down][xOffSet + j - left + right] != 0))
return false;
return true;
}
private void freezeAndNew() {
boolean[] clear = new boolean[4];
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++)
if (block[blockType][blockRotation][i][j] != 0)
matrix[i + yOffSet][j + xOffSet] = (short) (blockColor + 1);
clear[i]=i + yOffSet>=matrix.length?false:(matrix[i + yOffSet][0]!=0&&matrix[i + yOffSet][1]!=0&&matrix[i + yOffSet][2]!=0&&matrix[i + yOffSet][3]!=0&&matrix[i + yOffSet][4]!=0&&matrix[i + yOffSet][5]!=0&&matrix[i + yOffSet][6]!=0&&matrix[i + yOffSet][7]!=0&&matrix[i + yOffSet][8]!=0&&matrix[i + yOffSet][9]!=0);
}
for(int i=0;i<clear.length;i++)
if(clear[i])
for(int j=yOffSet+i;j>0;j--)
matrix[j]=matrix[j-1];
yOffSet = blockRotation = 0;
xOffSet = 2;
blockType = (short) Math.round(Math.random() * 6);
blockRotation = (short) Math.round((Math.random() * (block[blockType].length - 1)));
blockColor = (short) Math.round(Math.random() * 5);
}
public void keyPressed(KeyEvent e) {// 38-上 40-下 37-左 39-右
if ((e.getKeyCode() == 65 || e.getKeyCode() == 37) && check(1, 0, 0, 0)&&isPlaying==0) {// left
xOffSet--;
} else if ((e.getKeyCode() == 68 || e.getKeyCode() == 39)&& check(0, 1, 0, 0)&&isPlaying==0) {// right
xOffSet++;
} else if ((e.getKeyCode() == 87 || e.getKeyCode() == 38)&& check(0, 0, 1, 0)&&isPlaying==0) {// up
blockRotation = (short) ((blockRotation + 1) >= block[blockType].length ? 0: (blockRotation + 1));
} else if ((e.getKeyCode() == 83 || e.getKeyCode() == 40)&& check(0, 0, 0, 1)&&isPlaying==0) {// down
yOffSet += 1;
} else if((e.getKeyCode() == 83 || e.getKeyCode() == 40)&& isPlaying==0)
freezeAndNew();
repaint();
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}