//接上面class Workspace {
private static final int workspaceWidth = 10;
private static final int workspaceHeight = 30;
public int getWorkspaceWidth(){ return workspaceWidth;}
public int getWorkspaceHeight(){ return workspaceHeight;}
private int[][] workspace = new int[workspaceHeight][workspaceWidth];
public void setWorkspaceValue(int x,int y){
workspace[y][x] = 1;
}
public int getWorkspaceValue(int x,int y){
return workspace[y][x];
}
/**
*扫描workspace 是否有满的行
*返回一个数组,数组的值为满的行对应的行数
* @return
*/
public int[] scanWorkspace(){
int w;
int[] fullLines = new int[workspaceHeight];
for(int h = 0; h < workspaceHeight; h++){
for(w = 0; w < workspaceWidth; w++){
if(workspace[h][w] == 0) break;
}
if(w == workspaceWidth) fullLines[h] = h;
else fullLines[h] = 0;
}
int[] lines = {0, 0, 0, 0};
int i = 0;
for(int h = 0; h < workspaceHeight; h++){
if(fullLines[h] != 0){
lines[i] = fullLines[h];
i++;
}
}
return lines;
}
public void deleteLines(int[] lines){
for(int i = 0; i < lines.length; i++){
if(lines[i] != 0){
for(int y = lines[i]; y > 0; y--)
for(int x = 0; x < workspaceWidth; x++)
workspace[y][x] = workspace[y-1][x];
}
}
}
}class Game extends Thread {
private int score;
private int level;
private int speed;
protected int getScore(){ return score; }
protected int getLevel(){ return level; }
protected int getSpeed(){ return speed; }
private int[][] levelSpeed = {{0, 2000, 4000, 6000, 8000,10000,13000,16000,20000,0},
{0, 500, 400, 300, 200, 100, 80, 65, 50, 0}};
private int[] linesScore = {0, 100, 300, 500, 1000 };
private Workspace ws = new Workspace();
private Block block = new Block();
private int[][] newshape = new int[4][4];//用于对方块的打操作
private int shapePosX ;
private int shapePosY ;
protected synchronized void generateNewShape(){// 产生新的方块
newshape = block.generateShape();
shapePosX = (int)ws.getWorkspaceWidth()/2;
shapePosY = ws.getWorkspaceHeight();
}
public int getWorkspaceValue(int x,int y){
return ws.getWorkspaceValue(x,y);
}
/**
* 将产生的方块映射到workspace里
* @param x
* @param y
* @param shape
*/
protected void blockToWorkspace(int x,int y,int[][] shape){
for(int yy = 0; yy < shape.length; yy++){
for(int xx = 0; xx < shape[yy].length; xx++){
//使方块不会超出workspace的范围
if(shape[yy][xx] == 1 && y+yy < ws.getWorkspaceHeight()
&& x+xx < ws.getWorkspaceWidth()) {
ws.setWorkspaceValue(x+xx,y+yy);
}
}
}
}
protected boolean canPlace(int x,int y,int[][] shape){
if((x >= 0)&&(x < ws.getWorkspaceWidth())//未越界
&&(y >= 0)&&(y < ws.getWorkspaceHeight())){
for(int yy = 0; yy < block.getBlockCorner(); y++){
for(int xx = 0; xx < block.getBlockCorner(); xx++){
//workspace是否不为空
if((shape[yy][xx] == 1)&&(ws.getWorkspaceValue(xx+x,yy+y) == 1))
return false;
}
}
return true;
}
else { return false; }//越界
}
protected synchronized void moveLeft(){
if(canPlace(shapePosX-1,shapePosY,newshape)){
shapePosX = shapePosX - 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void moveRight(){
if(canPlace(shapePosX+1,shapePosY,newshape)){
shapePosX = shapePosX + 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void moveDown(){
if(canPlace(shapePosX,shapePosY+1,newshape)){
shapePosY = shapePosY + 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void turn(){
int[][] turnedShape = block.turn(newshape);
if(shapePosX+1 >= ws.getWorkspaceWidth())//shape挨着右壁
return ;
if(canPlace(shapePosX,shapePosY,turnedShape)){
for(int y = 0; y < block.getBlockCorner(); y++)
for(int x = 0; x < block.getBlockCorner(); x++){
//shape 紧挨的右壁是否为空的
if(newshape[y][x]==1 && newshape[y][x+1]!=1 &&
ws.getWorkspaceValue(shapePosX+x+1,shapePosY+y)==1)
return ;
}
newshape = turnedShape;
blockToWorkspace(shapePosX,shapePosY,newshape);
return ;
}
else return ;
}
protected boolean gameOver(){
for(int x = 0; x < ws.getWorkspaceWidth(); x++){
if(ws.getWorkspaceValue(x,0)==1)
return true;
}
return false;
}
private synchronized void gameHalt(int sp){
try{
Thread.sleep(sp);
}catch(InterruptedException e){}
}
/**
* 俄罗斯方块的主要运行函数
*/
public void run(){
score = 0;
level = 1;
speed = 500;
generateNewShape();//产生新的方块
blockToWorkspace(shapePosX,shapePosY,newshape);
while(true){
System.out.println("game start!!");
gameHalt(speed);//方块停顿
moveDown();
if(!canPlace(shapePosX,shapePosY,newshape)){
int[] lines = ws.scanWorkspace();
ws.deleteLines(lines);
System.out.println("game start!!");
int colines = 0;//一次共删除的行数
for(int i = 0; i < lines.length; i++){
if(lines[i] != 0) colines++;
}
score += linesScore[colines];//加分
if(score >= levelSpeed[0][level]){//够分时过关
level++;
speed = levelSpeed[1][level];
}
if(gameOver()) return;
generateNewShape();//产生新的方块
}
}
}
}
private static final int workspaceWidth = 10;
private static final int workspaceHeight = 30;
public int getWorkspaceWidth(){ return workspaceWidth;}
public int getWorkspaceHeight(){ return workspaceHeight;}
private int[][] workspace = new int[workspaceHeight][workspaceWidth];
public void setWorkspaceValue(int x,int y){
workspace[y][x] = 1;
}
public int getWorkspaceValue(int x,int y){
return workspace[y][x];
}
/**
*扫描workspace 是否有满的行
*返回一个数组,数组的值为满的行对应的行数
* @return
*/
public int[] scanWorkspace(){
int w;
int[] fullLines = new int[workspaceHeight];
for(int h = 0; h < workspaceHeight; h++){
for(w = 0; w < workspaceWidth; w++){
if(workspace[h][w] == 0) break;
}
if(w == workspaceWidth) fullLines[h] = h;
else fullLines[h] = 0;
}
int[] lines = {0, 0, 0, 0};
int i = 0;
for(int h = 0; h < workspaceHeight; h++){
if(fullLines[h] != 0){
lines[i] = fullLines[h];
i++;
}
}
return lines;
}
public void deleteLines(int[] lines){
for(int i = 0; i < lines.length; i++){
if(lines[i] != 0){
for(int y = lines[i]; y > 0; y--)
for(int x = 0; x < workspaceWidth; x++)
workspace[y][x] = workspace[y-1][x];
}
}
}
}class Game extends Thread {
private int score;
private int level;
private int speed;
protected int getScore(){ return score; }
protected int getLevel(){ return level; }
protected int getSpeed(){ return speed; }
private int[][] levelSpeed = {{0, 2000, 4000, 6000, 8000,10000,13000,16000,20000,0},
{0, 500, 400, 300, 200, 100, 80, 65, 50, 0}};
private int[] linesScore = {0, 100, 300, 500, 1000 };
private Workspace ws = new Workspace();
private Block block = new Block();
private int[][] newshape = new int[4][4];//用于对方块的打操作
private int shapePosX ;
private int shapePosY ;
protected synchronized void generateNewShape(){// 产生新的方块
newshape = block.generateShape();
shapePosX = (int)ws.getWorkspaceWidth()/2;
shapePosY = ws.getWorkspaceHeight();
}
public int getWorkspaceValue(int x,int y){
return ws.getWorkspaceValue(x,y);
}
/**
* 将产生的方块映射到workspace里
* @param x
* @param y
* @param shape
*/
protected void blockToWorkspace(int x,int y,int[][] shape){
for(int yy = 0; yy < shape.length; yy++){
for(int xx = 0; xx < shape[yy].length; xx++){
//使方块不会超出workspace的范围
if(shape[yy][xx] == 1 && y+yy < ws.getWorkspaceHeight()
&& x+xx < ws.getWorkspaceWidth()) {
ws.setWorkspaceValue(x+xx,y+yy);
}
}
}
}
protected boolean canPlace(int x,int y,int[][] shape){
if((x >= 0)&&(x < ws.getWorkspaceWidth())//未越界
&&(y >= 0)&&(y < ws.getWorkspaceHeight())){
for(int yy = 0; yy < block.getBlockCorner(); y++){
for(int xx = 0; xx < block.getBlockCorner(); xx++){
//workspace是否不为空
if((shape[yy][xx] == 1)&&(ws.getWorkspaceValue(xx+x,yy+y) == 1))
return false;
}
}
return true;
}
else { return false; }//越界
}
protected synchronized void moveLeft(){
if(canPlace(shapePosX-1,shapePosY,newshape)){
shapePosX = shapePosX - 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void moveRight(){
if(canPlace(shapePosX+1,shapePosY,newshape)){
shapePosX = shapePosX + 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void moveDown(){
if(canPlace(shapePosX,shapePosY+1,newshape)){
shapePosY = shapePosY + 1;
blockToWorkspace(shapePosX,shapePosY,newshape);
}
}
protected synchronized void turn(){
int[][] turnedShape = block.turn(newshape);
if(shapePosX+1 >= ws.getWorkspaceWidth())//shape挨着右壁
return ;
if(canPlace(shapePosX,shapePosY,turnedShape)){
for(int y = 0; y < block.getBlockCorner(); y++)
for(int x = 0; x < block.getBlockCorner(); x++){
//shape 紧挨的右壁是否为空的
if(newshape[y][x]==1 && newshape[y][x+1]!=1 &&
ws.getWorkspaceValue(shapePosX+x+1,shapePosY+y)==1)
return ;
}
newshape = turnedShape;
blockToWorkspace(shapePosX,shapePosY,newshape);
return ;
}
else return ;
}
protected boolean gameOver(){
for(int x = 0; x < ws.getWorkspaceWidth(); x++){
if(ws.getWorkspaceValue(x,0)==1)
return true;
}
return false;
}
private synchronized void gameHalt(int sp){
try{
Thread.sleep(sp);
}catch(InterruptedException e){}
}
/**
* 俄罗斯方块的主要运行函数
*/
public void run(){
score = 0;
level = 1;
speed = 500;
generateNewShape();//产生新的方块
blockToWorkspace(shapePosX,shapePosY,newshape);
while(true){
System.out.println("game start!!");
gameHalt(speed);//方块停顿
moveDown();
if(!canPlace(shapePosX,shapePosY,newshape)){
int[] lines = ws.scanWorkspace();
ws.deleteLines(lines);
System.out.println("game start!!");
int colines = 0;//一次共删除的行数
for(int i = 0; i < lines.length; i++){
if(lines[i] != 0) colines++;
}
score += linesScore[colines];//加分
if(score >= levelSpeed[0][level]){//够分时过关
level++;
speed = levelSpeed[1][level];
}
if(gameOver()) return;
generateNewShape();//产生新的方块
}
}
}
}
thank you very much!!!!
改好贴出来大家研究一下~~~~哈哈
http://expert.csdn.net/Expert/topic/2692/2692594.xml?temp=.4883539
不过只是出现一个什么都没有的Window
然后命令行窗口一直打印“game start!!”
关键的线程函数run()就是俄罗斯方块动作的整个过程,我也不知这样写对不对,对线程不是很了解 :-)Tetris 类就将Game类用图形化的方式显示出来,addKeyListener()是增加键盘输入功能.大概的思路就是这样.我的思路有点像IP/TCP协议的思想一层叠一层(哈哈,只打个比喻而已,那可以跟IP/TCP相提并论 :-) )最后希望大家将我的错出来和修正~~~~ 改正确后有分加~~
麻烦了.
改好后发给我哦,
谢谢
去这看看,说不定你有更大的收获呢.